Polymorphism Question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2006
Posts: 23
Reputation: jrivera is an unknown quantity at this point 
Solved Threads: 0
jrivera jrivera is offline Offline
Newbie Poster

Polymorphism Question

 
0
  #1
Feb 22nd, 2008
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?

  1. void boardconfig_level::initMineArray() {
  2. if (this->checkValues()){
  3. //do really cool things here
  4. }
  5. //JR throw error here
  6. }
  7.  
  8. bool boardconfig_level::checkValues() {
  9. //this seems to fail although child::getDimension returns number bigger than 1
  10. if (this->getDimension() < 1) {
  11. cout << "Invalid Board Dimension" << this->getDescription() << endl;
  12. return false;
  13. }
  14.  
  15. return true;
  16. }
  17.  
  18. boardconfig_level_basic::boardconfig_level_basic() {
  19. this->initMineArray();
  20. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,577
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Polymorphism Question

 
0
  #2
Feb 22nd, 2008
I guess what you need to do is make parent->GetDimension() a pure firtual function, which means all children must implement it.
  1. class base
  2. {
  3. // pure virtual function
  4. int getDimention() = 0;
  5. ...
  6. };
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Polymorphism Question

 
0
  #3
Feb 22nd, 2008
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
Last edited by vijayan121; Feb 22nd, 2008 at 2:20 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC