#include <stdio.h>
class abc{
public:
abc *next;
protected:
int flags;
const char * name;
const char * comments;
public:
abc(const char *name, const char *comments, int flags);
virtual ~abc() {
printf("\nReached at virtual ~abc\n");
printf("Returning from at virtual ~abc\n");
}
};
class def: public abc{
public:
def (const char *myname, const char *mycomments,
int myflags): abc(myname, mycomments, myflags)
{
printf("\nreached at def\n");
printf("name=%s; comments=%s\n", myname, mycomments);
printf("Returning from def\n");
}
};
class ghi: public def{
public:
ghi(const char *myname2, const char *mycomments2,
int myflags2): def(myname2, mycomments2, myflags2)
{
printf("\nreached at ghi\n");
printf("name=%s; comments=%s\n", myname2, mycomments2);
printf("Returning from ghi\n");
}
};
class jkl: public def{
public:
jkl(const char *myname2, const char *mycomments2,
int myflags2): def(myname2, mycomments2, myflags2)
{
printf("\nreached at ghi\n");
printf("name=%s; comments=%s\n", myname2, mycomments2);
printf("Returning from ghi\n");
}
};
ghi myVar("myVar", "Testing it", 0);
jkl myVar2("myVar2", "Testing it Again", 0);
abc::abc(const char *name, const char *comments, int flags) : next(0){
printf("\nreached at abc::abc\n");
printf("name=%s; comments=%s\n", name, comments);
printf("Returning from abc:abc\n");
}
int main(void){
printf("\nrunning main function\n");
printf("ending main function\n");
return 0;
}
輸出:C++虛繼承
reached at abc::abc
name=myVar; comments=Testing it
Returning from abc:abc
reached at def
name=myVar; comments=Testing it
Returning from def
reached at ghi
name=myVar; comments=Testing it
Returning from ghi
reached at abc::abc
name=myVar2; comments=Testing it Again
Returning from abc:abc
reached at def
name=myVar2; comments=Testing it Again
Returning from def
reached at ghi
name=myVar2; comments=Testing it Again
Returning from ghi
running main function
ending main function
Reached at virtual ~abc
Returning from at virtual ~abc
Reached at virtual ~abc
Returning from at virtual ~abc
我不明白:
- 又是怎樣的功能虛擬〜ABC叫什麼名字?
- 它爲什麼在程序結束時運行?
- 功能的目的是什麼?
你認爲你在哪裏使用'虛擬'繼承?我的意思是,引用它。 – Yakk
@Yakk虛擬繼承沒有在例子中編碼,但我想知道如何調用虛擬〜abc ..... – Vineet1982
在這個程序中沒有虛擬繼承。你可能想重新考慮你的問題。 –