Constructor in implementation versus header

The constructor should, as far as I know, be defined in the implementation file, but I could only find examples with the class inside one main file instead of splitting it into .h and .cpp file

All I need to know is that my next code is split appropriately.

Entity.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 getX(){return *X;}; int getY(){return *Y;}; /*Get Height, Width Methods*/ int getHeight(){return *Height;}; int getWidth(){return *Width;}; }; 

and Entity.cpp:

 #include "Entity.h" cEntity::cEntity (int x, int y, int height, int width) { X,Y,Height,Width = new int; *X = x; *Y = y; *Height = height; *Width = width; } cEntity::~cEntity () { delete X, Y, Height, Width; } 

I would also like to thank everyone for being so helpful, especially on my first question!

+4
source share
5 answers
 cEntity::cEntity (int x, int y, int height, int width) { 

right

  X,Y,Height,Width = new int; 

not so much. This sets Width to the new int , but not all the others. You probably planned:

  X = new int(x); Y = new int(y); Height = new int(height); Width = new int(width); 

Please note that this construction method will not work for objects without assignment / copying, for example links. For some objects, it is also slower than creating them. Thus, the preferred way to build it looks like this:

 cEntity::cEntity (int x, int y, int height, int width) { :X(new int(x)) ,Y(new int(y)) ,Height(new int(height)) ,Width(new int(width)) {} 

This is better, but if any exceptions are thrown, you will have to somehow release the highlighted ones. It is better to make each of these members std::unique_ptr<int> , so that they free themselves and save you a lot of headaches.

+3
source

Yes, everything is in order. However, there is a problem with your constructor and destructor. In fact, your code allocates one int, and your destructor also frees one int. In any case, there is no need to use pointers. Rather, a better implementation (if we don't use smart pointers) could be:

[Entity.h]

 private: /*Private fields*/ int X, Y; int Height, Width; 

[Entity.cpp]

 cEntity::cEntity (int x, int y, int height, int width) { X = x; Y = y; Height = height; Width = width; } cEntity::~cEntity () { } 

One more thing. Try to avoid using namespace std; in your header files. If you do this, you will force those who include your header to use this using statement, and it can cause namespace conflicts.

+3
source

Your division is beautiful. The implementations of these functions are incorrect, but you have separated them from the declaration accordingly. (They do not isolate or release as many objects as you think.)

+1
source

Yes. For separation, at least, is the best way to do this.

Regarding the actual implementation, you have some problems. I'm not quite sure what you are trying to do with the constructor, or if you have the correct data types for class member variables, but something seems to be turned off.

0
source

Any method defined in the class is directly implicitly nested, including the constructor.

those.

 class MyClass { public: MyClass() {}; }; 

defines a built-in constructor that may (or may not) improve the performance of your code,

While

 class MyClass { public: MyClass(); }; MyClass::MyClass() { }; 

is not built-in, and therefore will not have these advantages. Both options are correct C ++, though.

Only my 2 cents.

PS And yes, when you decide to store pointers inside the class this way, you will open the Pandora window.

-1
source

Source: https://habr.com/ru/post/1380856/


All Articles