Reputation: 26333
I am lost. I have a class Editor
and a class Controller
. Classes WorkflowEditor
and WorkflowController
derive from Editor
and Controller
respectively. Class Controller
has a protected member Editor editor
and class WorkflowController
has a private member WorkflowEditor editor
(with same name).
Edit from EitanT:
Here's a simplified code snipped to illustrate what the OP has described:
class Controller
{
Editor editor;
// ...
}
class WorkflowEditor : public Editor {
// ...
};
class WorkflowController : public Controller {
WorkflowEditor editor;
// ...
};
My application is a module with graphical interface. In workflow mode, a ribbon appears on Launch, and a wizard is displayed. On click on one button, a method in class Controller
is called. Execution crashes because at this time, Editor editor
class member of object with type Controller
is dead. I would like class member Editor editor
to be the same as class member WorkflowEditor editor
(same name).
In other words, if a class A
has a member of class B
and class childA
(derived from A
) has a member of class childB
(derived from B
), and member of type childB
and B
have same name, isn't the member "inherited"?
Upvotes: 0
Views: 123
Reputation: 254431
The object is inherited, but is not the same object as the one declared in the derived class. The derived class member is a separate object to the base class member, even though they have the same name. Technically, it hides the base class member, making it accessible only with its qualified name, Controller::editor
.
You can achieve what you want with a virtual function, which you override in the derived class to access an object contained there:
class Controller {
public:
// No data members, just an abstract interface
// Access a data member of the derived class
virtual Editor & editor() = 0;
virtual ~Controller() {}
};
class WorkflowController : public Controller {
public:
WorkflowEditor & editor() {return editor_;}
private:
// The object itself - accessible as its true type here, or
// as its abstract type via the abstract interface.
WorkflowEditor editor_;
};
Upvotes: 2
Reputation: 91825
The member in the derived class hides the member in the base class.
Upvotes: 1