c++ - 复制构造函数不识别继承的成员

标签 c++ inheritance copy-constructor sdl-2

我有两个类:

class entity {
public:
  SDL_Rect pos;
  float x;
  float y;

  std::string spriteFile;
  SDL_Surface * spriteHandle;
  int rePos (int newX, int newY);
  int move (float deltaX, float deltaY, bool check); // Move x & y the specified amounts, also moving pos.x and pos.y for every integer increase of x or y
  int display (SDL_Surface * screenSurface); //Use SDL_BlipSurface to blip the image to the screen
  int loadImage (SDL_PixelFormat * format); //Load the image using the spriteFile string and optimize it using the SDL_PixelFormat of the Screen

  entity (std::string file, int w, int h);
  entity () {};
  ~entity () { SDL_FreeSurface(spriteHandle);}

  entity (const entity &old) : pos(old.pos), x(old.x), y(old.y), spriteFile(old.spriteFile) {
  spriteHandle = new SDL_Surface(*spriteHandle);
  }

};

class multiEntity: public entity {
  /*
    Use multiEntity rather than entity when multiple images need to be blipped to different
    positions.
   */
private:
  static std::vector<stringBoolPair> deconstructed;
public:
  std::string entType;
  multiEntity (std::string file, int w, int h, std::string enttype) {
    entity(file, w, h);
    entType = enttype;
    bool found = false;
    for (int i = 0; i < deconstructed.size(); i++) {
      found = (enttype == deconstructed[i].str);
    }
    if (found) deconstructed.emplace_back(enttype, false);
  }

  multiEntity (const multiEntity& old) :
    spriteFile(old.spriteFile),
    x(old.x),
    y(old.y),
    spriteHandle(old.spriteHandle),
    pos(old.pos),
    entType(old.entType) {}
    ~multiEntity () {
    for (int i = 0; i < deconstructed.size(); i++) {
      if (deconstructed[i].str == entType) {
    SDL_FreeSurface(spriteHandle);
    deconstructed[i].Bool = true;
      }
    }
  }

  multiEntity& operator= (const multiEntity& old) {
    spriteFile = old.spriteFile;
    pos = old.pos;
    x = old.x;
    y = old.y;
    entType = old.entType;
    spriteHandle = old.spriteHandle;
    return *this;
  }
};

当我尝试编译包含此代码的代码时,我收到一条错误消息,指出 class multiEntity 没有任何名为“pos”的字段。除了 entType 之外,复制构造函数中的所有变量都会发生这种情况。 我试图做的是使用相同的 SDL_Surface 生成一个 entity vector 。因此,我觉得我应该创建一个单独的类,其中每个具有相同 entType 的对象都具有相同的 spriteHandle 值。这应该指向相同的图像,当我有 75 个图像实例要显示在屏幕上时,这是最有用的。我想使用 vector 的构造函数布局,以便复制信息而不是每次都创建一个新指针。

最佳答案

不能在派生类的拷贝构造函数中初始化基类的成员;它们应该通过基类的复制构造函数进行初始化。您应该只在成员初始化器列表中调用它:

multiEntity (const multiEntity& old) :
    entity(old),         // initialize entity's members via entity::entity(const entity&)
    entType(old.entType) // initialize multiEntity's member entType 
{}

如果基类的拷贝构造函数的行为不是你想要的,你可以在派生类的构造函数体中做一些赋值,例如

multiEntity (const multiEntity& old) :
                         // nothing specified, same as write entity() here
                         // entity's members will be initialized via entity::entity()
    entType(old.entType) // initialize multiEntity's member entType 
{
    // perform assignment here
    spriteFile = old.spriteFile;
    x = old.x;
    y = old.y;
    spriteHandle = old.spriteHandle;
    pos = old.pos;
}

关于c++ - 复制构造函数不识别继承的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44339562/

相关文章:

c++ - 如何在运行时使用静态成员函数初始化静态成员变量?

c++ - 在 C++ 中使用指针时的内存管理

Java 对象转换不能像我需要的那样工作

scala - 以下 'illegal inheritance' (Scala 2.9.2) 背后的原因是什么?

c++ - 未知的 C++ 对象实例化语法

java - 使用 clone() 复制字段与复制构造函数 Java

c++ - 为什么赋值运算符重载会创建一个对象的拷贝?

C++ Qt creator 我可以评论每一行,而不是只评论选择吗?

inheritance - 如何在backbone.js 中创建具有多种模型类型的集合?

c++ - 排序 vector 的最快方法是什么?