How Virtual Table works in C++

原文:http://www.learncpp.com/cpp-tutorial/125-the-virtual-table/

To implement virtual functions, C++ uses a special form of late binding known as the virtual table. The virtual tableis a lookup table of functions used to resolve function calls in a dynamic/late binding manner. The virtual table sometimes goes by other names, such as “vtable”, “virtual function table”, “virtual method table”, or “dispatch table”.

Because knowing how the virtual table works is not necessary to use virtual functions, this section can be considered optional reading.

The virtual table is actually quite simple, though it’s a little complex to describe in words. First, every class that uses virtual functions (or is derived from a class that uses virtual functions) is given it’s own virtual table. This table is simply a static array that the compiler sets up at compile time. A virtual table contains one entry for each virtual function that can be called by objects of the class. Each entry in this table is simply a function pointer that points to the most-derived function accessible by that class.

Second, the compiler also adds a hidden pointer to the base class, which we will call *__vptr. *__vptr is set (automatically) when a class instance is created so that it points to the virtual table for that class. Unlike the *this pointer, which is actually a function parameter used by the compiler to resolve self-references, *__vptr is a real pointer. Consequently, it makes each class object allocated bigger by the size of one pointer. It also means that *__vptr is inherited by derived classes, which is important.

By now, you’re probably confused as to how these things all fit together, so let’s take a look at a simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Base
{
public:
    virtual void function1() {};
    virtual void function2() {};
};
 
class D1: public Base
{
public:
    virtual void function1() {};
};
 
class D2: public Base
{
public:
    virtual void function2() {};
};

Because there are 3 classes here, the compiler will set up 3 virtual tables: one for Base, one for D1, and one for D2.

The compiler also adds a hidden pointer to the most base class that uses virtual functions. Although the compiler does this automatically, we’ll put it in the next example just to show where it’s added:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Base
{
public:
    FunctionPointer *__vptr;
    virtual void function1() {};
    virtual void function2() {};
};
 
class D1: public Base
{
public:
    virtual void function1() {};
};
 
class D2: public Base
{
public:
    virtual void function2() {};
};

When a class object is created, *__vptr is set to point to the virtual table for that class. For example, when a object of type Base is created, *__vptr is set to point to the virtual table for Base. When objects of type D1 or D2 are constructed, *__vptr is set to point to the virtual table for D1 or D2 respectively.

Now, let’s talk about how these virtual tables are filled out. Because there are only two virtual functions here, each virtual table will have two entries (one for function1(), and one for function2()). Remember that when these virtual tables are filled out, each entry is filled out with the most-derived function an object of that class type can call.

Base’s virtual table is simple. An object of type Base can only access the members of Base. Base has no access to D1 or D2 functions. Consequently, the entry for function1 points to Base::function1(), and the entry for function2 points to Base::function2().

D1′s virtual table is slightly more complex. An object of type D1 can access members of both D1 and Base. However, D1 has overridden function1(), making D1::function1() more derived than Base::function1(). Consequently, the entry for function1 points to D1::function1(). D1 hasn’t overridden function2(), so the entry for function2 will point to Base::function2().

D2′s virtual table is similar to D1, except the entry for function1 points to Base::function1(), and the entry for function2 points to D2::function2().

Here’s a picture of this graphically:

Although this diagram is kind of crazy looking, it’s really quite simple: the *__vptr in each class points to the virtual table for that class. The entries in the virtual table point to the most-derived version of the function objects of that class are allowed to call.

So consider what happens when we create an object of type D1:

1
2
3
4
int main()
{
    D1 cClass;
}

Because cClass is a D1 object, cClass has it’s *__vptr set to the D1 virtual table.

Now, let’s set a base pointer to D1:

1
2
3
4
5
int main()
{
    D1 cClass;
    Base *pClass = &cClass;
}

Note that because pClass is a base pointer, it only points to the Base portion of cClass. However, also note that *__vptr is in the Base portion of the class, so pClass has access to this pointer. Finally, note that pClass->__vptr points to the D1 virtual table! Consequently, even though pClass is of type Base, it still has access to D1′s virtual table.

So what happens when we try to call pClass->function1()?

1
2
3
4
5
6
int main()
{
    D1 cClass;
    Base *pClass = &cClass;
    pClass->function1();
}

First, the program recognizes that function1() is a virtual function. Second, uses pClass->__vptr to get to D1′s virtual table. Third, it looks up which version of function1() to call in D1′s virtual table. This has been set to D1::function1(). Therefore, pClass->function1() resolves to D1::function1()!

Now, you might be saying, “But what if Base really pointed to a Base object instead of a D1 object. Would it still call D1::function1()?”. The answer is no.

1
2
3
4
5
6
int main()
{
    Base cClass;
    Base *pClass = &cClass;
    pClass->function1();
}

In this case, when cClass is created, __vptr points to Base’s virtual table, not D1′s virtual table. Consequently, pClass->__vptr will also be pointing to Base’s virtual table. Base’s virtual table entry for function1() points to Base::function1(). Thus, pClass->function1() resolves to Base::function1(), which is the most-derived version of function1() that a Base object should be able to call.

By using these tables, the compiler and program are able to ensure function calls resolve to the appropriate virtual function, even if you’re only using a pointer or reference to a base class!

Calling a virtual function is slower than calling a non-virtual function for a couple of reasons: First, we have to use the *__vptr to get to the appropriate virtual table. Second, we have to index the virtual table to find the correct function to call. Only then can we call the function. As a result, we have to do 3 operations to find the function to call, as opposed to 2 operations for a normal indirect function call, or one operation for a direct function call. However, with modern computers, this added time is usually fairly insignificant.



C++中,`pow` 函数用于计算一个数的幂,即某个基数的指定次方。它定义在 `<cmath>` 头文件中,适用于浮点数、双精度浮点数以及整数类型(在C++11及以后的标准中)。该函数的基本语法如下: ```cpp double pow(double base, double exponent); float pow(float base, float exponent); long double pow(long double base, long double exponent); ``` 从C++11开始,`pow` 还支持整数类型的参数,它们会被自动转换为 `double` 类型进行计算 [^2]。 ### 使用示例 以下是一些使用 `pow` 函数的典型示例: ```cpp #include <iostream> #include <cmath> // 必须包含此头文件以使用 pow 函数 int main() { double base = 2.0; double exponent = 3.0; double result = pow(base, exponent); std::cout << base << " 的 " << exponent << " 次方是 " << result << std::endl; // 使用整数作为参数(C++11 及以后) int intBase = 5; int intExponent = 2; double intResult = pow(intBase, intExponent); std::cout << intBase << " 的 " << intExponent << " 次方是 " << intResult << std::endl; return 0; } ``` 输出结果将是: ``` 2 的 3 次方是 8 5 的 2 次方是 25 ``` 需要注意的是,如果底数是负数且指数是分数,则 `pow` 可能会抛出域错误(domain error),因为这会导致复数结果。例如: ```cpp double result = pow(-2, 0.5); // 尝试计算 -2 的平方根,将导致域错误 ``` 在这种情况下,程序可能会终止或抛出异常,具体取决于运行时的错误处理机制。 ### 注意事项 - `pow` 函数返回的是浮点类型的结果,即使输入是整数。 - 对于大指数的整数幂运算,使用 `pow` 可能会导致精度损失,因为浮点数无法精确表示所有整数。 - 如果需要处理非常大的整数幂运算,建议使用自定义的整数幂函数或使用大整数库。 ### 总结 `pow` 是 C++ 标准库中一个非常实用的数学函数,能够方便地进行幂运算。然而,在使用时应注意其对不同类型的支持情况以及可能出现的精度问题或域错误。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值