arrays - C++ Problem initializing an object twice -


I am relatively new to C ++ and the objects and pointers of the object are having difficulty understanding it immediately.

In terms of memory and usage, what is the difference between these two announcements? :

MyClass obj1; MyClass * obj2;

And the specific problem I have is that I have a class with an unsigned short array where the last position of the array varies if I do this:

  Myklus Ob; Obj = MyClass ("123");  

MyClass has two constructors that will take an int and by default it will specify zero and divide it into three digits or less. And the second that will represent a number of a string and will do this ... hope that makes sense!

This works well if I declare it

  MyClass obj = MyClass ("123,123,123");  

But if I do not do it in another way. Why?

difference:

  MyClass obj1; MyClass * obj2;  

Here is an example of obj1 MyClass.
While obj2 can potentially catch the address of an example of MyClass

Apart from this obj1 will be automatically started by consultants, while obj2 has not been started by default (and Thus indicates random memory). Once launched, obj2 can take special value tap which indicates that it is not pointing to an object.

  obj2 = & amp; Obj1;  

To get the obj1 memory here, we initialize obz2. If you change any member of obj1, you can see the changes by seeing them through obj2 (but because obj2 is a pointer that requires you to de-reference).

  obj1.plop = 5; Std :: cout & lt; & Lt; Obj1.plop & lt; & Lt; "\ N"; Std :: cout & lt; & Lt; Obj2-> Flat & lt; & Lt; "\ N"; The same values ​​should print  

The following are actually two different things:

  MyClass obj; Obj = MyClass ("123");  
  • The line launches 'obj' with an original manufacturer.
  • Line 2: String creates a temporary object created with "123" Once this temporary object is created, it is copied to 'Objie' using the assignment operator. If you have not defined the assignment operator, then the compiler will generate one for you. If your class consists of pointers, then the default version probably will not work correctly (in most other situations, the default assignment operator should work fine).

This row probably works:

  Myklos Obj = McCleus ("123123123");  

Because the compiler has optimized it:

  MyClass obj ("123123123");  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -