This question already has answers here:
Why is there no call to the constructor? [duplicate]
(3 answers)
Closed 5 years ago.
Class A
{
public:
A()
{
cout << "constructor called";
}
};
int main()
{
A obj; // constructor getting called
A obj1(); // constructor not getting called
}
When i instantiate obj object , my constructor getting called.
But when i instantiate obj1 , my constructor not getting called.
I would like to know the reason for it.
A obj1(); actually does not create any object, it's a function declaration.
() should be avoided here as the compiler can't tell if you meant to create a A object or declare a obj1 function returning a A object.
Related
This question already has answers here:
Why is the copy constructor called when we pass an object as an argument by value to a method?
(3 answers)
Copy Constructor in C++ [duplicate]
(5 answers)
why are copy constructors needed and what are the cases where they are very helpful? [duplicate]
(4 answers)
Who calls the copy constructor when passing by value?
(3 answers)
Closed 11 months ago.
I have the following code:
struct Entity {
Entity() {
std::cout << "[Entity] constructed\n";
}
~Entity() {
std::cout << "[Entity] destructed\n";
}
void Operation(void) {
std::cout << "[Entity] operation\n";
}
};
void funcCpy(Entity ent) {
ent.Operation();
}
int main() {
Entity e1;
funcCpy(e1);
}
This is the output:
[Entity] constructed
[Entity] operation
[Entity] destructed
[Entity] destructed
I expected my function to use the custom constructor, so the output would look like this:
[Entity] constructed
[Entity] operation
[Entity] constructed
[Entity] destructed
[Entity] destructed
Why does this happens? How could I use my custom constructor instead?
Thanks :)
https://en.cppreference.com/w/cpp/language/copy_constructor
object as params will call copy constructor of class
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?
This question already has answers here:
Two calls to destructor
(3 answers)
Closed 8 years ago.
In the following code, the destructor is called twice, while the constructor is called only once:
enum TFoo
{
VAL1,
VAL2
};
class CFoo
{
public:
TFoo mf;
CFoo()
{
cout<<"hi c'tor1\n";
//mf = f;
}
CFoo(TFoo f)
{
cout<<"hi c'tor2\n";
mf = f;
}
CFoo(TFoo &f)
{
cout<<"hi c'tor3\n";
mf = f;
}
~CFoo()
{
cout<<"bye\n";
}
};
int main()
{
vector<CFoo> v;
//v.assign(1, VAL1);
v.push_back(VAL1);
}
The code outputs:
hi c'tor2
bye
bye
I found a similar question, which mentioned copy constructors, so I added them, but with the same result. Uncommenting the line //v.assign(1, VAL1); also doesn't change anything.
It is initially constructing using the implicit conversion operator between TFoo and CFoo, CFoo(TFoo f), and then using that temporary object to pass it to push_back to construct the object in the container using the default copy constructor or move constructor, depending on whether you are using C++11 (which is not displaying anything). Then the temporary is destroyed and finally the object in the container (with the container itself).
You can see it here or here (C++11) even better.
This question already has answers here:
Why is the copy constructor not called?
(4 answers)
Closed 9 years ago.
I have an understanding that copy constructor will be called when an object is created from an already existing object and also when a function returns object by value. So why in the below code the copy constructor is not called but the default constructor?
class A {
public:
A() { cout << "A" << endl; }
A(A &) { cout << "A&" << endl; }
A(const A &) { cout << "const A&" << endl; }
};
A fun() {
class A a;
return a;
}
int main() {
class A a = fun(); // output: A
}
Short answer: compiler optimizations.
First, the a object from your function is created directly in the scope of the main function, in order to avoid having to copy (or move in C++11) the local parameter out of the scope of the function via the function's return. This is return value optimization.
Then, in main, the statement becomes equivalent to class A a = A() and again the compiler is allowed to the create the a object in place, without copying from a temporary object. This is copy elision.
This is allowed even if the copy constructor (which is bypassed entirely) has side-effects, like in your example.
This question already has answers here:
Do the parentheses after the type name make a difference with new?
(7 answers)
Default constructor with empty brackets
(9 answers)
different types of initialization in C++
(2 answers)
Closed 10 years ago.
class A {
public:
A() { cout << "Constructor\n"; } // (1) default constructor
};
A obj; // (2) instantiating obj
A obj(); // (3)
What is the difference between instantiating obj and obj()? obj calls the default constructor (1) mentioned above. Which constructor will obj() call?
A obj(); declares a function called obj which takes no arguments and which returns an A. It does not declare an A object at all.
As A obj(); does not declare an A object, it does not result in any constructor call.
The second one is declared as a function. the name of the function is obj. It takes no arguments. It returns the object of type A.