Using constructor in constructor
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Using constructor in constructor

  1. #1
    Join Date
    Dec 2011
    Posts
    10

    Question Using constructor in constructor

    Hello. I am trying to use constructor within constructor in the same class. Is that possible. I have tried something and it shows me a error message:

    error: type ‘mainClass’ is not a direct base of ‘glavna’

    This is the program I tried:
    Code:
    class mainClass
    {
        private:
            int x,y;
        public:
            // constructors
            mainClass(int a, int b) {x=a; b=y;}
            mainClass():glavna(0,0) {}
    
            int getX() {return x;}
            int getY() {return y;}
    };
    Thank you!!!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,431

    Re: Using constructor in constructor

    Quote Originally Posted by depecheSoul View Post
    Hello. I am trying to use constructor within constructor in the same class. Is that possible. I have tried something and it shows me a error message:
    What is "glavna"? It is not declared anywhere in that code you posted.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Dec 2011
    Posts
    10

    Re: Using constructor in constructor

    glavna is mainClass i forgot to translate it, when I copy/paste code in the forum Sorry.

    Code:
    class mainClass
    {
        private:
            int x,y;
        public:
            
            mainClass(int a, int b) {x=a; b=y;}
            mainClass():mainClass(0,0) {}
    
            int getX() {return x;}
            int getY() {return y;}
    };

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,587

    Re: Using constructor in constructor

    This specific case can be handled quite simply by providing default values for the two constructor parameters:

    Code:
    class mainClass
    {
        private:
            int x,y;
        public:
            
            mainClass(int a = 0, int b = 0) {x=a; b=y;}
             int getX() {return x;}
            int getY() {return y;}
    };
    That way you can construct mainClass objects by providing either two or no arguments. You can even pass only one, which then will correspond to a, and b gets the default value of 0.

    However, there are more complex cases that can't be solved that simply, and it is not possible to define a class' constructor in terms of another constructor of the same class. The common solution to this is to factor out the initialization code common to various constructors into a private initialization member function and then call that from the individual constructors.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,294

    Re: Using constructor in constructor

    Quote Originally Posted by Eri523
    However, there are more complex cases that can't be solved that simply, and it is not possible to define a class' constructor in terms of another constructor of the same class.
    It is possible, but you need a compiler on the bleeding edge (e.g., g++ 4.7 supports delegating constructors, but it is not a stable release yet).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,587

    Re: Using constructor in constructor

    Quote Originally Posted by laserlight View Post
    It is possible, but you need a compiler on the bleeding edge (e.g., g++ 4.7 supports delegating constructors, but it is not a stable release yet).
    Fascinating! Didn't know that. Am I right to assume this is a C++11 feature?

    At any rate, thanks for pointing that out.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  7. #7
    Join Date
    Dec 2011
    Posts
    10

    Re: Using constructor in constructor

    Thank you

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center