I'd like to know if these are basically the same thing.
class ExampleClass {
public:
ExampleClass(Object& newObj) : obj(newObj) {}
private:
Object obj;
}
class ExampleClass2 {
public:
ExampleClass2(Object& newObj) : objReference(newObj) {}
private:
Object& objReference;
}
So would this not work with either of the classes?
ExampleClass* getExampleObject() {
Object obj;
return new ExampleClass(obj);
}
ExampleClass2* getExample2Object() {
Object obj;
return new ExampleClass2(obj);
}
void main() {
ExampleClass* ec = getExampleObject();
ExampleClass2* ec2 = getExample2Object();
//Do something involving the member objects of ec and ec2
}
So are the member objects invalid after both getExampleObject methods? Doesn't the constructor of ExampleClass save a copy of the object referenced in its constructor?