How to call a derived class method from a base class method within the constructor of base?

No. All parts of B (starting with A, as it's base) are constructed before B's constructor is called. So, by the time SetNumbers is called, no part of B (except for the A part) has been constructed --- and that may include the v-table, so there's no way to know where that call is going to go Of course, there is a simple solution to this: Call B::SetNumber() from within B's constructor (That is, after all, the purpose of B's constructor).

No. All parts of B (starting with A, as it's base) are constructed before B's constructor is called. So, by the time SetNumbers is called, no part of B (except for the A part) has been constructed --- and that may include the v-table, so there's no way to know where that call is going to go.

Of course, there is a simple solution to this: Call B::SetNumber() from within B's constructor (That is, after all, the purpose of B's constructor).

Well... this is just an simple example for a server program I was writing... but I guess I have to rethink the automatic call of Init()... – Incubbus Aug 18 '10 at 19:08 @Incubbus: A lot of people who ask that question come from languages, like ObjectiveC, which don't have ctors, and where the idiom you describe in the norm -- and wonder why the idiom is broken in C++. – James Curran Aug 18 '10 at 19:12.

You can't do this for the simple logical reason that while the base class is being constructed, the derived class hasn't even begun to be constructed. You can't call a member function on an object that doesn't exist (yet). In practice, even if you managed to call SetNumbers and assign to the member variables of the derived class before they were initialized they would surely be overwritten when they finally get initialized.

I admit it's a bit pointless to reason about this as we would be well outside defined behaivour.

No, sorry. :( It might compile in one or two C++ compilers, but it's not recommended. From the C++ FAQ Lite section 10.7: 10.7 Should you use the this pointer in the constructor?...snip... Here is something that never works: the {body} of a constructor (or a function called from the constructor) cannot get down to a derived class by calling a virtual member function that is overridden in the derived class.

If your goal was to get to the overridden function in the derived class, you won't get what you want. Note that you won't get to the override in the derived class independent of how you call the virtual member function: explicitly using the this pointer (e.g. , this->method()), implicitly using the this pointer (e.g. , method()), or even calling some other function that calls the virtual member function on your this object. The bottom line is this: even if the caller is constructing an object of a derived class, during the constructor of the base class, your object is not yet of that derived class.

You have been warned. NOTE: Emphasis mine. More details at the link.

The only time you can do this is when something is derived from a template that is parameterised by itself: template class base { T* down_cast() throw() { return static_cast(this); } const T* down_cast() const throw() { return static_cast(this); } public: base() { down_cast()->doSomething(); } /* … */ }; class derived : private base { public: void doSomething() { } }; Note that doSomething is public and not virtual. We can static_cast to derived, because it's known that derived is the derived type. Deriving something from a base parameterised by itself is a strange thing to be doing at the best of times.It's said that when the ATL team in microsoft used it they asked the C++ compiler team if it was valid and nobody was sure, though it is valid because template construction depends on names as follows: First the template is available, but not used in a class.

Then, the name derived available. Then it instantiates the layout of base — this requires knowledge of the member variables and virtual functions, as long as none of that depends upon knowledge of derived’s layout (pointers and references are fine) this will all go okay. Then it will create the layout of derived, and finally it will create derived’s member functions, which may include creating member functions for base.

So as long as base doesn’t contain a derived member variable (base classes can never contain a member variable of a type derived from themselves) or a virtual function that requires knowledge of derived’s layout we can indeed do the dicey-looking piece of inheritance above. This includes being able to call non-virtual public members of derived from base during construction, because it's already part of base. There are strong limitations on this.

In particular, if doSomething() depends on anything constructed in derived's constructor it won't work as derived hasn't been constructed yet. Now, is this actually a good idea?No.

A simple design solution is to use aggregation instead of inheritance.

That would merely make what he is doing impossible. It would, however, not solve his problem (since his design is based on inheritance) – James Curran Aug 18 '10 at 19:20 That's why I'm saying it's a design alternative. – Nikolai N Fetissov Aug 18 '10 at 19:46 Besides, what he's trying to do is impossible anyway.

– Nikolai N Fetissov Aug 18 '10 at 20:03 s current design compiles, but fails at run-time. Your solution merely makes it fail at compile time. However, except for this one hurdle, his overall task is suited for inheritance.

Changing the entire design to aggregation, just to solve this problem is pointless extra work. – James Curran Aug 18 '10 at 20:47 My solution is to avoid inheritance if possible, and never use protected data members. I think we are arguing about different things.

– Nikolai N Fetissov Aug 18 '10 at 20:58.

C++ - How to call a derived class method from a base class method within the constructor of base - Stack Overflow.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions