Passing a parent by reference

I have this code in the library:

class Parent { //some data and functions }; void myfunc(Parent& ref); 

and I want to make this code in my application:

 class Child : public Parent { // some other data and functions void dostuff() { myfunc(*this); } }; 

Is it safe to transmit this? (without slicing, without copying ...) Is it better to call myfunc as follows:

 myfunc( * ((Parent*)this) ) 

Please note that I have no control over what happens inside myfunc, in some cases I don’t even know what happens inside.

I used pass-parent-by-pointer many times and I got used to it, but never used pass-parent-by-reference before.

+6
source share
2 answers

myfunc(*this) is fine as long as it is announced that myfunc accepts the link - what it is.

This will not copy the object. It will pass a link to the source object. In addition, it will not cut the object. The link will be of type Base& , but the object to which it refers will not change.

Just to let you know that if you then wrote polymorphic (e.g. virtual ) methods on this Base& , polymorphism will still work correctly and do what you expect - just as if you were calling through a pointer. In other words:

 Base& b = *derived; b.SomeVirtualFoo(); 

... will have the same effect as:

 Base* b = derived; b->SomeVirtualFoo(); 
+10
source

No first version:

  myfunc(*this); 

The second version will probably work in this case, but I'm not sure that it will work in all cases if multiple inheritances are involved (since you are using the C-Style cast). I have to pull out my standard to look at the exact behavior of C-Style.

If it worked following a pointer, the same method will work if you convert to using links.

Update:

Now that I have read the standard, I see that C-Cast will do the right thing (since static_cast <> can be used to drop the class hierarchy from child to parent).

5.4 Explicit type conversion (cast notation)

4 Conversions performed using - const_cast (5.2.11),
- static_cast (5.2.9),
- static_cast followed by const_cast,
- reinterpret_cast (5.2.10), or
- reinterpret_cast followed by const_cast,

can be performed using an explicit type conversion letter. The same semantic limitations and possibilities are used, except that when static_cast is executed in the following situations, the conversion is valid even if the base class is not available:

- a pointer to an object of a type of a derived class or an lvalue or rvalue of a type of a derived class can be explicitly converted to a pointer or a reference to a unique type of the base class, respectively.
- a pointer to a member of a type of a derived class can be explicitly converted to a pointer to an element of an unambiguous type of a non-virtual base class;
- a pointer to an object of an unambiguous type of a non-virtual base class, a glvalue of a single-valued type of a non-virtual base class, or a pointer to a member of a unique type of a non-virtual base class can be explicitly converted to a pointer, reference, or pointer to an element of a type of a derived class, respectively.

If a transformation can be interpreted by more than one of the methods listed above, the interpretation that appears in the list is used, even if the result of this interpretation is poorly formed. If the transformation can be interpreted in more than one way as static_cast followed by const_cast, the transformation is poorly formed.

+5
source

Source: https://habr.com/ru/post/902247/


All Articles