This question already has answers here:
C++ virtual function from constructor [duplicate]
(7 answers)
Closed 9 years ago.
I have this layout
class Base {
public:
virtual void Initialize() { // base Implementation }
Base() { Initialize(); }
};
class der_1 : public Base
{
public:
der_1() : Base() {}
virtual void Initialize() { // der_1 Implementation }
};
class der_2 : public Base
{
public:
der_2() : Base() {}
virtual void Initialize() { // der_2 Implementation }
};
Now, whenever I create a new object of class der_1 or der_2, I will end up calling the base implementation of Initialize(). Apparently, I can't call a virtual function while the object is being created.
As of now, I am calling the Initialize function after I create the object of type der_1 or der_2, which doesn't seem a correct practice to me as that will couple the Initialize function call to each time an object is created.
Can someone suggest me better alternatives?
During the constructor call, the object "is" still only an instance of the base class, so it does know about your overloaded Initialize() function.
There are some suggestions for dealing with the situation here:
C++ virtual function from constructor
Related
This question already has answers here:
What is object slicing?
(18 answers)
Why doesn't polymorphism work without pointers/references?
(6 answers)
Closed 2 years ago.
I have a base and derived class which derived class is overriding a function in base class
struct Base{
virtual void action(){}
}
struct Derived:public Base{
virtual void action() override{}
}
I have another class that uses Base interface like following
struct Observable{
deque<Base> myObjects;
void addObject(Base &base){
myObjects.push_back(base);
}
void notify(){
for (auto it = myObjects.begin(); it != myObjects.end(); it++) {
it.action();
}
}
}
In main I initialized observer class like this
Derived myDeived;
Observable observable;
observable.addObject(myDerived);
observable.notify();
the problem when I call "notify" and as a consequence it call all "action" function in array It does not call "action" function overrided in Derived but in Base class? please explain me why this happened and how to correct it?
Thank you
This question already has answers here:
Why does an overridden function in the derived class hide other overloads of the base class?
(4 answers)
Closed 3 years ago.
I have a Base class where there are 2 overloaded expanded() functions
I am overloading one of them in Derived class and trying to call the other one inside it.
class Base
{
public:
bool expanded()
{
return false;
}
void expanded(bool isit)
{
}
};
class Derived : public Base
{
public:
void expanded(bool isit)
{
expanded();
}
};
This fails with compilation error: 'Derived::expanded': function does not take 0 arguments
The new method i.e. the method in child class hides the scope of the old one. To call it, you need to be explicit with the scope:
class Derived : public Base
{
public:
void expanded(bool isit)
{
Base::expanded();
}
};
and if you want to maintain the same interface, you'll need to redefine in the derived class.
class Derived : public Base
{
public:
void expanded(bool isit)
{
Base::expanded();
}
bool expanded()
{
return Base::expanded();
}
};
The followings will work:
Solution 1:
class Derived : public Base
{
public:
void someOtherMethod(bool isit)
{
expanded();
}
};
Solution 2:
class Derived : public Base
{
public:
void expanded(bool isit)
{
// Will call child's expanded(bool) and will become recursive.
expanded(false);
}
};
Solution 3:
class Derived : public Base
{
public:
void expanded(bool isit)
{
Base::expanded();
}
};
When you define a method same as parent method in your child class, then any time that method name is encountered in the child class, the compiler will search for the definition in the child class only. That is what is happening here. As the child class' expanded takes 1 argument, compiler is expecting a parameter to be passed.
Similar question has already been answered in Overloads of inherited member functions
Adding to the previous answer. There is no overloading across scopes – derived class scopes are not an exception to this general rule. You can easily solve your error by declaring Baseclass like this
class Derived : public Base
{
public:
using Base::expanded;
void expanded(int isit)
{
expanded();
}
};
This question already has answers here:
Calling virtual functions inside constructors
(15 answers)
Closed 7 years ago.
I'd like to do the following:
void do_stuff(Base* base_ptr) {
// here I need the overridden methods
base_ptr->init();
}
class Base {
Base() {
do_stuff(this);
}
virtual void init() {}
};
class Derived : public Base {
virtual void init() override {
// Derived specific init
}
}
But all I get are calls to Base::init(), is it even possible to do what I intend?
You are calling a virtual function from within the constructor!
Duplicated of -> Calling virtual functions inside constructors
This question already has answers here:
How to call a parent class function from derived class function?
(7 answers)
Closed 7 years ago.
C++ newbie here. Is there a way to call an overloaded function in a base class from the same function name with the same signature in a derived class? In Smalltalk, I can do it the "super" keyword. Is there any C++ equivalent?
class MyBaseClass {
void initialize() { doSomething(); }
};
class MyDerivedClass : public MyBaseClass {
void initialize() {
super initialize(); // first, call MyBaseClass::initialize()
doLocalInitialize(); // now initialize non-inherited members
}
Thanks,
Norm
You literally said the correct syntax in your comment :)
class MyBaseClass {
void initialize() { doSomething(); }
};
class MyDerivedClass : public MyBaseClass {
void initialize() {
MyBaseClass::initialize(); // first, call MyBaseClass::initialize()
doLocalInitialize(); // now initialize non-inherited members
}
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Calling virtual functions inside constructors
class Base
{
virtual void method()
{ cout << "Run by the base."; };
public:
Base() { method(); };
};
class Derived: public Base
{
void method()
{ cout << "Run by the derived."; };
};
void main()
{
Derived();
}
Output:
Run by the base.
How can one have the derived method run instead, without making a derived constructor?
Calling virtual functions inside constructors
http://www.artima.com/cppsource/nevercall.html
You can't since the "derived" part of the object has not been constructed yet so calling a member function from it would be undefined behavior.
You can't do this without adding extra code.
A common way to achieve this is to use a private constructor and a create function that first calls the constructor (via new) and then a second finish_init method on the newly created object. This does prevent you from creating instances of the object on the stack though.