Thread: Initialization of class members

  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
    28,413
    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;
    };
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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, 03: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, 11:33 AM
  5. Inheiritance and derived classes
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2001, 03:50 PM