首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对虚拟表的困惑

对虚拟表的困惑
EN

Stack Overflow用户
提问于 2012-09-21 13:18:17
回答 2查看 174关注 0票数 1

我在面试中被问到这个问题。下面的程序创建了多少个虚拟表,每种情况下的输出是什么。

代码语言:javascript
复制
#include <iostream> 
using namespace std;
class A
{
 virtual void func1()
 {
  cout << "0 " <<endl;
 }

 virtual void func2()
 {
  cout << "1 " <<endl;
 }
};

 class B:Public A
 {
  void func1()
  {
   cout << "2" <<endl;
  }
 };

class C:public B
{
 virtual void func2()
 {
  cout << "3" <<endl;
 }
}

int main()
{
A* objA;

B objB ;
C objC ;

//case1:
objA = &objB;
objA->func1();
//case2:
objA = &objC;
objA->func2();
objA->func1();
return 0;
}

我在B类中感到困惑,它会为B类创建vtable吗?在这种情况下,将调用哪个函数。谁能给我解释一下。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-21 15:07:49

请参阅评论

代码语言:javascript
复制
// I'll pretend you have a #include <stdio.h> here

class A
{
 virtual void func1()
 {
  print 0; // error!!  I'll pretend this was puts("0");
 }

 virtual void func2()
 {
  print1;  // error!!  I'll pretend this was puts("1"); 
 }
};

// there is a virtual table for class A. (for two virtual methods)


 class B:Public A
 {
  void func1()
  {
   print2; // error!!  I'll pretend this was puts("2"); 
  }
 };

// there is a virtual table for class B. (for two virtual methods)


class C:public B
{
 virtual void func2()
 {
  print 3; // error!!  I'll pretend this was puts("3"); 
 }
}

// there is a virtual table for class C. (for two virtual methods)


int main()
{
A objA;
B* objB = new B();

C* objC = new C();

//case1:
objA = &objB;   // error!! left side of type A right side of type B**
objA->func1();  // error!!  operator -> on non pointer

//case2:
objA = &objC;  // error!! left side of type A right side of type B**
objA->func2(); // error!!  operator -> on non pointer
objA->func1(); // error!!  operator -> on non pointer
return 0;
}

// nothing is printed

既然您编辑了操作码,那么这里就是新版本代码的答案。请参阅评论:

代码语言:javascript
复制
#include <iostream> 
using namespace std;
class A
{
 virtual void func1()
 {
  cout << "0 " <<endl; // it's ok, but is the space supposed to be there?
 }

 virtual void func2()
 {
  cout << "1 " <<endl; // it's ok, but is the space supposed to be there?
 }
};
// there is a virtual table for class A. (for two virtual methods)

 class B:Public A  // error!! I'll pretend Public was public (lowercase)
 {
  void func1()
  {
   cout << "2" <<endl; // it's ok, but here there's no space, is that correct?
  }
 };
// there is a virtual table for class B. (for two virtual methods)

class C:public B
{
 virtual void func2()
 {
  cout << "3" <<endl; // it's ok, but here there's no space, is that correct?
 }
}
// there is a virtual table for class C. (for two virtual methods)

int main()
{
A* objA;

B objB ;
C objC ;

//case1:
objA = &objB;
objA->func1(); // outputs (to stdout) a '2' (two) and whatever a 
               // newline is on your system (could be '\r' or '\n' or both
               // or in fact anything your platform defines a newline is)
               // stdout is then flushed.
//case2:
objA = &objC;
objA->func2(); // outputs (to stdout) a '3' (three) and whatever a 
               // newline is on your system (could be '\r' or '\n' or both
               // or in fact anything your platform defines a newline is)
               // stdout is then flushed.

objA->func1(); // outputs (to stdout) a '2' (two) and whatever a 
               // newline is on your system (could be '\r' or '\n' or both
               // or in fact anything your platform defines a newline is)
               // stdout is then flushed.
return 0;
}
// the output is '2' <newline> '3' <newline> '2' <newline> 
// where the actual character(s) for <newline> are platform dependent  
票数 1
EN

Stack Overflow用户

发布于 2012-09-21 13:31:56

类B的实例也是类型A的实例。您的类B调用类A的构造函数,如果B没有从A重写func1(),那么B就有一个虚拟func2()和一个来自A的func1()重写。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12524650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档