English 中文(简体)
C++ 参照类型中的虚拟功能
原标题:C++ virtual function in type casting of reference
  • 时间:2011-11-02 22:55:08
  •  标签:
  • c++

我在虚拟职能和参考方面有问题。 当我试图准备面谈时,这从一片迷惑中解。 我只看一看问题,但看不到确切的情况。

该法典规定:

class A{
public:
 virtual void foo() {cout << "A::foo" << endl;}
};

class B: public A{
public:
 void foo() {cout << "B::foo" << endl;}
};

class C: public B{
public:
 void foo() {cout << "C::foo" << endl;}
};

int main(void){
 C c;

 B *q;
 q = &c; q->foo();
 return 0;
}

我认为产出为B:foo,但答案实际上是C:foo。 谁能告诉我,为什么表胜者选择执行B? 增 编

问题回答

If foo was not virtual, then the function called would be based on the type of the pointer, and B::foo() would be called.

由于“代码>foo在基数类别中的定义是虚拟的,因此在所有衍生类别中仍然是虚拟的。 因此,所要求的功能是以标的类型(at)而不是点名的类型为依据的。 由于标注为<条码>C,因此,名称为<条码>C:foo(<>>>。

B,因为它超越了基类的虚拟功能,尽管并未明确宣布该功能为virtual

The most derived type of the object called through q->foo() is C and C s final overrider for foo with the signature void foo() is C::foo so this is the function that is called.

由于q 各点至C,物体及其虚拟功能表包含一个虚拟功能foo。 认为在某一类别中宣布的任何虚拟功能在任何衍生类别中都是虚拟的。 这是利用基类进入衍生产品的途径。

Afaik:

表格一旦使用,即由<条码> /代码>的<代码>:foo<>/code>的申报所强制使用,则并非你所申报的类型,它总是指最具体方法的方法,即<条码>:foo。

Can someone tell me why the vtable wont choose B s implementation?

这是拥有虚拟功能的整个点,因此,通过基类点人采用压倒一切的方法,仍然将这种方法从衍生的类别中提出来。

q is of type B *, but it is pointing to a C object. So, when you call the virtual function foo() the C::foo() implementation gets called.

尽管foo(>>>未在>/code>和上标出<> / >,但该编码仍是一个虚拟功能,因为在<代码>中已宣布为虚拟功能。 A

If you want B::foo() to get called you ll need to call it explicitly.

q->B::foo();

foo()是一种虚拟功能,这意味着,在“”栏目中定义的版本实际上是q上指明的,而不是指点人类别中确定的版本。

认为成员的职能一旦在级别上几乎某个地方出现,即不需要在B类和C类中明确标明职能,因为A类已经这样做。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?