我正在阅读C++入门书,完全不理解一行:
using int_array = int[4];
typedef int int_array[4]; // This line
for (int_array *p = ia; p != ia + 3; ++p) {
for (int *q = *p; q != *p + 4; ++q)
cout << *q << ' '; cout << endl;
}
Run Code Online (Sandbox Code Playgroud)
好的typedef
是一样的using
.这是否意味着int[4][4]
现在int
以及如何理解这一点?什么类型int_array
的for
循环?
谢谢
最近我尝试运行以下代码。
#include <iostream>
class Base
{
public:
virtual void func()
{
std::cout<<"Base called"<<std::endl;
}
};
class Derived: public Base
{
public:
virtual void func() override
{
std::cout<<"Derived called"<<std::endl;
}
};
int main()
{
void (Base::*func_ptr)()=&Base::func; //Yes, the syntax is very beautiful.
Base* bptr=new Derived();
(bptr->*func_ptr)();
}
Run Code Online (Sandbox Code Playgroud)
我的预期输出是Base called
. 然而,输出是
Derived called
Run Code Online (Sandbox Code Playgroud)
这让我感到惊讶,因为根据我的理解,func_ptr
应该只能看到Base
成员(因为据我所知,func_ptr
不是通过 访问成员函数_vptr
,而是访问函数地址本身。
我想知道,在这种情况下虚拟调度是如何发生的(如何访问虚拟表),以及这种行为在 C++ 标准中定义的位置(我搜索过但找不到任何东西)?