- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
<分区>
我尝试实现 Jesse Liberty 和 Tim Keogh 编写的“C++ 编程入门”示例,但实现纯虚函数是编译而不是构建。它给出了错误:未定义对“vtable for circle”的引用
我尝试用变量 itsRadius
替换虚函数 GetItsRadius
以查看它是否可行,但它开始在 switch 语句中给我同样的错误,但对于 Rectangle
和 Square
以及 circle
之前的错误。
代码如下:
#include <iostream>
using namespace std;
enum BOOL { FALSE, TRUE };
class Shape
{
public:
Shape(){}
~Shape(){}
virtual long GetArea() = 0; // error
virtual long GetPerim()= 0;
virtual void Draw() = 0;
private:
};
void Shape::Draw()
{
cout << "Abstract drawing mechanism!\n";
}
class Circle : public Shape
{
public:
Circle(int radius):itsRadius(radius){}
~Circle(){}
long GetArea() { return 3 * itsRadius * itsRadius; }
long GetPerim() { return 9 * itsRadius; }
void Draw();
private:
int itsRadius;
int itsCircumference;
};
class Rectangle : public Shape
{
public:
Rectangle(int len, int width):
itsLength(len), itsWidth(width){}
~Rectangle(){}
long GetArea() { return itsLength * itsWidth; }
long GetPerim() { return 2*itsLength + 2*itsWidth; }
virtual int GetLength() { itsLength; }
virtual int GetWidth() { itsWidth; }
void Draw();
private:
int itsWidth;
int itsLength;
};
void Rectangle::Draw()
{
for (int i = 0; i<itsLength; i++)
{
for (int j = 0; j<itsWidth; j++)
cout << "x ";
cout << "\n";
}
Shape::Draw();
}
class Square : public Rectangle
{
public:
Square(int len);
Square(int len, int width);
~Square(){}
long GetPerim() {return 4 * GetLength();}
};
Square::Square(int len):
Rectangle(len,len)
{}
Square::Square(int len, int width):
Rectangle(len,width)
{
if (GetLength() != GetWidth())
cout << "Error, not a square... a Rectangle??\n";
}
void startof()
{
int choice;
BOOL fQuit = FALSE;
Shape * sp;
while (1)
{
cout << "(1)Circle (2)Rectangle (3)Square (0)Quit: \n";
cin >> choice;
switch (choice)
{
case 1: sp = new Circle(5);
break;
case 2: sp = new Rectangle(4,6);
break;
case 3: sp = new Square (5);
break;
default: fQuit = TRUE;
break;
}
if (fQuit)
break;
sp->Draw();
cout << "\n";
}
}
int main()
{
startof();
}
我对 virtual inheritance 很感兴趣这件事给我带来了神秘感。让我们以 virtual inheritance 为例。 : struct Base { virtual void
虽然手头的问题已解决,但让我有点困惑的是什么数据用于构造类的虚表以及虚表的布局存储在哪里。如果有人可以提供澄清或指出一些可能满足我好奇心的信息,我将不胜感激。 背景 两个独立的 VC6.0 项目:一个
关于我的环境的信息 Mac OS 大苏尔:11.4 VS for Mac:8.10.6(内部版本 10) Xamarin.Android:11.3.0.4 SDK 工具版本:26.1.1 SDK平台工
根据我的理解,VTable 是调用最派生版本的函数所必需的。它将包含有关函数的最派生版本的条目......我的疑问是为什么我们需要为基类提供一个 VTable。因为使用基对象进行调用总是需要调用基类函
我读了这篇文章: https://shaharmike.com/cpp/vtable-part2/ 我不明白为什么在 vtable 中(在文章末尾)我们有这个指针: 0x400918 0x400820
#include #include using namespace std; class Foo { public: virtual void f1() { cou
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关闭 9 年前。 这个问题似乎是题外话,因为它缺乏足够的信息来诊断问题。更详细地描述您的问题或inclu
class Base { public: virtual void function1() {}; virtual void function2() {}; }; class D1:
Variant-like 类型通常通过模拟 Vtables 来实现,参见示例 https://www.youtube.com/watch?v=uii2AfiMA0o 现在,作为替代方案,我们可以简单地
假设我有一个继承自 B 类和 C 类的 A 类(多重继承)。A 类有多少 vtable 成员?单继承是什么情况? 此外,假设: Class A : Public B {} 和: B* test = n
我知道这里有很多关于 vtables 的问题,但我还是有点困惑。 只有当我们有一个指向基类的指针来解析要调用派生类的哪个虚函数时,才使用 vtables 吗? 在我下面的示例中,在第一种情况下,是否在
我有一些关于虚拟析构函数和 vtable 的具体问题。 假设我有以下代码: class Base { public: virtual ~Base(); }; class Child : pub
这个问题在这里已经有了答案: What is a vtable in C++ [duplicate] (3 个答案) Why do we need a virtual table? (5 个答案)
如何在位置无关代码中实现虚函数? 我知道如果我的类有虚函数,编译器通常会为它生成一个 vtable,其中包含所有虚函数的地址,并在我的类的每个对象中存储一个指向 vtable 的指针。 现在,如果我的
网上有很多关于虚表的资源。他们通常对他们有相同的陈述: "只要类本身包含虚函数或重写父类的虚函数,编译器就会为该类构建一个 vtable。这意味着并非所有类都有编译器为它们创建的 vtable。 vt
我对 vtables 的理解是,如果我有一个带有虚函数 speak() 的类 Cat,它有子类 Lion 和 HouseCat,则有一个 vtable 将 speak() 映射到每个子类的正确实现。于
vtable 包含指向那个类的虚函数的指针。它是否也包含指向非虚函数的指针? 谢谢! 最佳答案 这是一个实现细节,但不是。如果一个实现将指向非虚函数的指针放入虚表中,它就不能使用这些指针进行函数调用,
我们有没有virtual table对于 abstract class ? 最佳答案 首先,vtables 的使用是实现定义的,而不是标准强制要求的。 对于使用 vtable 的实现,答案是:是的,通
你能解释一下编译器是否为一个有虚函数的类生成了 vtable 并且没有使用那个类(对象不是以任何形式创建的)。比如说, class A { public:
我对 C++ 和虚拟继承感到好奇 - 特别是解决 bass 类和子类之间的 vtable 冲突的方式。我不会假装理解它们如何工作的细节,但到目前为止我所看到的是它们是由于该分辨率而使用虚函数造成的一个
我是一名优秀的程序员,十分优秀!