据我所知,构造函数应该在实现文件中定义,但我只能在一个主文件中找到包含类的示例,而不是拆分为 .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) 我只是想知道设置继承成员的最有效方法是,如果以下代码可以使用:
这是基类的声明:
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) 如果您正在循环通过容器:
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?
这个问题最重要的是好奇心,我唯一可以看到这是现实生活场景中适用的问题的唯一原因是,如果你需要传达的话,那就是你不要将价值观改变为另一个从事代码工作的人.