c++ - 是否可以将成员作为指针继承?
问题描述
我想是否可以将成员作为指针继承。
struct base
{
int x, y;
};
struct derived : public base
{
// inherit x and y as int pointers
}
只要行为相似,它不必看起来像上面的示例。
我想将它用于 SOA(Structure of Arrays) 。
伪代码:
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)
{
}
}
推荐阅读
- javascript - 如何使用香草Javascript更改滚动标题的背景颜色
- sql - 如何在sql server中连接两个具有不同列但行数相同的选择查询的结果?
- python - 当 argparse 中未设置 optinal 时如何采用两个位置参数
- swift - 滑动手势与移动 UISlider nob 混淆?
- powershell - Powershell - 变量问题
- reactjs - 如何通过 <b>{qNumber} 向组件显示整数值。下面的代码
- python - 参数“never_split”不适用于 bert 分词器
- javascript - 如何使用标签 ol 和 strong 正确解析 html
- python - 收到错误“sqlite3.InterfaceError:错误绑定参数 0 - 可能是不受支持的类型。” 使用 sqlite3 python 将数据插入表时
- javascript - 服务的角度排序结果