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?

Recommended Answers

All 2 Replies

Just do it inside the constructor, that's what it is there for.

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);
    }
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.