Reputation: 961
I am learning C++ inheritance and I'm trying to access a container class method.
I've got an Environment class that has a public method called drawText().
The Environment class has a Robot class robot inside it:
robot = new Robot;
How can I call drawText() from inside the Robot class?
Thank you in advance!
Upvotes: 1
Views: 778
Reputation: 472
This is what you need:
class Environment {
public:
Environment() { }
~Environment() { }
setRobot(Robot* robot) {
robot_ = robot;
}
drawTest() { }
private:
Robot* robot_;
};
class Robot {
public:
Robot(Environment* env) {
env_ = env;
}
void foo() {
env_.drawTest();
}
private:
Environment* env_;
};
Upvotes: 2
Reputation: 343
You can add in Robot.h Enviroment reference
//robot.h
namespace envrNamespace
{
class Environment;
};
namespace rbtNamespace
{
class Robot
{
Environment* _parent;
void setParent(Environment* _env) {_parent = _env;};
Environment* getParent() {return _parent;};
//...
}
};
//Environment.cpp
Robot* robot = new Robot();
robot->setParent(this);
Upvotes: 3