Originally Posted by
laserlight
I agree. On the other hand, I have noted that pass by reference is an option. It really depends on the situation.
Yes, but then you would at least attempt one copy of the class and if you keep using just references, the objects would go out of scope. References are useless things in modern C++, they're almost of no use.
Code:
class Register
{
private:
Car* m_pCar;
public:
void RegisterCar(Car* pCar) { m_pCar = pCar; } // OK, no copying whatsoever.
};
Code:
class Register
{
private:
Car& m_rCar; // Will require initialization in constructor.
};
But this is a register, right? So we should be able to add objects and remove them at will, making this useless.
Code:
class Register
{
private:
Car m_Car; // Oops, constructor will be called
public:
void RegisterCar(Car& rCar) { m_Car = rCar; } // Oops, copy constructor will be called
};
Not to mention that any resources used by m_Car will be allocated and you may have to overlord or specify your own copy constructor to release any resources that Car might use before copying the new object which will probably require memory copy.
Code:
class Register
{
private:
Car* m_pCar;
public:
void RegisterCar(Car& rCar) { m_pCar = &rCar; } // BAD!
};
Useless, since references are just like pointers - compiler will pass the address to the object and it adds more complexity, having to do &rCar too, since we can't put a reference member unless it's initialized.