Quesion on explicit copy constructors.

i wonder why the following code can not be compiled.

// compiler: gcc 3.3.1
#include <iostream>
using namespace std;

class a {
public:
a(){}
explicit a(const a& _a) {}
};

a fun()
{
return a(); // what's happened here?
}

int main(){}


Anyboby can help to rescue? thanks!!!
Never, ever, make a copy constructor explicit, unless you want to jump through hoops to fix the compile errors the above code gives you.

In order to return an "a" from a function, the copy constructor has to be called .... implicitly. There is no
way to return an "a" by explicitly calling the copy constructor. You would have to return either a reference
to a static instance of "a" or a pointer to a dynamically allocated instance of "a".

By declaring the copy constructor explicit, you've told the compiler that it cannot call your copy constructor
implicitly ... ever.
Topic archived. No new replies allowed.