Initialised class member objects

Discussion in 'C++' started by claire.bell1, Jul 21, 2003.

  1. claire.bell1

    claire.bell1 Guest

    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
    claire.bell1, Jul 21, 2003
    #1
    1. Advertising

  2. "claire.bell1" <> wrote...
    > 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
    Victor Bazarov, Jul 21, 2003
    #2
    1. Advertising

  3. "claire.bell1" <> wrote in message
    news:reZSa.6835$...
    > 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
    John Harrison, Jul 22, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    4
    Views:
    2,415
    Andy Fish
    Dec 17, 2003
  2. Anon
    Replies:
    39
    Views:
    2,098
    Joona I Palaste
    May 2, 2004
  3. Thomas Hawtin

    static field initialised twice

    Thomas Hawtin, Apr 27, 2006, in forum: Java
    Replies:
    6
    Views:
    628
    Chris Smith
    Apr 28, 2006
  4. Joerg Toellner
    Replies:
    1
    Views:
    320
    Jakob Bieling
    Jul 16, 2003
  5. CFF
    Replies:
    5
    Views:
    583
    Michael Chisholm
    Sep 1, 2004
Loading...

Share This Page