Home > Back-end >  Derived classes zhangs a virtual function and a custom function
Derived classes zhangs a virtual function and a custom function

Time:09-23

#include
using namespace std;
Class A
{
Public:
Virtual void testfunc () const {cout & lt; <"A" & lt; Protected:
Private:
int i;
};
Class B: public A
{
Public:
Void testfunc () const {cout & lt; <"B" & lt; };
Class C: public B
{
Public:
Void testfunc () {cout & lt; <"C1" & lt; Void testfunc () const {cout & lt; <"C" & lt;
};

Int main ()
{
C C;
A. & amp; Pa=c;
C & amp; PC=c;
Pa. Testfunc ();
PC. Testfunc ();
Getchar ();

}

Why PC. Testfunc () output is c1, the layout of the object should be C
,

CodePudding user response:

Void testfunc (const)
Const often function, often function called when the object is often object to,

CodePudding user response:

reference 1st floor u010165006 response:
void testfunc (const)
Const often function, when the object is often object to call functions, often


Often a member function, wrong, should be a function with the same cover, there is no polymorphism first call a virtual function,

CodePudding user response:

Or in front of the wrong, should be overloaded functions of const object default calls a const member function, (often object can only be called member functions)
http://blog.csdn.net/u014630623/article/details/51290954

#include
using namespace std;
Class A
{
Public:
Virtual void testfunc () {cout & lt; <"A" & lt; Protected:
Private:
int i;
};
Class B: public A
{
Public:
Void testfunc () {cout & lt; <"B" & lt; };
Class C: public B
{
Public:
Void testfunc () const {cout & lt; <"C1" & lt; Void testfunc () {cout & lt; <"C" & lt;
};

Int main ()
{
C C;
A. & amp; Pa=c;
C & amp; PC=c;
Pa. Testfunc ();
PC. Testfunc ();
Getchar ();
return 0;
}
Results:
C
C

CodePudding user response:

reference u010165006 reply: 3/f
front or wrong, should be overloaded functions of const object default calls a const member function, (often object can only be called member functions)
http://blog.csdn.net/u014630623/article/details/51290954

#include
using namespace std;
Class A
{
Public:
Virtual void testfunc () {cout & lt; <"A" & lt; Protected:
Private:
int i;
};
Class B: public A
{
Public:
Void testfunc () {cout & lt; <"B" & lt; };
Class C: public B
{
Public:
Void testfunc () const {cout & lt; <"C1" & lt; Void testfunc () {cout & lt; <"C" & lt;
};

Int main ()
{
C C;
A. & amp; Pa=c;
C & amp; PC=c;
Pa. Testfunc ();
PC. Testfunc ();
Getchar ();
return 0;
}
Results:
C
C

I focus on virtual and non - virtual call on and ignored the const, thank you for your answer, now I feel my problem is that some of the problems, I want to ask is a function of VTBL and non - will call the virtual function name repetition, and not only is the same as the same name and argument list that is just a virtual, a non - virtual, such as:
Class A
{
Public:
Virtual void testfunc () {cout & lt; <"A" & lt; Private:
int i;
};
Class B: public A
{
Public:
Void testfunc () {cout & lt; <"B" & lt; };
Class C: public B
{
Public:
Void testfunc () {cout & lt; <"C" & lt; Int testfunc () {cout & lt; <"C" & lt;
};
But finish found wrong, because is not allowed to override only the return values of different function, that is to say the c + + overloading mechanism has to avoid a situation where I have described the , if don't want to same name and argument list, add const methods, such as when choosing who calls at this time there will be other additional conditions, such as whether the const object,

CodePudding user response:

Overload resolution is done at compile time
Virtual functions is the runtime


 
Void testfunc () {cout & lt; <"C" & lt; Int testfunc () {cout & lt; <"C" & lt;
This is not overloaded, it will direct compilation errors,

CodePudding user response:

reference 5 floor akirya reply:
overload resolution is done at compile time
Virtual functions is the runtime


 
Void testfunc () {cout & lt; <"C" & lt; Int testfunc () {cout & lt; <"C" & lt;
This is not overloaded, can directly compile error,


What I mean is that the overloading don't allow that to happen

CodePudding user response:

reference 5 floor akirya reply:
overload resolution is done at compile time
Virtual functions is the runtime


 
Void testfunc () {cout & lt; <"C" & lt; Int testfunc () {cout & lt; <"C" & lt;
This is not overloaded, can directly compile error,

And although belongs to the run-time polymorphism mechanism of virtual functions, but the virtual function table (VTBL) is determined at compile time, run time is decided to use which virtual pointer (VPTR),

CodePudding user response:

From the design point of view, it is best not to name repetition,
  • Related