| | |
Polymorphism Question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2006
Posts: 23
Reputation:
Solved Threads: 0
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?
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?
C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
class base { // pure virtual function int getDimention() = 0; ... };
Last edited by Ancient Dragon; Feb 22nd, 2008 at 9:44 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
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
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
Last edited by vijayan121; Feb 22nd, 2008 at 2:20 pm.
![]() |
Similar Threads
- C++ interview questions (C++)
- Just a quick question (C++)
- Overloading / Redefining / Overriding (C++)
- OOP's and GUI's...oh my! (Computer Science)
- virtual methods and inheritance (C++)
- hello need a little help here (Java)
- Inheritance and Polymorphism (C++)
- Need example of how to use INHERITANCE! (C++)
- Why Data Structures???...QUESTIONS INSIDE (C++)
Other Threads in the C++ Forum
- Previous Thread: A quick question, about compiling...
- Next Thread: wierd exception handling - can someone explain ?
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






