Copy Constructors

This is a discussion on Copy Constructors within the C++ Programming forums, part of the General Programming Boards category; Code: class Expression; Expression func() { Expression x(3); return x; } Expression func2() { return func(); } I can't define ...

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493

    Copy Constructors

    Code:
    class Expression;
    
    Expression func() {
      Expression x(3);
      return x;
    }
    
    Expression func2() {
      return func();
    
    }
    I can't define a copy constructor like this:
    Code:
    Expression::Expression (Expression);
    So how would I correct the program to silence the error messages saying I need one? I already have a copy constructor looking like this:
    Code:
    Expression::Expression (Expression&);

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    You'll need to be more specific about your problem, or post some more useful code.
    p.s. What the alphabet would look like without q and r.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Let me start off with this question, then I'll ask another if I need to...

    Why must copy constructors use references in the arguments?
    Code:
    Expression::Expression(Expression&);
    instead of
    Code:
    Expression::Expression(Expression);
    My compiler tells me that that's an illegal constructor.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Do you know what pass-by-value means? When you don't use a reference, and use this form:

    Expression::Expression(Expression);

    you are passing by value, so the compiler needs to make a copy of the argument for the function. And guess what? To make a copy, it needs to call the copy constructor. So, your copy constructor calls itself, and once again it needs to make a copy of the argument to the function, so it calls itself again, and so on and so on, resulting in a recursive infinite call to itself.
    Last edited by 7stud; 05-11-2003 at 08:31 PM.

  5. #5
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    I read that the default copy constructor MUST use a reference AND the reference must be const:
    Code:
    class X{
      X();
      X(const X&);
    
    };
    Hope that helps
    Nothing more to tell about me...
    Happy day =)

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    gustavosserra,

    A copy constructor is a function just like any other, and you can make it do what you want:

    Code:
    #include <iostream>
    using namespace std;
    
    class MyClass
    {
    public:
    	MyClass(int a)
    	{
    		var = a;
    	}
    	MyClass()
    	{
    		var = 0;
    	}
    	MyClass(MyClass& m)
    	{
    		var = m.var;
    		m.var += 2;
    	}
    	int Get_var(void)
    	{
    		return var;
    	}
    
    private:
    	int var;
    };
    
    
    int main()
    {
    	MyClass a(10);
    	cout<<a.Get_var()<<endl;
    
    	MyClass m = a;
    	
    	cout<<a.Get_var()<<endl;
    	cout<<m.Get_var()<<endl;
    
    	return 0;
    	
    }
    Last edited by 7stud; 05-11-2003 at 11:36 PM.

  7. #7
    Registered User devil@work's Avatar
    Join Date
    Mar 2003
    Posts
    33
    it doesnt need to be const but since it doesnt change anything and just copy something to something it is better to make it const to avoid any error that we make.

  8. #8
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Originally posted by 7stud
    gustavosserra,

    A copy constructor is a function just like any other, and you can make it do what you want:

    Yes, I know. But wont the compiler look for an especific version of the constructor, I mean the version with const. But you are right! Ive made this code for test:

    Code:
    #include <iostream.h>
    class MyClass{
      public:
        MyClass(){
          cout << "Calling the default constructor\n";
        }
        MyClass(MyClass&){
          cout << "Calling the  copy constructor\n";
        }
      
    };
    void function(MyClass parameter){}
    int main(){
    
      MyClass X;
      
      function(X);
      
      cin.get();
    }
    The program outputs the both "cout".
    Nothing more to tell about me...
    Happy day =)

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "but since it doesnt change anything..."

    Mine does.

    gustavosserra,

    "But wont the compiler look for an especific version of the constructor, I mean the version with const."

    You tell me. Does the code in my previous post work?

    The line:

    MyClass m = a;

    calls the copy constructor.
    Last edited by 7stud; 05-13-2003 at 01:17 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy constructors; Best practices (and private)
    By Mario F. in forum C++ Programming
    Replies: 15
    Last Post: 06-23-2006, 05:42 PM
  2. Help with copy constructors
    By nessie in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2005, 08:50 AM
  3. 'Passing by Refrence for Efficiency', Copy Constructors?
    By Zeusbwr in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2004, 08:11 AM
  4. Copy constructors and operator=()
    By filler_bunny in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2003, 08:43 AM
  5. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM

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