Observe the following class:
class Object {
public:
Object() {}
Object(const Object & other) = delete;
void setName(const std::string & name) {
this->name = name;
}
void printName() {
std::cout << name << std::endl;
}
private:
std::string name = "Janice";
};
Now observe this code:
int main() {
Object o;
Object p (o);
return 0;
}
I understand that the previous code invokes the implicit copy constructor Object(const Object & other). Thus, if we delete the copy constructor we get an error:
// delete copy constructor
class Object {
public:
Object(const Object & other) = delete;
error: use of deleted function 'Object::Object(const Object&)'
But what if we change p to a reference?
int main() {
Object o;
Object & p (o);
return 0;
}
The code compiles fine without a copy constructor or a warning. What is actually happening when we construct a reference with the parenthesis () syntax?
Is there any difference (neglible or not) between Object & p (o); and Object & p = o;?
Object p; Object &o(p);