In C++ tutorials I've noticed that functions who take as a parameter classes interchange between using the actual object (dog
) and a reference to the object (&dog
). Yet, both approaches appear to be working just fine. Is there any difference? And if there isn't, what approach is generally considered best?
class Dog {
public:
void printDog() {
std::cout << "DOG, \n";
}
};
void executePrint(Dog dog) {
dog.printDog();
}
void executePrint2(Dog &dog) {
dog.printDog();
}
int main() {
Dog dog;
executePrint(dog);
executePrint2(dog);
return 0;
}