Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 有没有办法放入一个未定义大小的数组并作为静态数组继承?_C++_Arrays_Inheritance_Struct - Fatal编程技术网

C++ 有没有办法放入一个未定义大小的数组并作为静态数组继承?

C++ 有没有办法放入一个未定义大小的数组并作为静态数组继承?,c++,arrays,inheritance,struct,C++,Arrays,Inheritance,Struct,我想知道是否有一种方法可以让类/结构中的数组没有精确的设置值,因此当它被继承时,它可能会有一个与其他结构不同的值。在本例中: structfoo { 虚拟智力测验[]; }; 结构栏:foo { int检验[3]; }; 结构条:foo { 智力测验[5] }; 在本例中,我尝试在foo类中定义一个新的int数组,其中包含一个虚拟关键字。正如您所看到的,bar的测试数组大小为3,但在bar中,测试数组大小为5。但是,当我尝试将foo继承到bar或bar时,它失败并给出一个错误:无效的基类 编辑

我想知道是否有一种方法可以让类/结构中的数组没有精确的设置值,因此当它被继承时,它可能会有一个与其他结构不同的值。在本例中:

structfoo
{
虚拟智力测验[];
};
结构栏:foo
{
int检验[3];
};
结构条:foo
{
智力测验[5]
};
在本例中,我尝试在
foo
类中定义一个新的int数组,其中包含一个虚拟关键字。正如您所看到的,
bar
的测试数组大小为3,但在
bar
中,测试数组大小为5。但是,当我尝试将
foo
继承到
bar
bar
时,它失败并给出一个错误:
无效的基类


编辑:很抱歉,我的帖子不太清楚。我试图解释一个大小未知的数组,但不知怎么的,结果却出现了一些更令人困惑的问题。另外,对于将要说使用
std::vector
的人,请重新阅读我上面的编辑

是否有解决方法?更好的是,还有什么比允许从继承的动态数组变成新的静态数组的数组更有效的吗


在基类中使用std::vector。。。并使其受到保护。

不确定您试图实现的目标。无论如何,你可以使用

struct foo
{
    foo(int* t) : test(t) {}
    int* test;
};
struct bar : foo
{
    bar() : foo(derived_test) {}

    int derived_test[3];
};
不过,使用
std::vector
会更好

struct foo
{
    foo(size_t size) : test(size) {}
    std::vector<int> test;
};
struct bar : foo
{
    bar() : foo(3) {}
};
structfoo
{
foo(大小):test(大小){
病媒试验;
};
结构栏:foo
{
bar():foo(3){}
};

虚拟整数测试[]不是虚拟函数