tags:

views:

274

answers:

3

I have a c++ class, let's say it's called c, and I want to use the implicit copy constructor in one of the methods, like this:

c c::do_something() {
  c copy = this; //I want implicit copy constructor here!
  copy.something_else(); 
  //........//
  return copy;
}

However, gcc returns this error:

error: invalid conversion from 'c* const' to 'long unsigned int'

(I have another constructor from long unsigned int present)

... as if the copy constructor didn't exist. What am I doing wrong?

+16  A: 

this is a pointer to an object so it should be

c copy = *this;
tliff
ok ... yes, that's it, apparently. thanks :)
This is really using the default constructor and then the assignment operator, *not* the copy constructor. See Matt's response.
Andrew Medico
Andrew: I am not sure - Josh below (and some sites) says the opposite
@Andrew no, it uses the copy constructor.
Andrew Grant
A a = b; will actually never invoke the default ctor + the assignment operator. There are circumstances where more than just the copy ctor is invoked (eg cast operators), but you'll never get default ctor + assignment operator. See copy-initialization in the C++ standard.
Logan Capaldo
+2  A: 

Whoa whoa! Also, don't confuse the copy constructor and the copy assignment operator.

c copy (*this);

is the copy constructor;

c copy = *this;

is the copy assignment operator. Either will work, though the copy constructor is technically more efficient, though the copy assignment operator is often optimized to do the same thing.


Wow - I find myself corrected. Using the = operator in the same statement as the declaration of a variable does indeed call the copy constructor. You learn something new every day!

Matt
No - c copy(*this); is identical to c copy = *this; (provided that *this is of type c) both invoke the copy constructor. c copy; copy = *this; invokes the default constructor and then the assignment operator.
Eclipse
It's not just a matter of efficiency... the copy constructor and assignment operator can be completely different functions.
Andrew Medico
+5  A: 

Quite an aside, but it won't really fit in a comment and there seems to be some disagreement. Try this piece of code out to get an idea of when copy-constructors and assignment operators get called:

class A
{
public:
    A() { cout << "A::A()\n"; }
    A(const A &) { cout << "A::A(const A &)\n"; }
    void operator =(const A &) { cout << "A::operator=(const A &)\n"; }
};

int main()
{
    A a; // default constructor
    A copy = a; // copy constructor (and no assignment operator)
    A copy2(a); // copy constructor (and no assignment operator)
    a = copy; // assignment operator
    A function(); // declares a function return A
    A b = A(); // default constructor (but no assignment operator)
    A b2 = A(a); // copy constructor (but no assignment operator)
}
Eclipse
For completeness it might be an idea to add a constructor "A(int)" and then show that "A a = 10;" is equivalent to: "A a(A(10))".
Richard Corden