C ++继承和构造函数

时间:2010-05-29 13:31:15

标签: c++ inheritance

试图找出如何使用具有继承类的构造函数。我知道这是非常错误的,我现在已经写了大约三天的C ++了,不管怎么说这是我的代码:

clientData.h,两个类,ClientData扩展实体:

#pragma once

class Entity
{
public:
 int x, y, width, height, leftX, rightX, topY, bottomY;

 Entity(int x, int y, int width, int height);
 ~Entity();
};

class ClientData : public Entity
{
public:
 ClientData();
 ~ClientData();
};

和clientData.cpp,其中包含以下函数:

#include <iostream>
#include "clientData.h"
using namespace std;

Entity::Entity(int x, int y, int width, int height)
{
 this->x = x;
 this->y = y;
 this->width = width;
 this->height = height;

 this->leftX = x - (width/2);
 this->rightX = x + (width/2);
   this->topY = y - (height/2);
 this->bottomY = y + (height/2);
}

Entity::~Entity()
{
 cout << "Destructing.\n";
}

ClientData::ClientData()
{
 cout << "Client constructed.";
}

ClientData::~ClientData()
{
    cout << "Destructing.\n";
}

最后,我正在创建一个新的ClientData:

ClientData * Data = new ClientData(32,32,32,16);

现在,我并不感到惊讶,我的编译器向我发出错误,所以如何将参数传递给正确的类?

第一个错误(来自MVC2008)是 错误C2661:'ClientData :: ClientData':没有重载函数需要4个参数

和第二个,它弹出我似乎做出的任何改变 错误C2512:'实体':没有适当的默认构造函数可用 感谢。

6 个答案:

答案 0 :(得分:4)

目前,Client数据类的构造函数不起作用。您需要为客户端数据创建一个构造函数,如:

ClientData(int x, int y, int width, int height): Entity(x, y, width, height)

如果你想打电话

new ClientData(32,32,32,16);

答案 1 :(得分:3)

第一点

new ClientData(32,32,32,16);

不会工作,因为ClientData的唯一构造函数不带参数。构造函数不是在c ++中继承的,你必须再次定义构造函数。

class ClientData : Entity
{
   public:
   ClientData(int a,int b,int c,int d);
   //...
}

其次是调用基类的构造函数。通常,编译器使用调用基类的非参数构造函数,因为实体只有一个带参数的构造函数,这将失败 - 你必须对实体构造函数进行显式调用。

ClientData::ClientData(int a,int b, int c, int d)
: Entity(a,b,c,d)//Initializer list call base class constructor here
{
 //...
}

答案 2 :(得分:2)

使用constructor initializer初始化基础和成员:

struct Entity {
  int x, y, width, height, leftX, rightX, topY, bottomY;

  Entity(int x, int y, int width, int height);
};

Entity::Entity(int x, int y, int width, int height)
: x(x), y(y), width(width), height(height),
  leftX(x - (width / 2)), rightX(x + (width / 2)),
  topY(y - (height / 2))
{
  bottomY = y + (height / 2); // for members like leftX, rightX, topY,
  // and bottomY, assignment inside the ctor (instead of initialization)
  // can be appropriate
}


struct ClientData : Entity {
  ClientData();
  ClientData(int x, int y, int width, int height);
};

ClientData::ClientData() : Entity(0, 0, 0, 0) {}  // you may not even want a
// default ctor for this type

ClientData(int x, int y, int width, int height)
: Entity(x, y, width, height)
{}

答案 3 :(得分:1)

答案 4 :(得分:0)

您将使用初始化列表来调用基类的构造函数(注意:它也可以用于调用对象中类对象的构造函数):

class Base
{
    private:
    int myVal;

    public:
    Base(int val)
    {
        myVal = val;
    }
};

class Heir : public Base
{
    private:
    std::string myName;

    public:
    Heir(std::string Name, int Val) : Base(Val)
    {
        myName = Name;
    }
};

答案 5 :(得分:0)

我对您的代码进行了一些更改。它可能会对C ++类,成员和构造函数有所帮助。

使用继承类时,必须在派生类构造函数上调用带有必要参数的基类构造函数:

ClientData :: ClientData(int x,int y,int width,int height):实体(x,y,宽度,高度)

只是意见:不要使用与类成员相同的参数名称。我通常在课堂成员之前使用'm_'前缀,以便于识别。

class Entity
{
public:
 int m_x, m_y, m_width, m_height, m_leftX, m_rightX, m_topY, m_bottomY;

 Entity(int x, int y, int width, int height);
 ~Entity();
};

class ClientData : public Entity
{
public:
 ClientData(int x, int y, int width, int height);
 ~ClientData();
};

Entity::Entity(int x, int y, int width, int height)
{
 m_x = x;
 m_y = y;
 m_width = width;
 m_height = height;

 m_leftX = x - (width/2);
 m_rightX = x + (width/2);
 m_topY = y - (height/2);
 m_bottomY = y + (height/2);
}

ClientData::ClientData(int x, int y, int width, int height) : Entity(x, y, width, height)
{
 cout << "Client constructed.";
}