CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2002
    Posts
    199

    can i call copy constructor in default constructor?

    class A
    {
    public:
    A(){ //A(0);}
    A(int i) { m = i;}
    private:
    int m;
    }
    i think it's illegal, but can anyone tell me why?

  2. #2
    Join Date
    Jun 2003
    Location
    Cochin
    Posts
    364

    Re: can i call copy constructor in default constructor?

    Originally posted by jianmin jin
    class A
    {
    public:
    A(){ //A(0);}
    A(int i) { m = i;}
    private:
    int m;
    }
    i think it's illegal, but can anyone tell me why?
    Why do u think so?
    It's legal.

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: can i call copy constructor in default constructor?

    Originally posted by jianmin jin
    can i call copy constructor in default constructor?
    Well...why would you do that?

  4. #4
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: can i call copy constructor in default constructor?

    Originally posted by jianmin jin
    class A
    {
    public:
    A(){ A(0);}
    A(int i) { m = i;}
    private:
    int m;
    }
    i think it's illegal, but can anyone tell me why?
    Your example doesn't show a copy constructor. The signature of a copy constructor is: A(const A& a)

    If I am not wrong, I believe that you want to call a different constructor within the default constructor, to initialize it's member variable. This cannot be done and you can proof it by using a debugger or simply print out the value of the member variable. This is because the statement A(0) in your default constructor only creates an unnamed temporary instance of A. This instance is immediately destroyed when it leaves the scope of the default constructor. As you may see, it never update the member variable of the original instance.

  5. #5
    Join Date
    Apr 2002
    Posts
    199
    thanks for ur answers, i did it just to prove whether it's right or wrong ,hehe.

  6. #6
    Join Date
    Apr 2002
    Posts
    199

    Re: Re: can i call copy constructor in default constructor?

    Originally posted by Kheun
    Your example doesn't show a copy constructor. The signature of a copy constructor is: A(const A& a)

    If I am not wrong, I believe that you want to call a different constructor within the default constructor.
    yeah, that's what i want to do, so it's illegal ,right? thanks for ur
    good answer

  7. #7
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Originally posted by jianmin jin
    yeah, that's what i want to do, so it's illegal ,right? thanks for ur
    good answer
    Not quite. We can't really say that it is illegal as this code compiles and runs properly. However, in C++, calling the ctr within another ctr serves no purpose in initialization.

    FYI. Other OO programming languages, like Java, allow such initialization which is adding to the confusion.

  8. #8
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    In C++, a better way to share initialization code is to write another function.

    Code:
    class A
    {
    public:
    A(){ init(0);}
    A(int i) { init(i);}
    
    private:
    void init(int a) {m = a;}
    
    int m;
    }

  9. #9
    Join Date
    Sep 2002
    Location
    Belarus - Tirol, Austria
    Posts
    647
    Originally posted by Kheun
    In C++, a better way to share initialization code is to write another function.

    Code:
    class A
    {
    public:
    A(){ init(0);}
    A(int i) { init(i);}
    
    private:
    void init(int a) {m = a;}
    
    int m;
    }
    It's better to use an initialization list, so there is no any need for init() here

    Code:
    class A
    {
    public:
    A() : m(0) {}
    A(int i) : m(i) {}
    
    private:
    int m;
    }
    "UNIX is simple; it just takes a genius to understand its simplicity!"

  10. #10
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Or, in this particular case:
    Code:
    class A
    {
    public:
        A(int i = 0) : m(i) {}
    
    private:
        int m;
    }
    And I'd make the constructor explicit as well.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  11. #11
    Join Date
    Aug 2002
    Posts
    78
    In this trivial case, where only member variables are set, then yes, the initializer list is fine. For non-trivial cases where other work is done, say calculations or initializations of more complex structures or even devices, then an init() is good standard fun.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured