Thread: contructors of Class memebers that are classes

  1. #1
    Registered User DeadArchDown's Avatar
    Join Date
    Apr 2002
    Posts
    28

    contructors of Class memebers that are classes

    When is the contructor called for a class thats been declared as a member of another class if you dont implicitly call the contructor? Is it called at all?
    -------------------
    "Exception"

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    be more specific please!

    i didn't get much of that u got an example maybe

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Try it out, this didn't take two minutes to type in:
    Code:
    class Member
    {
    public:
        Member() { cout << "Member class constructor called." << endl; };
    };
    
    class Base
    {
    public:
        Member m_Member;
        Base() { cout << "Base class constructor called." << endl; };
    };
    
    int main()
    {
        Base MyBase;
        return 0;
    }
    This prints out the following on my system:
    Member class constructor called.
    Base class constructor called.

    Looks like the Member class gets constructed first. Think about it a little. If the constructor for the Base class did stuff to the m_Member variable, itself a class, and that m_Member wasn't constructed yet, what would happen? So, the construction of any objects belonging to a class has to occur before the class itself.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    now take it easy again from the begining. u call that constructers
    but i dont see any constructers in your code!

  5. #5
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Code:
    	Member() { cout << "Member class constructor called." <<  endl; };
    
    	Base() { cout << "Base class constructor called." << endl; };
    Those are the constructors.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    maybe the compiler sees the constructor

    correct me again as usual if i'm wrong Dual-Catfish but

    shouldn't a constructor always have a value as argument




    ...yes i believe so

  7. #7
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    shouldn't a constructor always have a value as argument
    No, think of it as a function, if it needs an argument, give it one. If not, then don't.

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    u cant think that way

    and it does need an argument
    otherwise it would'nt be a constructor

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >now take it easy again from the begining. u call that
    >constructers but i dont see any constructers in your code!
    That code relies on the default constructors that do not need to be defined, they're supplied by the compiler for the simple duty of creating objects. For further functionality you would need to define a specialized constructor.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    u cant think that way
    For this particular instance, yes, you can. But you're correct, it's not right to think of constructors as functions.

    and it does need an argument
    otherwise it would'nt be a constructor
    As prelude said, the code is relying on default constructors (What! how can that be? Constructors have to have arguments!)

    Oh well, this is way off topic... if you want to discuss it more PM me.

  11. #11
    Registered User DeadArchDown's Avatar
    Join Date
    Apr 2002
    Posts
    28
    That was my problem...stupidity. I was wondering about it, because I know you can explicity call the constructor...so I was thinking, why would it call it for you if sometimes you had to call it? And the answer, as you guys have awesomely pointed out, is that it only calls the default constructor, and you need to call explicitly if you want another constructor called. Braine freeze on my part...StonedCoder left his mark on me. Anyway, am I right to awesome that call a constructor supresses the default constructor from being called?
    -------------------
    "Exception"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. Array of classes within class definition
    By subdene in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2005, 10:41 AM
  5. Using 2 classes in a class
    By Travis Dane in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2002, 04:34 PM