trouble with polymorphism

This is a discussion on trouble with polymorphism within the C++ Programming forums, part of the General Programming Boards category; I'm trying to implement a generic minimax class so that I can just fill in the functions that change with ...

  1. #1
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179

    trouble with polymorphism

    I'm trying to implement a generic minimax class so that I can just fill in the functions that change with different implementations. I tried declaring the functions that get overwritten as virtual void whatever()=0; but I need to instantiate the class in order to build a tree for the algorithm, so I went and changed all my pure virtual functions to be like virtual void whatever() {}. This works for calling the overwritten functions in everywhere but one spot. Like I said, I need ot instantiate a root for a tree, and I need to call functions on this class:
    Code:
    MiniMaxGame* root = new MiniMaxGame(player1Type,player2Type,boardw,boardh); //this is why they can't be pure virtual
    root->expand(side); //doing this calls the empty expand in the base class, no good
    I thought that I could get it to call the new function by putting it in a wrapper like this and using the same mechanism that works everywhere else:
    Code:
    void doExpand(int side) {expand(side);}
    but this just gives the same problem. Is there some way to get around this, or should I look at a way to write the algorithm that doesn't require instantiating the base class?
    Illusion and reality become impartiality and confidence.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I thought that I could get it to call the new function
    Functions in a class are called by objects of that class. Presumably root is an object of the base class, so how can it call a function defined in a derived class? Furthermore, there can be many derived classes and each one can override the same virtual function in the base class. How would the linker know which derived class function to call?

    I thought that I could get it to call the new function by putting it in a wrapper like this and using the same mechanism that works everywhere else:
    Base classes do not inherit derived class functions--it's the other way around: derived classes inherit base class functions. Base class objects can only call functions defined in the base class. So, when you write

    baseObject.doExpand(expand(side))

    the linker looks around in the base class for the expand() function.

    Is there some way to get around this, or should I look at a way to write the algorithm that doesn't require instantiating the base class?
    You could define an expand() function in the base class that does what you want. That is the whole idea behind virtual functions: the base class objects can be defined to do one thing, and the derived class objects can be defined to do something else--or by not overriding the base class function, derived objects can inherit the same behavior.
    Last edited by 7stud; 01-25-2006 at 11:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism - "pointers" or "references"?
    By Petike in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2009, 05:06 PM
  2. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 11:58 AM
  3. The trouble with polymorphism
    By Stabbrie in forum C++ Programming
    Replies: 6
    Last Post: 04-05-2005, 09:06 AM
  4. Inheritance and Polymorphism
    By bench386 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 09:19 PM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21