This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference between A* pA = new A; and A* pA = new A();
Variable initialization (pointer and value)
Assuming that MyClass has a default constructor, what's the difference between
MyClass *mc = new MyClass;
and
MyClass *mc = new MyClass();
Assuming that MyClass has a default constructor
2 extra characters in the code.
If the class is a POD type (not your case), the latter will perform value-initialization.
Related
This question already has answers here:
What uses are there for "placement new"?
(25 answers)
Two different values at the same memory address
(7 answers)
Closed 19 days ago.
What does (ptr) stands for and why does it change the data stored in ptr?
struct X
{
int a;
int const b;
};
X* ptr = new X{ 1, 2 };
X* nptr = new (ptr) X{ 3, 4 };
And for some reason it doesn't run in any godbolt's compilers but does in visual studio
It requires #include <new> to work. It is an overloaded new operator which calls the constructor without allocating new memory. Instead, the new struct is constructed to memory pointed by ptr and the old data is thus overwritten. See that for reference.
This question already has answers here:
What is the point of deleted destructor?
(1 answer)
How does =delete on destructor prevent stack allocation?
(6 answers)
Closed 4 years ago.
We can define a class with its destructor deleted:
class A {
public :
A() = default;
~A() = delete;
};
Then, we cannot define a variable of type A.
However, we can use new operator to allocate and construct an object of type A in the heap and we can never delete it.
I want to know whether this class has any usage in the real-world?
This question already has answers here:
Does destroying and recreating an object make all pointers to this object invalid?
(6 answers)
Is it allowed to call destructor explicitly followed by placement new on a variable with fixed lifetime?
(2 answers)
Closed 4 years ago.
Suppose I have a class like so:
#include <new>
struct thing {
thing() = default;
void foo()
{
this->~thing();
new (this) thing();
}
};
Is calling the destructor like this and then reconstructing the object using a placement new defined behavior?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
For example I have line of code:
EDITED:
MyClass myObject = MyClass(5);
Here, the constructor of MyClass MyClass() will be called. It take the parameter and does what suposed..
But I this example myObject is not a reference - it is an actual object. So how the compiler do this:
construct an object by MyClass constructor, which does not know the location of actual object
(by saying location I mean address in RAM on stack).
Same question applies also to other examples (myObject = new MyClass(5);, myobject(5), etc.)
EDIT
1) Why MyClass myObject = MyClass(i);
This is actually equivalent to: MyClass myObject(i);
2) Is in this case "=" is operator= (overloaded)?
3) Can you please write an equivalent code to compiler generated operations?
4) What role copy-constructor acts here (MyClass myObject = MyClass(i);)?
MyClass myObject = MyClass( int i );
This is a syntax error, an illegal mixture of declaration and use. You probably meant:
int i = 42;
MyClass myObject = MyClass( i );
This is actually equivalent(*) to:
MyClass myObject( i );
This declares a MyClass object with local scope ("on the stack"), and then initializes the memory using the MyClass constructor.
myObject = new MyClass(int i);
Again, this is an invalid mix of declaration and use syntax. You probably meant:
int i = 42;
MyClass * myObject = new MyClass( i );
This allocates dynamic storage ("on the heap") and constructs a MyClass object there.
A pointer to that memory / object is then assigned to myObject, which is of type pointer to MyClass.
In both cases, the constructor initializes a MyClass object at {some memory location}. Which location that is depends on the context in which the constructor is called. The compiler knows that context, and knows (how to obtain) the memory address at runtime.
Answering the added questions (which, actually, should be separate questions):
1) Why MyClass myObject = MyClass(i); This is actually equivalent to: MyClass myObject(i);
This looks like a constructor call creating a temporary MyClass object which is then assigned (operator=()) to myObject.
But it is not, because that would be just stupid.
Since myObject -- the object assigned to -- is created by this statement, and the temporary would not exist after this statement, the C++ standard allows the "temporary" MyClass object to be created in place of myObject by the constructor(*), avoiding the duplicate effort of construction / assignment / destruction of the temporary.
(A mechanic that C++11 expanded to any kind of temporaries, in a way, with the advent of the "move" semantics and && rvalue references.)
Consider:
#include <iostream>
class MyClass
{
public:
MyClass( int i ) : member( i ) { std::cout << "const\n"; }
~MyClass() { std::cout << "dest\n"; }
MyClass & operator=( MyClass const & other ) { std::cout << "assign\n"; }
private:
int member;
};
int main()
{
MyClass x = MyClass(42);
}
This will print:
const
dest
There is no assignment.
2) Is in this case "=" is operator= (overloaded)?
No. As I said, that specific statement is equivalent to MyClass myObject( i ) -- no assignment taking place, just in-place construction.
3) Can you please write an equivalent code to compiler generated operations?
I don't think you will learn much by looking at disassembly. And anyway, your compiler is better at that than I am. (Or you are.)
4) What role copy-constructor acts here (MyClass myObject = MyClass(i);)?
What role? I have no idea what you mean.
(*): As rici correctly pointed out, the copy elision making this equivalent is only legal if a visible copy / move constructor exists.
This question already has answers here:
Is there a difference between copy initialization and direct initialization?
(9 answers)
Closed 7 years ago.
If a class is having constructor which will take parameters as below
class myclass
{
public:
myclass(int a, int b);
};
what are the differences between creating objects as below
myclass a(3, 4);
myclass a = myclass(3, 4);
and which one is better?
The first one is better and recommended. The second one creates a temporary then uses copy construction, although most of the time the copy is elided.