小编Nic*_*age的帖子

实现中的构造函数与标头

据我所知,构造函数应该在实现文件中定义,但我只能在一个主文件中找到包含类的示例,而不是拆分为 .h 和 .cpp 文件

我需要知道的是我的以下代码是否以可接受的方式分开。

实体.h:

    using namespace std;

class cEntity {
private:
    /*-----------------------------
    ----------Init Methods---------
    -----------------------------*/
    int *X, *Y;
    int *Height, *Width;

public:
    /*-----------------------------
    ----------Constructor----------
    -----------------------------*/
    cEntity (int,int, int, int);

    /*-----------------------------
    ----------Destructor-----------
    -----------------------------*/
    ~cEntity ();

    /*-----------------------------
    ----------Set Methods----------
    -----------------------------*/

    /*Set X,Y Methods*/
    void setX(int x){*X=x;};
    void setY(int y){*Y=y;};
    void setXY(int x, int y){*X=x; *Y=y;};

    /*Set Height, Width Methods*/
    void setHeight(int x){*Height=x;};
    void setWidth(int x){*Width=x;};
    void setDimensions(int x, int y){*Height=x; *Width=y;};

    /*-----------------------------
    ----------Get Methods----------
    -----------------------------*/

    /*Get X,Y Methods*/
    int …
Run Code Online (Sandbox Code Playgroud)

c++ implementation header

5
推荐指数
1
解决办法
1万
查看次数

在派生构造函数中调用基本方法的不良做法?

我只是想知道设置继承成员的最有效方法是,如果以下代码可以使用:

这是基类的声明:

class cEntity{
private:
    int X, Y;
    int Height, Width;
public:
    cEntity();
    cEntity(int x,int y,int h,int w);
    ~cEntity();

    void setX(int x){X=x;};
    void setY(int y){Y=y;};
    void setCoords(int x, int y){X=x;Y=y;};

    void setHeight(int h){Height = h;};
    void setWidth(int w){Width = w;};
    void setArea(int h, int w){Height=h;Width=w;};

    int getX(){return X;};
    int getY(){return Y;};
    //void getXY(int,int);

    int getHeight(){return Height;};
    int getWidth(){return Width;};
    //void getArea(int,int);
};
Run Code Online (Sandbox Code Playgroud)

这是派生类的构造函数:

cOrganism::cOrganism () {
    setCoords(0,0);
    setArea(0,0);
    Name = "UNKNOWN";
    Health = 100;
    MaxHealth = 100;
    HealthHiRange =100;
    HealthLoRange …
Run Code Online (Sandbox Code Playgroud)

c++ oop methods constructor class

2
推荐指数
1
解决办法
191
查看次数

'auto'关键字是否知道何时使用const迭代器?

如果您正在循环通过容器:

typedef std::vector<std::unique_ptr<BaseClass>> Container;
Container container;

for(Container::const_iterator element = container.begin(); element != container.end(); element++)
{
    //Read through values
}
Run Code Online (Sandbox Code Playgroud)

而不是使用typedef你决定使用auto:

std::vector<std::unique_ptr<BaseClass>> container;

for(auto element = container.begin(); element != container.end(); element++)
{
    //Read through values
}
Run Code Online (Sandbox Code Playgroud)

假设你不改变这些值,auto关键字是否使用const迭代器而非const const?

这个问题最重要的是好奇心,我唯一可以看到这是现实生活场景中适用的问题的唯一原因是,如果你需要传达的话,那就是你不要将价值观改变为另一个从事代码工作的人.

c++ loops vector auto c++11

2
推荐指数
1
解决办法
1477
查看次数

标签 统计

c++ ×3

auto ×1

c++11 ×1

class ×1

constructor ×1

header ×1

implementation ×1

loops ×1

methods ×1

oop ×1

vector ×1