#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は呼ばれているのですか?
- なぜプログラムの最後に実行されていますか?
- 機能の目的は何ですか?
あなたはどこで 'virtual'継承を使っていると思いますか?私はまさにそれを引用することを意味します。 – Yakk
@Yakk仮想継承は例ではコード化されていませんが、virtual〜abcがどのように呼び出されているかを知りたい..... – Vineet1982
このプログラムには仮想継承はありません。あなたはあなたの質問を再考したいかもしれません。 –