是否可以将成员作为指针继承
Is it possible to inherit members as pointers?
我想知道是否可以将成员作为指针继承。
struct base
{
int x, y;
};
struct derived : public base
{
// inherit x and y as int pointers
}
它不必看起来像上面的例子,只要行为相似即可。
我想把它用于SOA(数组结构(。
伪代码:
template<size, return_type, ...traits>
struct Soa<size, return_type, traits...> {
Soa<size, return_type, traits...>()
{
/*
buld the rdata as something like this
for(int i = 0; i < size; i++) {
rdata[i] = {&get<I>(soa_data)[i]...};
}
*/
}
second_type : public return_type
{
// inherit members as pointers
}
return_type &operator[](int i)
{
return (return_type &)rdata[i];
}
tuple<traits[size]...> soa_data;
second_type rdata[size];
};
// sample use of the Soa
struct base {
int x, y;
};
Soa<100, base, int, int> soa; // where the two ints correspond to the members of base
base &b = soa[i];
或者也许我对这个问题的看法不对?
无法更改继承变量的类型。但是没有什么可以阻止您创建指向父变量的新变量。
struct base
{
int x, y;
};
struct derived : public base
{
int *px, *py;
derived() : px(&x), py(&y)
{
}
}
是否可以将成员作为指针继承?
否。
派生类有一个子对象,它的成员与基类如果不是子对象时的成员完全相同。没有办法影响你从基地获得的成员类型。
相关文章:
- 关于C++中具有多重继承"this"指针的说明
- 共享指针继承,而不先显式强制转换
- 派生类是从基类继承 v 指针并仅使用它,还是也有自己的 v 指针?
- 使用智能指针附加的继承对象的深层复制
- 嵌套类、继承和C++中的共享指针
- 删除C++中的继承类指针数组
- 具有虚拟指针和继承的 CPP 类大小
- 钻石继承虚拟成员铸造与指针
- 当键是虚拟继承中涉及的基类指针时,对 std::unordered_map 项的访问崩溃
- 当依赖关系和依赖关系都是多态时,在哪个继承级别存储依赖关系指针?
- cppyy 继承包含智能指针的类
- 作为模板参数的成员函数指针在继承的成员函数上失败,如何以及为什么?
- 是否可以将成员作为指针继承
- C++14 不能调用从唯一指针继承的类的复制构造函数或运算符 =
- 是无限的Typedef函数指针继承
- 是否可以从 C++11 智能指针继承并覆盖相关运算符?
- 继承:使用从父指针继承的方法
- 使用智能指针继承的皮条
- 是覆盖返回类型所需的指针-继承
- 调用未从BaseClass指针继承的DerivedClass函数