But let's say that I need a function that returns the value of Animal, which really is a Dog.
- Do I understand correctly that the closest I can get is a link?
Yes you are right. But I think the problem is not that you don’t understand the links, but that you don’t understand the different types of variables in C ++ or how new works in C ++. In C ++, variables can be primitive data (int, float, double, etc.), an Object, or a pointer / reference to a primitive and / or object. In Java, variables can only be a primitive or an object reference.
In C ++, when you declare a variable, the actual memory is allocated and associated with the variable. In Java, you must explicitly create objects using new ones and explicitly assign the new object to a variable. The key point here is that in C ++, the object and variable that you use for access is not the same thing when the variable is a pointer or reference. Animal a; means something other than Animal *a; , which means something different from Animal &a; . None of them have compatible types, and they are not interchangeable.
On input, Animal a1 in C ++. A new Animal object is created. So, when you type Animal a2 = a1; , you get two variables ( a1 and a2 ) and two Animal objects elsewhere in memory. Both objects have the same value, but you can change their values independently if you want. In Java, if you type the exact same code, you will get two variables, but only one object. Until you reassign any of the variables, they will always have the same value.
- In addition, he must use one that uses the rFunc interface to see that the returned link assigns Animal &? (Or else deliberately assign a reference to the Animal, which throws out polymorphism by slicing.)
When you use references and pointers, you can access the value of an object without copying it to where you want to use it. This allows you to modify it from outside the curly braces where you declared the existence of the object. References are usually used as function parameters or to return members of the private data of an object without a new copy. As a rule, when you receive a link, you do not assign it to anything. Using your example, instead of assigning the link returned to the rFunc() variable, you should usually enter rFunc().makeSound(); .
So, yes, the user rFunc() must, if they assign a return value to something, assign it a link. You can understand why. If you assign the link returned by rFunc() variable declared as Animal animal_variable , you get one Animal variable, one Animal object and one Dog object. The Animal object associated with animal_variable is, as far as possible, a copy of the Dog object that was returned by reference from rFunc() . But you cannot get polymorphic behavior from animal_variable , because this variable is not related to the Dog object. The Dog object that was returned by the link still exists because you created it with new , but it is no longer available - it leaked.
- How should I return a link to a newly created object without doing the stupid thing I did above in rFunc? (At least I heard it was stupid.)
The problem is that you can create an object in three ways.
{
Everything new in C ++ creates objects in memory where they will not be destroyed until you say so. But in order to destroy it, you must remember where it was created in memory. You do this by creating a pointer variable, Animal *AnimalPointer; , and assigning it the pointer returned by new Animal() , AnimalPointer = new Animal(); . To destroy an Animal object, when you are done with it, you must enter delete AnimalPointer; .