#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>

class Helper 
{
public:
    Helper() { init(); }
    virtual void print() {
        int nSize = m_vItems.size();
        std::cout << "Size : " << nSize << std::endl;
        std::cout << "Items: " << std::endl;
        for(int i=0; i<nSize; i++) {
            std::cout << m_vItems[i] << std::endl;
        }
    }
protected:
    virtual void init() { m_vItems.push_back("A"); }
    std::vector<std::string> m_vItems;
};

class ItemsHelper : public Helper
{
public:
    ItemsHelper() { }
protected:
    virtual void init() { 
        Helper::init();
        m_vItems.push_back("B");
    }
};

int _tmain(int argc, _TCHAR* argv[]) {
    ItemsHelper h;
    h.print();
}

This output's that how big the vector is 1. I was expecting the dimensions to become 2 because within the ItemsHelper::init function I known as the bottom class Helper::init() function, i quickly give a second item towards the vector. The issue is, the ItemsHelper::init does not get known as, the bottom class init function will get known as rather.

I would like the ItemsHelper::init function to obtain known as, and that i can perform that by calling the init function within the ItemsHelper ctor instead of within the base class. BUT, now you ask ,, it is possible to better way to accomplish this but still keep your call towards the init() within the base class? Because let's say I wish to produce a Assistant object rather than a ItemsHelper, then your init function would not get known as.

btw, this can be a simplified version of the problem I am seeing inside a much bigger object, I simply made these objects up for instance.

Inside a base class constructor, the derived class has not been built therefore the overriden function around the derived class is not available. There is a FAQ entry about this somewhere... that we aren't able to find.

The easiest option would be to simply place the .push_back("A") a part of init in to the Helper constructor and also the .push_back("B") in to the ItemsHelper constructor. This appears to complete what you're attempting to do and reduces the unwanted init virtual function.

Mind that virtual functions fail to work as "expected" in constructors!

Helper() { init(); }

Here, init() will invariably call the "init" from the current class (Assistant), despite the fact that it's marked virtual.

EDIT: There is a similar question on SO.

Generally (unless of course you understand fully how constructors and virtual functions are specified to operate), you should not call virtual functions in constructors, because you generally will not obtain the 'most virtual' version from the function. The fast version of methods virtual functions operate in constructors is the fact that once the virtual function is known as, you will get the main one for that 'current' degree of the course heirarchy that's presently being built.

Begin to see the following articles for particulars:

However , virtual functions aren't effective how you think they are doing in constructors. When an ItemsHelper is built, first the bottom class Assistant in built. In the constructor, the kind of the item is Assistant, therefore the call to init calls Assistant::init(). Then your ItemsHelper constructor is known as. There's not a way to call a derived class function from the base class constructor. The very best you should do is call init() following the ItemsHelper object is built.