Reputation: 2635
I have a class Piece
which I've put the default constructor has private
, because I want to use only a specific constructor when the object is created:
class Piece
{
private:
Piece();
public:
Piece (int var1, int var2);
~Piece();
}
And now I have a class Game
with has a vector of Pieces
:
class Game
{
private:
std::vector<Piece> m_pieces;
public:
Game();
~Game();
CreatePieces(); //<-- only here, I will create the Piece objects, not in the constructor
}
Now I want to have a class Foo
, that contains a Piece
:
class Foo
{
private:
Piece m_piece;//ERROR!!! cannot access private member declared in class 'Piece'
public:
Foo();
~Foo();
}
My question:
Now I need to use the default constructor for the m_piece
on the Foo class
.
But I wanted to avoid that and use as I was using on the Game class
.
Is there anyway I can keep my Piece class
as it is, and create a Piece object
, like in the Foo class
, but initialize it with the constructor Piece (int var1, int var2);
on my Foo()
constructor?
Upvotes: 0
Views: 835
Reputation: 37192
You can initialise m_piece
in the Foo
constructor to call a specific constructor, e.g.:
class Foo
{
Foo() : m_piece(0,0)
{
}
}
Upvotes: 7