Initialization of class members

This is a discussion on Initialization of class members within the C++ Programming forums, part of the General Programming Boards category; When generating a new instance of a class, do the constructors of all private members are called automatically? And what ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    49

    Initialization of class members

    When generating a new instance of a class, do the constructors of all private members are called automatically? And what about the case I want to call a non default constructor?

    Code:
    class Something
    {
    private:
        YOYO yoyo; //default constructor called automatically?
        YOYO yoyo2(int num); //Possible? if not, what's the right way?
    };
    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,725
    When generating a new instance of a class, do the constructors of all private members are called automatically?
    Generally, the default constructors of member variables are called.

    And what about the case I want to call a non default constructor?
    You would provide an initialisation list, e.g.,
    Code:
    class Something
    {
    public:
        Something() : yoyo(123) {}
    private:
        YOYO yoyo;
    };
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    49
    Thank you

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Don't worry. The default constructors of all variables within your regardless of type and visibility is always called.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning class and struct members
    By simone.marras in forum C++ Programming
    Replies: 17
    Last Post: 03-16-2009, 11:10 AM
  2. A question about class members and constructors
    By Megidolaon in forum C++ Programming
    Replies: 5
    Last Post: 01-30-2009, 02:01 PM
  3. Bool initialization in class
    By FlyingIsFun1217 in forum C++ Programming
    Replies: 10
    Last Post: 08-05-2008, 02:33 AM
  4. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 10:33 AM
  5. Inheiritance and derived classes
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2001, 02:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21