没有虚函数的多态行为 [英] Polymorphic behavior without virtual functions
问题描述
这个程序的预期输出是什么以及为什么???
#include< iostream>
使用命名空间std;
班级基础
{
公开:
~ base(){cout<< "基地" <<结束;}
};
派生类:公共基础
{
public:
~sources(){cout<< "衍生QUOT; << endl;}
};
int main()
{
const base& r = derived();
(void)r; //抑制关于未使用的var的编译器警告。
}
What is the expected output of this program and why???
#include <iostream>
using namespace std;
class base
{
public:
~base() {cout << "base" << endl;}
};
class derived: public base
{
public:
~derived() {cout << "derived" << endl;}
};
int main()
{
const base &r = derived();
(void) r; // Suppress compiler warning about unused var.
}
推荐答案
" Dave"其中p是*********** @ yahoo.com>写了...
"Dave" <be***********@yahoo.com> wrote...
这个程序的预期输出是什么以及为什么???
#include< iostream>
>使用命名空间std;
类基础
{
公共:
~base(){cout<< "基地" << endl;}
};
类派生:公共基地
公共:
〜derived(){cout<< "衍生QUOT; << endl;}
};
int main()
{
const base& r = derived();
(void)r; //抑制关于未使用的var的编译器警告。
}
What is the expected output of this program and why???
#include <iostream>
using namespace std;
class base
{
public:
~base() {cout << "base" << endl;}
};
class derived: public base
{
public:
~derived() {cout << "derived" << endl;}
};
int main()
{
const base &r = derived();
(void) r; // Suppress compiler warning about unused var.
}
输出应该是
派生
base
''main''函数完成后''r'超出范围。
关于你帖子的主题:这里没有多态的
行为。创建了一个临时类型''derived'',并且当一个常量引用(绑定到它的基础
类子对象)不再存在时,后来销毁了
(见12.2 / 5) )。
Victor
The output should be
derived
base
After ''main'' function finishes and ''r'' goes out of scope.
Regarding the subject of your post: there is no polymorphic
behaviour here. A temporary of type ''derived'' is created and
later destroyed when a constant reference (bound to its base
class subobject) ceases to exist (see 12.2/5).
Victor
" Victor Bazarov" <五******** @ comAcast.net>在消息中写道
news:VLmGb.470930
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:VLmGb.470930
275.1368307@attbi_s53 ...
275.1368307@attbi_s53...
" Dave"其中p是*********** @ yahoo.com>写了...
"Dave" <be***********@yahoo.com> wrote...
这个程序的预期输出是什么以及为什么???
#include< iostream>
>使用命名空间std;
类基础
{
公共:
~base(){cout<< "基地" << endl;}
};
类派生:公共基地
公共:
〜derived(){cout<< "衍生QUOT; << endl;}
};
int main()
{
const base& r = derived();
(void)r; //抑制关于未使用的var的编译器警告。
}
What is the expected output of this program and why???
#include <iostream>
using namespace std;
class base
{
public:
~base() {cout << "base" << endl;}
};
class derived: public base
{
public:
~derived() {cout << "derived" << endl;}
};
int main()
{
const base &r = derived();
(void) r; // Suppress compiler warning about unused var.
}
输出应该是
派生的
基础
关于帖子的主题:这里没有多态的行为。当一个常量引用(绑定到它的基础类子对象)不再存在时,会创建一个临时类型''derived'',然后销毁它(见12.2 / 5)。
Victor
The output should be
derived
base
After ''main'' function finishes and ''r'' goes out of scope.
Regarding the subject of your post: there is no polymorphic
behaviour here. A temporary of type ''derived'' is created and
later destroyed when a constant reference (bound to its base
class subobject) ceases to exist (see 12.2/5).
Victor
如果我使用指针而不是参考
,那么该程序会有未定义的行为。我正在努力理解基础语言
机制使这个案例不同,但我不确定是否有一个确实令人满意的答案。答案可能是它只是......
This program would have undefined behavior if I had used a pointer rather
than a reference. I''m trying to understand the underlying language
mechanism that makes this case different, but I''m not sure that there is a
really satisfying answer; the answer may be "it just is"...
这篇关于没有虚函数的多态行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!