This is Homework.

I have been working on a tournament program. Currently the tournament class gets user input and instantiates a player class and a game class. The game class then calls the board class where it instantiates a boardconfig class object. At this point Is where I get my problem.

the boardconfig class is a grandparent to the boardconfig_level class which is parent to the boardconfig_level_basic class.

Grandparent class has the following method:
protected int getDimension() { return 0; }

Parent class has the following methods
protected void intiMineArray(); //defined in parent.cpp
protected virtual int getDimension() {return 0; }
private bool checkValues(); //defined in parent.cpp

Child Class has:
protected int getDimension () {return 6;}

The problem is when the constructor for the child class is called, it call s parent::initMineArray(). This method then calls the Parent::checkValues() which finally calls getDimension(). My problem is instead of calling the child::getDimension(), it calls the parent getDimension which always returns 0 instead ocalling child::getDimension.

What am I doing wrong?

void boardconfig_level::initMineArray() {
    if (this->checkValues()){ 
        //do really cool things here
    }
    //JR throw error here
}

bool boardconfig_level::checkValues() {
    //this seems to fail although child::getDimension returns number bigger than 1
    if (this->getDimension() < 1) {
        cout << "Invalid Board Dimension" << this->getDescription() << endl;
        return false;
    }

    return true;
}

boardconfig_level_basic::boardconfig_level_basic() {
    this->initMineArray();
}

I guess what you need to do is make parent->GetDimension() a pure firtual function, which means all children must implement it.

class base
{
// pure virtual function
    int getDimention() = 0;
...
};

the problem is that you are calling a virtual function during the construction of the base class. this will not behave the way you expect it to behave; the wrong vtable is being pointed to at the time. if you call virtual functions during construction or destruction, such calls will never be dispatched to a more derived class than the one that is currently being constructed or destroyed.
for a more detailed explanation of why this is so (and why it is a good thing) and a possible work around, see http://www.artima.com/cppsource/nevercall.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.