initialization - C++ Class design - easily init / build objects -
Using C ++ I have created a square of several squares, as well as many functions that can be said in the row during the runtime. So I ended up with the code that looks:
A * A = new A (); To do this & gt; Seta (); To do this & gt; Setb (); To do this & gt; SETC (); ... A-> DOA (); To do this & gt; date of birth (); No, this is bad, but I do not like typing "a->"
So I liked to rewrite the definition of my class: < Pre> class A {public: A (); Virtual ~ A (); A * set A (); A * set B (); A * set C (); A * Doo (); A * Doo (); // Other functions private: // vars};
Then I can do my class like this: (method 1)
a * one = new A (); To do this & gt; Seta () - & gt; Setb () - & gt; SETC (); ... a- & gt; DoA () - & gt; Dob ();
(which I like because it's easy to write)
For a more accurate implementation of this, you can see your SDL Sprite C ++ Class
Everything seems to work well, however, I would be interested in any reaction to this approach I have seen a problem if I like in my class like: (Method 2)
A = A (); A.setA () - & gt; Setb () - & gt; SETC (); ... a.doA () - & gt; Submerged ();
Then I have various memory problems and sometimes nothing works (you can change it by changing it to all the Sprite objects in the main CPP of my Sprite demo ).
Is this normal? Or what behavior should be the same?
Edit are primarily meant to simplify my life in the beginning. My main question method method 1 and method 2 treat me differently?
Edit: Here is an example recipient and setter:
Sprite * Sprite: set speed (int i) {speed = i ; This return; } Ent Sprite :: Mills Speed () {Return speed; }
A note is not related to your question, statement aa = A ();
Probably you are not expecting objects in C ++ are not reference types, which are blank by default, so this statement is almost never true. You might have a a a;
A
to be a new example of A
, but = A ()
part is a temporary default A copy of the A
with the built code is the convertor. If you just had aa
then this would have just created a new instance of A
using the default constructor.
If you do not explicitly apply your own copy constructor for a class, the compiler will make it one for you. The compiler made copy creator will only make a carbon copy of the data of other objects; This means that if you have any indication, then it will not copy the data.
So essentially, this is making a new example of the line A
, then the second construct is the temporary example of a
with the default constructor, then the temporary Copying A
to the new A
, then destroying the temporary A
. If the temporary A
is acquiring the resources in its constructor and is de-allocating it to the district constructor, then you can take part in issues where your object is already deployed data Trying to use that which is undefined behavior.
Take this code for example:
struct A {a () {myData = new int; Std :: cout & lt; & Lt; "Lectated int" and "lt;
The output will look something like this:
Dialed Intex 0x9FB7128 for Int 0x9FB7128 AAMAdata allocated on 0x9FB7128 As you can see, a.myData is pointing to an address that was already distributed. Have If you try to point out the data, then you can access completely invalid data or even the data of any other object that takes place in memory. Once again, your If a
is out of the scope, it will try to delete the data for the second time, which will cause more problems.