0

Hi all,

How about this?

In this example, what is the correct way to initialise Secret mission?

class Secret
{
private:
	int data;
public:
	Secret(int _data)
	{
		data = _data;
	}
};


class Mission
{
public:
	Secret mission;

	Mission();
	{
		
	}
};

the only way i know of is using the initialisation list

Mission():mission(22) {};

is this method correct? is there any other method?

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Tom Gunn
0

the only way i know of is using the initialisation list

That is the ideal way, and it is required if the Secret class does not have a default constructor. If the Secret class does have a default constructor you can use assignment in the constructor body:

class Secret
{
private:
    int data;
public:
    Secret(int _data=0)
    {
        data = _data;
    }
};

class Mission
{
public:
    Secret mission;

    Mission()
    {
        mission = Secret(22);
    }
};
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.