如何在继承的类成员函数中访问基类成员的地址
How to access the address of base class member in inherited class member functions?
我最近进入了c++中类、继承和模板的整个世界。但我被困住了。请给我一个解决这个问题的方法。
#include <iostream>
using namespace std;
template <typename type>
class a
{
protected:
type *b;
};
template <typename type>
class p : public a<type>
{
public:
void f()
{
type **q = &a<type>::b;
cout << *q << endl; // some other code in reality related to (*q)
}
};
int main()
{
p<int> obj;
obj.f();
return 0;
}
但结果是失败的:
x.cpp: In instantiation of ‘void p<type>::f() [with type = int]’:
x.cpp:26:9: required from here
x.cpp:9:9: error: ‘int* a<int>::b’ is protected
type *b;
^
x.cpp:18:16: error: within this context
type **q = &a<type>::b;
^
x.cpp:18:26: error: cannot convert ‘int* a<int>::*’ to ‘int**’ in initialization
type **q = &a<type>::b;
^
我将type **q = &a<type>::b;
转换为type* a<type>::* q = &a<type>::b;
。然后我得到了一个额外的错误:
x.cpp: In instantiation of ‘void p<type>::f() [with type = int]’:
x.cpp:26:9: required from here
x.cpp:9:9: error: ‘int* a<int>::b’ is protected
type *b;
^
x.cpp:18:26: error: within this context
type* a<type>::* q = &a<type>::b;
^
x.cpp:19:13: error: invalid use of unary ‘*’ on pointer to member
cout << *q;
^
因此,我将b
从protected:
转换为class a
的public:
成员。但这也给了我一个错误:
x.cpp: In instantiation of ‘void p<type>::f() [with type = int]’:
x.cpp:26:9: required from here
x.cpp:19:13: error: invalid use of unary ‘*’ on pointer to member
cout << *q;
^
现在我不能执行进一步的修改。我很想知道原始代码是否篡改了类被保护的特征
您仍然可以使用protected: type *b;
,如果您在代码中更改以下行:
type **q = &a<type>::b; // `protected: b` is not accessible in this context
type **q = &(this->b); // we make sure that `protected: b` is accessed here
在这种情况下,您实际上将b
视为继承的protected
成员。
为什么使用this
访问基类?
reference:在模板化的派生类中,为什么需要用"this->"来限定基类成员名?在成员函数内部?
另一种方式
最初由@Quentin链接,下面的帖子说明了简单指针和指向成员的指针的区别:
Pointer-to-member困惑
所以,在你最初的问题中,实际上你试图通过以下语法获得一个指向成员变量的指针:
&a<type>::b ==> int* a<int>::*
你可能想要&(a<type>::b)
,这将导致简单的int*
。
这是一个笔记本上的例子,展示了在正确的地方放括号的好处!: -)
相关文章:
- 这是关于成员访问规则的正确摘要吗
- 为什么我在空指针错误(链表)中获取成员访问权限
- 成员访问是否在空指针上定义C++?
- C++ IDE 不会推断/自动完成对模板类中的 std::array 下标表达式的成员访问
- 为什么类成员数据必须是静态的才能被模板化类的模板化结构成员访问
- 为什么c++允许成员函数定义中实例的私有成员访问
- C/C++ 包含点的宏参数(成员访问运算符)
- 访问说明符(私有/公共/受保护)如何在内部工作(限制成员访问)?
- 如何将超类的受保护成员访问到其派生类. 如果已在派生类中声明了具有相同名称的函数?
- 内部类私有成员访问和封闭的友好性
- 通过 C++ 中的另一个结构成员访问结构
- 具体化 PRVALUES 成员访问的 decltype 行为不正确
- 常量表达式中的静态成员访问
- XVALUE来自类成员访问表达式
- 未经授权的私有类成员访问会产生编译时错误而不是运行时错误?
- 在 c++ 中,为什么 -> 被称为二进制中缀指针成员访问运算符?
- 如何访问模板参数的成员?“成员访问不完整的类型”
- 不明确的可变参数类成员访问
- C 受保护的成员访问
- 将typeID转换为静态成员访问(C )的命名空间