如何通过指针的类指针访问类的成员函数
How can i access member functions of a class via Class pointer of pointer?
下面是我的C++代码,我在下面的代码中注释掉了我的问题:
#include <iostream>
using namespace std;
class Base{
public:
virtual void f1(){
cout << "f1 from basen"; }
virtual void f2(){
cout << "f2 from basen";
}
};
class D1: public Base {
public:
virtual void f1(){
cout << "f1 from D1n";
}
};
class D2: public Base {
public:
virtual void f2(){
cout << "f2 from D2n";
}
};
int main(){
Base* b1 = new D1;
b1->f1();
b1->f2();
delete b1;
Base* b2 = new D2;
b2->f1();
b2->f2();
Base** ptr = &b2;
// here how to use member function
// of b2
return 0;
}
我想通过指针访问f1()
D1
类的功能ptr
。我已经声明了Base**
并为其分配了b2
。其中b2
是Base*
类的指针。
您必须尊重Base**
一次,以便可以通过指针基类调用函数。
一种可能的方法是写
(*ptr)->f1();
相关文章:
- 如何正确编写指针函数声明?
- C++常规指针函数或模板
- 如何重新定义 C++ 指针函数?
- C++ 指向其他类函数的指针函数
- 指针到指针函数参数
- 将指向成员的指针函数传递到模板中
- C++ 在 none 常量指针函数中返回一个常量指针
- 如何使用指针函数编写/读取数组
- 是C 中的函数指针函数对象
- 如何从另一个类调用指向成员的指针函数
- 如何声明采用指向成员的指针函数的函数
- 如何构造一个以可变参数指针函数作为成员的类?
- c 通过值或指针函数语法
- 将typedef方法作为指针函数传递
- 从 Main 中的双指针函数打印出指针数组
- strcpy 对指针函数的引用
- 这是否仍然声明一种指针函数的别名?
- 将指向成员的指针函数与 std::shared_ptr 结合使用
- 'Incomplete type' 为标准::函数声明指向成员的指针函数模板参数时出错
- 从注入进程的 DLL 调用函数并更改指针函数的地址