Is it possible to override a function in a C ++ child class without using a virtual keyword for the function of the parent class, which is abstract?

class Parent { public: void func1(); // Complete meaningful definition in parent given. virtual HRESULT func2()=0; // Bcoz of this function Parent class is abstract. }; class Child: public Parent { public: void func1(); // Different definition in child. }; 

Is this possible in C ++? I override func1() , which is NOT virtual and already has a definition in the parent abstract class.

+7
source share
5 answers

[assuming here is Child extends Parent , unlike what code binding shows]

Yes, it is possible [it is called shelter] - but you will not get dynamic distribution behavior.

The static type will determine which method will be called, not the dynamic type.

For example:

 Parent* p = new Child; p->func1(); 

Parent::func1() call
while:

 Child* c = new Child; c->func1(); 

Call Child::func1()

+19
source

No, it’s actually not possible to override the definition in the parent (at least when it comes to C ++, β€œoverriding” is usually reserved specifically for invoking virtual functions). Instead, defining a function with the same name in the child class simply hides the function in the parent that has the same name (that is, in the context of the child, searching for that name will only find the function in the child, and not in the parent).

If you want (and the functions have different signatures), you can also get the functions in both the parent and the child, processed as overloaded, so the call will try to call depending on which matches are better:

 struct parent { void func1(char) {} }; struct child : public parent { void func1(long) { } using parent::func1; }; 

Now you get:

 child c; c.func1('a'); // calls parent::func1 c.func1(123L); // calls child::func1 

This is another third type of behavior, although it differs from having a virtual function or having a function in a child that hides it in the parent.

Using a virtual function, the choice of the called function is based on the dynamic type, so if you have a pointer / reference to the base class, the called function depends on whether it refers to a database object or a derived class.

When you hide a function, the called function is based on a static type, so if you call it with a pointer / reference to the base, it calls the base function, even if it actually refers to an object of the derived class. If, however, you use a pointer or a reference to (or directly use an instance of) the derived class, it will call the function in the derived class.

Using the using statement, you get an overload of functions, so when you call a function (in the context of a derived class), the called function is based on which function signature best matches the parameter (s) you are passing.

+3
source

You can overload it if they have different types of arguments.

You can hide this, in the sense shown here, so that Child::func1 called instead of Parent::func1 in code that knows that it is looking at the child instance. However, as Amit points out, you are not receiving dynamic dispatch.


 struct StaticParent { void foo(); }; struct StaticChild : public StaticParent { void foo(); } StaticChild sc; sc.foo(); // calls StaticChild::foo StaticParent &sp = sc; sp.foo(); // calls StaticParent::foo 

 struct VirtualParent { virtual void foo(); }; struct VirtualChild : public VirtualParent { virtual void foo(); } VirtualChild vc; vc.foo(); // calls VirtualChild::foo VirtualParent &vp = vc; vp.foo(); // calls VirtualChild::foo 
+2
source

Yes, this is valid C ++ syntax and will define Child :: func1, which hides Parent :: func1 in Child.

However, this does not mean that it completely replaces Child :: Parent :: func1. This function can still be explicitly called by the element of the child element.

 Child c; c.Parent::func1(); 

In addition, this feature will not be virtual. So the following.

 Child c; Parent& p = c; p.func1(); 

will call Parent :: func1, not Child :: func1.

0
source

Most have been said.

Your expectations would be correct if you were talking about java. In java, you can exclude any non-closed, non-final method. You may be familiar with a different programming language than C ++

By the way, two things are wrong in your code

  • " class " must be lowercase
  • default access for members of the private class. It is NOT permitted and not useful to redefine private members that are not clean.
0
source

All Articles