kadina
kadina

Reputation: 5366

Does copy constructors have return value in C++

I understood the concept of copy constructor. Till now I was thinking that copy constructors won't have return values. But I saw some code on internet regarding smart pointers, which defines copy constructor as returning some value. Is it correct? Does Copy constructors in C++ have return values?

Upvotes: 1

Views: 161

Answers (2)

Constructors (in general) do not have a return type, nor do they return a value:

No return type (not even void) shall be specified for a constructor. A return statement in the body of a constructor shall not specify a return value.

§12.1 [class.ctor]

You may be thinking of the copy assignment operator, which does something different, but related, and looks like:

T & operator = (const T &);

Upvotes: 8

user3941871
user3941871

Reputation:

Constructors do not have return types .But can have access specifiers . If a constructor is attached with a return type ...compiler treats it as a function

Upvotes: 1

Related Questions