2

I am trying to use a delegated constructor and am trying to follow the format found in this question and this question, however, I am still having issues.

My player.h file is this:

#ifndef PLAYER_H_
#define PLAYER_H_

#include <string>

class Player
{
public:
   Player(void);
   Player(std::string name, int score);
   ~Player();
protected:
   std::string name_;
   int score_;
};

#endif

My player.cpp file is this:

#include "player.h"
Player::Player(std::string name, int score)
{
   score_ = score;
   name_ = name;
}

Player::Player(void) : Player(NULL,0)
{

}

However, when I try to compile, I get the following error:

1>a:\projects\test\src\player.cpp(5): error C2614: 'Player' : illegal member initialization: 'Player' is not a base or member

What am I doing wrong? If it is relevant, I am using VS2012.

Community
  • 1
  • 1
  • 2
    As far as I know (though I don't know much), vs2012 doesn't support delegated constructors. – Michael Krelin - hacker Sep 24 '12 at 20:39
  • 5
    You want to use delegating constructors but don't even know how to use ordinary initializer lists? I think you might benefit from backing up a bit... – Kerrek SB Sep 24 '12 at 20:40
  • 1
    @KerrekSB I was using initializer lists, but I thought they might be causing the issue so I got rid of them. I'm not sure why you're trying to discourage people who are learning the language from learning the different ways to call a constructor. –  Sep 24 '12 at 20:45
  • 3
    @Kyryx: OK, fair enough - but with initalizer lists, the second constructor would have been easy to write as `Player() : name_(), score_() { }`, so not having delegating constructors wouldn't have been a big issue... – Kerrek SB Sep 24 '12 at 20:50

2 Answers2

14

If it is relevant, I am using VS2012.

It is, because Visual Studio doesn't implement this C++11 feature. Sorry. There are quite a lot of C++11 features they don't implement.

T.E.D.
  • 44,016
  • 10
  • 73
  • 134
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
7

This isn't an answer to your question, but even though your compiler doesn't support delegating con­struc­tors, I want to address a few separate issues that your code would otherwise have suffered from:

  1. Use initializer lists, not assignment.

  2. When constructing heavy objects, pass by value and move.

  3. You cannot initialize a std::string from a null pointer. If you want an empty string, pass an empty string.

  4. You should only very rarely and in very exceptional cases have a destructor.

Putting it all together, we arrive at the following code:

class Player
{
public:
   Player(std::string name, int score)
   : name_(std::move(name))
   , score_(score)
   { }

   Player()
   : Player("", 0)
   { }

protected:
   std::string name_;
   int         score_;
};
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Thank you, helpful feedback. Do you think you can explain point 2 though? –  Sep 24 '12 at 20:52
  • 1
    @Kyryx: The point is that you should be able to say both `Player(str, 1)`, which makes a copy, and `Player(std::string("Hello"), 2)`, which can *move* the resource without an additional allocation. Passing to the constructor by value and moving allows you to permit both cases with maximal efficiency. – Kerrek SB Sep 24 '12 at 20:53
  • Good catch there with the string object receiving 0. On Mac OS X, `std::string str(0);` crashes immediately. – bobobobo Jun 15 '13 at 16:26