C++ - How to call creator class/object

2019-08-02 04:06发布

I need to call properties and functions of an object from a different class.

The idea is passing 'this' as a parameter to the other class constructor. E.g.:

instance = ClassName(this);

And then do:

ParentClass parentInstance;
ClassName::ClassName(MainApp _instance){
    parentInstance = _instance;
}

However, my compiler says that ParentClass does not name a type. Ideas? Also, should I use a pointer to save memory? How?

Thanks in advance.


UPDATE:

Ok, sorry for the delay. Here it goes the actual code. First, a simple class.

Game class:

Header file

#ifndef _GAME
#define _GAME

#include "ofMain.h"

class Game{

 public:
  Game();
  ~Game();

  void hi();
};

#endif

cpp file:

#include "Game.h"
Game::Game(){}
Game::~Game(){}

void Game::hi(){ 
cout << "hi, I'm game! " << endl;
}

Then, from MainApp I create the object: - Relevant code on header file:

#ifndef _MAIN_APP
#define _MAIN_APP

#include "ofMain.h"
#include "Game.h"

class MainApp : public ofSimpleApp{
 public:
  Game game;
};

#endif

Relevant code on the cpp file:

game = Game();
game.hi();

This obviously works as I'm only creating a bloody object. However, problem comes with composition.

I could pass the main app as argument in the constructor, I could pass it via game.setParent(this);... problem is, I can't even define the variable to store the reference to the app.

E.g.: (making it easy/inefficient without pointers or anything)

Game.h:

#define _GAME
#ifndef _GAME

#include "ofMain.h"
#include "MainApp.h"

class Game{
 MainApp app;

 public:
  Game();
  ~Game();

  void hi();
 };
#endif

This returns a "does not name a type" error and declaring class MainApp returns an "incomplete type" error

I'm sure I'm doing something dumb.


UPDATE 2:

The problem with that method is that I can't call a function of the pointed object now.

This is Game.h:

#ifndef _GAME
#define _GAME

#include "ofMain.h"

class MainApp;
class Game{


 public:
  Game();
  Game(MainApp* _app);
  ~Game();

  void hi();

  MainApp* app;
};

#endif

As you see, app (of the type MainApp) is passed as a parameter. That's fine, MainApp exists as it's the forward declaration. However, when I try to call any of app's functions I can't (compiler error saying Request for member appHi in .... which is non-class type 'MainApp'.

MainApp is NOT included in Game.h but Game.h IS included in MainApp.h.

Ideas?

7条回答
等我变得足够好
2楼-- · 2019-08-02 04:19

I think there may be two issues here:

1) You need to declare the ParentClass (i.g. #include its .hpp-file) before using it

2) The assignment "parentInstance = _instance" will invoke the assignment operator, which i'm guessing is not what you want. let "parentInstance" be a pointer instead.

查看更多
虎瘦雄心在
3楼-- · 2019-08-02 04:23

The problem is you have a circular reference - Game includes MainApp, and MainApp includes game. You need a 'forward declaration', as per the example by DeadMG.

See here.

查看更多
爷的心禁止访问
4楼-- · 2019-08-02 04:24

Note the section on "#include." http://www.cplusplus.com/doc/tutorial/program_structure/

After the "Intro to the C++ Language" section look for the verbiage about #include. http://www.cprogramming.com/tutorial/lesson1.html

Namespaces: http://www.tenouk.com/Module23.html

HTH

查看更多
一夜七次
5楼-- · 2019-08-02 04:25

It's called composition and is a common pattern. It's highly efficient in both semantics and in terms of runtime speed/memory footprint.

Your code example is a little too much pseudocode for me to read it correctly. Let me show you how it's done.

class X;
class Y {
    ...
    void DoSomething(X* x, ... args);
};
class X {
    Y y;
    void DoSomething() {
        y.DoSomething(this, args);
    }
};
查看更多
劳资没心,怎么记你
6楼-- · 2019-08-02 04:26

If all you need to do is use functions and data members of another class, read up on the friend keyword. It will allow access to class members from other classes.

UPDATE: Alternatively, store a pointer or reference to the object you need access to, and make getters for data members and make the functions public... but I get the feeling this is not what you're after...

查看更多
Animai°情兽
7楼-- · 2019-08-02 04:27

That's not how things work in C++. Unlike javascript, you cannot inject methods or fields into existing objects at runtime.

查看更多
登录 后发表回答