复制构造函数无法识别继承的成员
Posted
技术标签:
【中文标题】复制构造函数无法识别继承的成员【英文标题】:Copy constructor doesn't recognize inherited members 【发布时间】:2017-06-03 02:07:01 【问题描述】:我有两个班级:
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 does not have any field named 'pos'
。除了entType
之外,复制构造函数中的所有变量都会发生这种情况。
我正在尝试做的是使用相同的 SDL_Surface 拥有entity
s 的向量。因此,我觉得我应该创建一个单独的类,其中每个具有相同entType
的对象都具有相同的spriteHandle
值。这应该指向同一个图像,当我有 75 个图像实例我试图在屏幕上闪烁时,这是最有用的。我想使用vector的构造函数布局,以便复制信息而不是每次都创建一个新指针。
【问题讨论】:
Initialize parent's protected members with initialization list (C++)的可能重复 您无需发布所有这些代码即可指出您遇到的问题。 An example 谢谢@PaulMcKenzie。我会尽量记住这一点,并在未来更具描述性。 【参考方案1】:您不能在派生类的复制构造函数中初始化基类的成员;它们应该通过基类的复制构造函数进行初始化。您应该只在成员初始化器列表中调用它:
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;
【讨论】:
那么我如何确保 SDL_Surface 是浅拷贝?我希望它复制父类中的数据,但只复制子类中的地址。 @BobPaw 答案已修改。以上是关于复制构造函数无法识别继承的成员的主要内容,如果未能解决你的问题,请参考以下文章