Thread: Is calling a default constructor from an argument constructor possible?

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Is calling a default constructor from an argument constructor possible?

    I'm assuming that its possible to call a default constructor of a class from an argument constructor? I hope so, so I wont have to write nearly the same thing in both constructors (as my default constructor definition is kind of long).
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    No it is not possible at this time to call a constructor from a constructor.

    Jim

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by jimblumberg View Post
    No it is not possible at this time to call a constructor from a constructor.

    Jim
    Hmm...I guess a workaround then might be to initialize each class member in its own separate initialize function, that way I would only have to call each init function from each constructor, thus shortening the code immensely.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    You can have a member function that is called by the constructor do the initialization.
    Code:
       class foo
       {
          public:
              foo(){ init_foo();}
          private:
              init_foo() { // Do init here}
       };

    Jim

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by jimblumberg View Post
    You can have a member function that is called by the constructor do the initialization.
    Code:
       class foo
       {
          public:
              foo(){ init_foo();}
          private:
              init_foo() { // Do init here}
       };

    Jim
    Oh, yeah. Good idea.

    Thanks.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Patience, young one. Your feature is already planned for the next standard as we speak.
    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. Calling argument of member function
    By davewang in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2010, 01:51 PM
  2. Calling a function with argument
    By Ali.B in forum C Programming
    Replies: 2
    Last Post: 08-08-2009, 10:58 AM
  3. calling default instead of argument constructor, why?!
    By cppn00b in forum C++ Programming
    Replies: 6
    Last Post: 01-30-2005, 04:24 AM
  4. Calling Default Constructor
    By Davros in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2004, 01:08 AM
  5. Help with default parameter value using ptr-to-functor
    By registering in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2004, 04:21 PM