Initialised class member objects

C

claire.bell1

Hi,
Im having problems initialising class member objects in the class's
constructor

e.g. My program isnt about monkeys, but there is too much code to post here.

class monkey
{
private:
int age;
public:
monkey(int);
};
monkey::monkey(int a)
{
age = a;
}
ok class monkey is pretty irrelevant, it will be a member object for person

class person{
private:
monkey ownedMonkey;
public:
person(int)
};

person::person(int monkeyAge)
{
ownedMonkey(monkeyAge);
}

basically im not sure how to initialise the owned monkey in the person's
constructor. When I try to compile my code it says there is no constructor
for monkey(void), even though im using a value.

Anyone know how to correctly initialise member objects?

Thanks,
Vipa
 
V

Victor Bazarov

claire.bell1 said:
Im having problems initialising class member objects in the class's
constructor

e.g. My program isnt about monkeys, but there is too much code to post here.

class monkey
{
private:
int age;
public:
monkey(int);
};
monkey::monkey(int a)
{
age = a;
}
ok class monkey is pretty irrelevant, it will be a member object for person

class person{
private:
monkey ownedMonkey;
public:
person(int)
};

person::person(int monkeyAge)
{
ownedMonkey(monkeyAge);
}

basically im not sure how to initialise the owned monkey in the person's
constructor. When I try to compile my code it says there is no constructor
for monkey(void), even though im using a value.

Anyone know how to correctly initialise member objects?

You better believe it! And soon you will be one of those who know.

Inintialiser lists exist specifically for initialising base classes
and members. You need to read up on those, but for now I will just
give you an example using your classes:

class monkey
{
int age;
public:
monkey(int a);
};

monkey::monkey(int a) : age(a) // initialise, not assign
{
}

class person
{
monkey ownedMonkey;
public:
person(int monkey_age)
};

person::person(int monkey_age) : ownedMonkey(monkey_age)
{
}

BTW, those things must be described in FAQ section 10 (IIRC).

Victor
 
J

John Harrison

claire.bell1 said:
Hi,
Im having problems initialising class member objects in the class's
constructor

e.g. My program isnt about monkeys, but there is too much code to post here.

Shame.


class monkey
{
private:
int age;
public:
monkey(int);
};
monkey::monkey(int a)
{
age = a;
}
ok class monkey is pretty irrelevant, it will be a member object for person

class person{
private:
monkey ownedMonkey;
public:
person(int)
};

person::person(int monkeyAge)
{
ownedMonkey(monkeyAge);
}

basically im not sure how to initialise the owned monkey in the person's
constructor. When I try to compile my code it says there is no constructor
for monkey(void), even though im using a value.

Anyone know how to correctly initialise member objects?

Thanks,
Vipa

Which book are you reading which doesn't mention initialiser lists?

person::person(int monkeyAge) : ownedMonkey(monkeyAge)
{
}

It surprises me how many newbies ask this question. Surely every book on C++
must talk about initialiser lists?

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top