相关疑难解决方法(0)

使用c ++ typedef/using类型别名

我正在阅读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_arrayfor循环?

谢谢

c++ typedef c++11

14
推荐指数
2
解决办法
9620
查看次数

指向成员函数的指针执行虚拟调度?

最近我尝试运行以下代码。

#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++ 标准中定义的位置(我搜索过但找不到任何东西)?

c++ virtual-functions function-pointers member-pointers

6
推荐指数
1
解决办法
81
查看次数