Cut override method by casting

I have a class B that inherits publicly from A:

class A { private: virtual void method(); } class B : public A { private: void method(); } 

Now I need to somehow call the original A::method() inside B::method() without calling the copy constructor for A.
A is defined in a library that I am trying to extend, so I cannot change this code (for example, to protect a method). Is it possible to somehow hide this ptr inside B::method() and cut off the overridden method ?

I am using an external interface that calls A::method() . This interface correctly calls my overridden B::method() , but I cannot force the interface call inside B::method() not generate a stack overflow.

+5
source share
2 answers

Since the private method cannot be called qualified, and overriding cannot be undone, you cannot call the private method without another object. Casting an object will have no effect, because the way that virtual functions are processed is part of the actual object.

In the past, I advocated the creation of all virtual functions (except the destructor) private , but the need to call the version of the base class is actually not so unusual. Thus, virtual functions should not be private , but rather protected . Of course, if an interface actually makes its functions virtual private , a design error cannot be undone by the user of that interface.

Having seen that answers defending horrible hacks ( #define private protected ) get upvotes, I would rather rather rely on the added non- virtual member functions without changing the layout of the object and editing the header file to add a suitable function:

 class A { private: virtual void method(); protected: void call_method() { this->A::method(); } }; 

This change has a much more local effect, and is also simply not tolerated. However, it simply relies on the fact that the layout of the object does not change by adding a non virtual ( inline ) method and an access specifier. No warnings at the language level or so will be affected.

+4
source

There is no appropriate way to call a private function in a derived class.

At the very least, if there is no friend or member template, you can specialize in your personal type in order to undermine intent by abiding by the letter of the law.

  • However, there is #define private public -hack that might work on your implementation.
    This is an Undefined Behavior, although do not complain that it does not work everywhere. The likely unsuccessful mode is a binding error, although theoretically it can be arbitrarily bizarre.

  • More reliable would be the addition of a non-virtual secure inline-forwarder, although this means actually editing the header.

  • In addition, gcc has -fno-access-control to disable all access checks: https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc/C_002b_002b-Dialect-Options.html#C_002b_002b-Dialect- Options

    Disable access check. This switch is mainly useful for handling errors in access control code.

+3
source

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


All Articles