I realize the merit in not passing large objects by value in C++ but that still leaves us with a lot of choices. I made following pseudo-code to enumerate every possible way to declare and pass objects other than by-value or const(that I know of). I realize It's best to not declare object using dynamic memory unless there is a reason but I'm still a bit overwhelmed with the possibilities.
ClassType::byReference( ClassType &object ) { … };
ClassType::byPointer ( ClassType *object ) { … };
ClassType tester;
ClassType object;
ClassType& reference = object;
ClassType* pointer = &object;
tester.byReference( object ); // most preferable?
tester.byReference( reference ); // also good?
tester.byReference( *pointer ); // okay?
tester.byPointer ( &object ); // meh?
tester.byPointer ( &reference ); // how bout that?
tester.byPointer ( pointer ); // least preferable?
Which methods are better, those that take pointers to object as arguments or references to objects as arguments? It seem the either one can take either kind of arg so long is it's done with the right syntax. Is there any advantage or dis-advantage to any of these ways?
Aucun commentaire:
Enregistrer un commentaire