Quote:
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.
But this is a register, right? So we should be able to add objects and remove them at will, making this useless.
This is precisely why I asked you: better and more efficient than what?
Notice that I wrote: "one could use pass by (const) reference instead of passing pointers". That is, what I had in mind was argument passing. The use of pass by (const) reference can reduce the need to add additional levels of indirection while also alleviating the need to copy the objects passed.
Still, if you want to bring up pointer versus reference member variables: in your Register and Car example, you have an association that can be changed. In fact, the association is composition, so the Register has ownership of the Car. Consequently, storing a reference is not the right way. On the other hand, if a Register must be associated with the same Car object for its whole lifetime, then a reference member variable would be an option. As I noted, it depends on the situation.
Quote:
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.
Actually, you have to write your own copy constructor, copy assignment operator and destructor since you store a Car* with ownership of the Car object. With a Car member variable instead, the compiler generated copy constructor, copy assignment operator and destructor will work with normal copying semantics. If you do not take ownership of the object, then having a Car member variable is wrong to begin with, so any comparison of efficiency is unfair.
Quote:
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.
Now you have a straw man argument: you are proposing an unusual design, then claiming that references are a Bad Thing.
Quote:
The whole discussion started because I wanted pointers for class members and pointers for argument for the classes instead of passing the arguments by value and storing an extra copy of the argument type in the class
You probably should have said so from the start. Merely stating "it's more efficient for functions to take pointers to them and create the objects on the heap to avoid hugging valueble stack space and to avoid all copying of objects forth and back, wasting precious resources and cpu time" is ambiguous. Of course, one should use smart pointers rather than raw pointers in such a case.
Quote:
The power is what I love of the language - to be able to manipulate raw memory, to use objects and polymorphism to do things with ease. And unfortunately, references just can't live up to that - the more powerful elements of the language.
Polymorphism works with references as well, for your information.