有什么方法可以将指针指向孩子范围内的父母(派生)班级
Is there any way to have a pointer to a parent (derived) class inside the scope of the Child?
假设以下内容:
class child : public parent
{
public:
fun1(parent * obj);
//somewhere on the child class:
fun2 ()
{
fun1(this::...POINTER TO THE PARENT....); //how can I do such a thing without having to create an object of parent class?
}
};
我正在寻找类似于"这个"指针的东西,该指针指向当前类的地址。但是,是否有"这个"的东西可以在孩子的班级中引用父母班级?
父级是基类而不是派生类。同样,this
也可以隐式转换为基类类型,因此您可以将其传递。
在您的情况下:
class child : public parent
{
public:
fun1(parent * obj);
//somewhere on the child class:
fun2 ()
{
fun1(this);
}
};
最后,在您显示的具体情况下,您要做的事情没有任何意义。孩子可以直接访问基类的任何受保护或公共成员,因此您无需将指针传递给父母。
喜欢以下内容:
class parent
{
/* Rest of the code here */
protected:
int m_member;
};
class child : public parent
{
public:
int fun1() { m_member = 1; }
};
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- 错误:未在此范围内声明'reverse'
- 并行用于C++17中数组索引范围内的循环
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 不计算一个范围内的完美数
- 错误:"imread"未在此范围内声明
- 我在范围内未声明的错误类有问题
- 如何在cpp中使用地图显示给定日期范围内(在下面的问题中)的费率?
- 我有一个数组,我想输入一个范围,然后找到范围内所有偶数的总和?
- 未在此范围内声明错误 'xy'
- 在C++中使用变量而不是"#define"来指定数组大小是不是一种糟糕的做法?(C错误:在文件范围内
- 命名空间范围内的外部 - GCC vs clang vs msvc
- 如何改进一堆在已知值范围内评估变量的 else-if 条件?
- 如何仅使用 While 循环在给定范围内找到可被 7 整除的计数整数
- Socklen_t未在此范围内声明
- 错误:'[' 之前预期的非限定 id 和错误:'users'未在此范围内声明
- 查找给定范围内最长连续 1 的频率
- "Main"已在当前范围内声明
- c++ 变量在宏的扩展中没有在这个范围内声明
- 有什么方法可以将指针指向孩子范围内的父母(派生)班级