| joeface (1) | |||||
|
I can't seem to grasp when to choose which method to populate a class object. Why would I choose one of the following ways of creating a class object and assigning values over the other? (I know this could be with/without pointers/references, just trying to get a handle on the general idea of which is better & why) Method A
Method B
Thanks | |||||
|
|
|||||
| LowestOne (895) | |||
|
The first method does not work. It doesn't make much sense to set variables this way. Better to use the constructor and/or set methods:
| |||
|
|
|||
| helios (10258) | ||
Returning an object, if your compiler isn't very smart, incurs in an extra copy. For potentially large objects this can be a waste of time, so in those cases it's better to pass by reference and modify the object. IINM, C++11 addresses this problem with the introduction of move semantics. I.e. if a temporary object (such as the return value of a function) is assigned to another object that implements a special copy constructor (the move constructor), this other constructor is called instead. | ||
|
Last edited on
|
||