0

coming from Java I'm a bit confused about calling the base class constructor (or super()). I have 2 classes: Player (abstract class), and HumanPlayer which is a child of Player. now, i have a constructor for Player with the declaration:

Player(string name, list<Point> points);

and now im trying to implement something of this sort:

HumanPlayer() {
    string name = get name from user...
    list<Point> points = get points from user...
    ...
    ...
    ...
    super(name, points);
}

really new to c++, having a hard time with it's syntax. regards,

Felix Kreuk
  • 193
  • 1
  • 9
  • Please check [this question](http://stackoverflow.com/questions/120876/c-superclass-constructor-calling-rules). It should give you the correct syntax. – Nemesis Nov 08 '14 at 15:25
  • i did. but it forces me to call the super constructor before i implement the child constructor. doesnt help me – Felix Kreuk Nov 08 '14 at 15:26
  • Not completely sure, but can you use `Player(name, points);` instead of `super()`? – Nemesis Nov 08 '14 at 15:28
  • nop. player is abstract. – Felix Kreuk Nov 08 '14 at 15:28
  • You typically wouldn't collect data from the user in a constructor, especially in a parameter-less constructor. All kinds of interesting things will happen when you add the object to an STL container, among other things. – 3Dave Nov 08 '14 at 15:39

2 Answers2

5

The only way to initialize base of a derived class is in constructor's initialization list. That's the thing after the colon, before the function body. There's no other NORMAL way to do that.

HumanPlayer() :
        Player("...", list_of_points)
{
...
}

If you really HAVE TO initialize the base "in" the body of the constructor, not on the list, you have to modify your code - for example by introducing an "initialize()" function in the base class or by using "pimpl idiom".

Freddie Chopin
  • 8,440
  • 2
  • 28
  • 58
  • "you have to modify your code - for example by introducing an "initialize()" function in the base class or by using "pimpl idiom"." ... or fix your design. – edmz Nov 08 '14 at 15:48
1

If the "get name from user..." and "get points from user..." bits are very simple, and can be implemented as simple expressions or function calls:

HumanPlayer()
   : Player( "get name from user... code",
             "get points from user... code")
{


}

If, on the other hand, both "get" bits are non-trivial, and have mutual dependencies on each other, then this is going to be a bit more complicated. A brief synopsis of a typical approach to something like this:

class HelperClass {

public:

    std::string name;
    std::list<Point> points;

    HelperClass()
    {
         // The constructor here will do whatever is needed to initialize name and points
    }
};


// ...

HumanPlayer(const HelperClass &helper=HelperClass())
 : Player(helper.name, helper.points)
{
}

Some might find this to be somewhat hack-ish: abusing default parameter values to instantiate an object used in the constructor. But it works. If using a C++ compiler that supports at least the C++11 standard, this can be done slightly better, in a different way.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148