Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.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+中的派生类中分配数据成员数组+;?_C++_Arrays_Inheritance - Fatal编程技术网

C++ 如何在C+中的派生类中分配数据成员数组+;?

C++ 如何在C+中的派生类中分配数据成员数组+;?,c++,arrays,inheritance,C++,Arrays,Inheritance,我试图创建一个基类,其中包含派生类应该设置的数组。该应用程序是一个俄罗斯方块游戏,因此我希望有一个BaseShape类,它包含一个旋转数组,每个派生类(即SquareShape、TShape等)将根据特定形状的旋转设置 class BaseShape { public: BaseShape(); BaseShape(int my_rot[4][5][5]); ~BaseShape(); int rot[4][5][5]; int i_rot; };

我试图创建一个基类,其中包含派生类应该设置的数组。该应用程序是一个俄罗斯方块游戏,因此我希望有一个BaseShape类,它包含一个旋转数组,每个派生类(即SquareShape、TShape等)将根据特定形状的旋转设置

class BaseShape
{
  public:
    BaseShape();
    BaseShape(int my_rot[4][5][5]);
    ~BaseShape();

    int rot[4][5][5];
    int i_rot;
};

class SquareShape : public BaseShape
{
  public:
    SquareShape();
    ~SquareShape();
};
我想为SquareShape类中的rot成员设置值。我试过几种方法,比如在派生类的构造函数中分配它(然后知道不能分配数组),在SquareShape初始值设定项列表中设置它(然后知道不能在派生类的初始值设定项中初始化基类的成员),将数组传递给基类构造函数,并将成员初始化为参数,该参数无法编译,错误为:

error: invalid conversion from ‘int (*)[5][5]’ to ‘int’ [-fpermissive]
   rot{my_rot}
所以我想知道是否真的有一种方法来实现这一点,或者是否有一种更简单的方法来实现这一点

然后了解到无法在派生类的初始值设定项中初始化基类的成员

。。。严格地说,这是正确的,但是您可以调用适当的构造函数,而不是直接初始化类成员:

#include <iostream>    

struct Base { 
    int x;
    Base(int a) : x(a) {}
};

struct Foo : Base {
    Foo(int a) : Base(a) {}
};
int main() {
    Foo f(1);
    std::cout << f.x << "\n";
}
#包括
结构基{
int x;
基(inta):x(a){}
};
结构Foo:Base{
Foo(inta):基(a){}
};
int main(){
Foo f(1),;
标准::cout
然后了解到无法在派生类的初始值设定项中初始化基类的成员

…严格地说,这是正确的,但是您可以调用适当的构造函数,而不是直接初始化类成员:

#include <iostream>    

struct Base { 
    int x;
    Base(int a) : x(a) {}
};

struct Foo : Base {
    Foo(int a) : Base(a) {}
};
int main() {
    Foo f(1);
    std::cout << f.x << "\n";
}
#包括
结构基{
int x;
基(inta):x(a){}
};
结构Foo:Base{
Foo(inta):基(a){}
};
int main(){
Foo f(1),;

std::coutC-array不可复制,您可以使用
std::array

using Rot = std::array<std::array<std::array<int, 5>, 5>, 4>;


class BaseShape
{
  public:
    BaseShape();
    BaseShape(Rot my_rot) : rot(my_rot) {}
    ~BaseShape();

    Rot rot;
    int i_rot = 0;
};
使用Rot=std::数组;
类基本形状
{
公众:
BaseShape();
BaseShape(Rot my_Rot):Rot(my_Rot){}
~BaseShape();
腐烂;
int i_rot=0;
};

C-array不可复制,您可以使用
std::array

using Rot = std::array<std::array<std::array<int, 5>, 5>, 4>;


class BaseShape
{
  public:
    BaseShape();
    BaseShape(Rot my_rot) : rot(my_rot) {}
    ~BaseShape();

    Rot rot;
    int i_rot = 0;
};
使用Rot=std::数组;
类基本形状
{
公众:
BaseShape();
BaseShape(Rot my_Rot):Rot(my_Rot){}
~BaseShape();
腐烂;
int i_rot=0;
};

C-array不可复制,您可以使用
std::array
代替。C-array不可复制,您可以使用
std::array
代替。