Initializing objects on the fly

Posted by pocoa on Stack Overflow See other posts from Stack Overflow or by pocoa
Published on 2010-04-03T12:21:16Z Indexed on 2010/04/03 12:23 UTC
Read the original article Hit count: 253

I have a vector called players and a class called Player. And what I'm trying to do is to write:

players.push_back(Player(name, Weapon(bullets)));

So I want to be able to create players in a loop. But I see an error message says "no matching function for call Player::Player..."

Then I've changed that to:

Weapon w(bullets);
Player p(name, w);
players.push_back(p);

Here is my Player definition:

class Player {
public:
   Player(string &name, Weapon &weapon);
private
   string name;
   Weapon weapon;
}

I'm just trying to learn what is the difference between these definitions. And is this the right way to pass an object to an object constructor.

Note: These are not my actual class definitions. I'm just trying to learn something about object oriented programming in C++ with coding it. I mean I know that Weapon should be initialized in Player :)

© Stack Overflow or respective owner

Related posts about c++

Related posts about function