I have two classes: OuterClass and InnerClass. InnerClass is a private member of OuterClass and should be created in OuterClass constructor with an InnerClass(int) constructor, however the default InnerClass constructor is still called.
InnerClass.hpp:
#ifndef INNERCLASS_HPP_
#define INNERCLASS_HPP_
class InnerClass {
public:
int a;
InnerClass();
InnerClass(int);
~InnerClass();
};
#endif /* INNERCLASS_HPP_ */
InnerClass.cpp:
#include "InnerClass.hpp"
#include <iostream>
InnerClass::InnerClass() {
a = 1;
std::cout << "inner class constructed, a = " << a << std::endl;
}
InnerClass::InnerClass(int x) {
a = x;
std::cout << "inner class constructed, a = " << a << std::endl;
//automatically: object InnerClass (a=3) is destroyed here...
}
InnerClass::~InnerClass() {
std::cout << "inner class destructed, a = " << a << std::endl;
}
OuterClass.hpp:
#ifndef OUTERCLASS_HPP_
#define OUTERCLASS_HPP_
#include "InnerClass.hpp"
class OuterClass {
private:
InnerClass blah;
public:
OuterClass();
~OuterClass();
void doSth();
};
#endif /* OUTERCLASS_HPP_ */
OuterClass.cpp:
#include "OuterClass.hpp"
#include <iostream>
OuterClass::OuterClass() {
// automatically: blah = InnerClass();
std::cout << "outer class constructing started, blah.a = " << blah.a << std::endl;
blah = InnerClass(3);
std::cout << "outer class constructed" << std::endl;
}
OuterClass::~OuterClass() {
std::cout << "outer class destructed" << std::endl;
}
void OuterClass::doSth() {
std::cout << "doSth: " << blah.a << std::endl;
}
main:
#include "OuterClass.hpp"
#include <iostream>
int main(int argc, char** argv) {
std::cout << "Compiled at " << __TIME__ << std::endl;
OuterClass x = OuterClass();
x.doSth();
std::cout << "done" << std::endl;
}
output:
Compiled at 12:11:12
inner class constructed, a = 1 //this is unexpected
outer class constructing started, blah.a = 1 //this should be random data
inner class constructed, a = 3
inner class destructed, a = 3 //this is unexpected
outer class constructed
doSth: 3
done
outer class destructed
inner class destructed, a = 3
Questions:
Why is the default constructor of InnerClass called at the start of OuterClass constructor?
What and why is destructed in OuterClass constructor ("inner class destructed, a = 3 //this is unexpected")?
It seems that InnerClass object with a = 3 was destructed in the OuterClass constructor, than why does method doSth() return 3 instead of random data?
Why does removing an InnerClass() constructor (from both InnerClass.hpp and InnerClass.cpp files) result in compile-time error at the OuterClass constructor in OuterClass.cpp file? The error says that no InnerClass() definition found.
Use initializer-list in constructor.
OuterClass::OuterClass() : blah(3) {
// automatically: blah = InnerClass();
std::cout << "outer class constructing started, blah.a = " << blah.a << std::endl;
std::cout << "outer class constructed" << std::endl;
}
Since when you use
OuterClass::OuterClass() {
// automatically: blah = InnerClass();
std::cout << "outer class constructing started, blah.a = " << blah.a << std::endl;
blah = InnerClass(3);
std::cout << "outer class constructed" << std::endl;
}
firstly for initialize blah will be called default c-tor and in blah = InnerClass(3);, that creates temporary object and copy it to blah, after this string will be called destructor of temporary object.
1) Why is the default constructor of InnerClass called at the start of OuterClass constructor?
To construct blah.
2) What and why is destructed in OuterClass constructor ("inner class destructed, a = 3 //this is unexpected")?
The InnerClass(3) you constructed in the second line of the constructor. The one you used to hold the value you assigned to blah. It is destructed because it goes out of scope once the assignment to blah is complete.
3) It seems that InnerClass object with a = 3 was destructed in the OuterClass constructor, than why does method doSth() return 3 instead of random data?
Because you assigned the value 3 to blah. Your code reads;
blah = InnerClass(3);
This creates an InnerClass with the value 3 and then copies its value to blah. So both blah and this temporary object have the same value. The temporary is then destroyed.
If you think about it, there is no other sensible way to implement this line of code.
4) Why does removing an InnerClass() constructor (from both InnerClass.hpp and InnerClass.cpp files) result in compile-time error at the OuterClass constructor in OuterClass.cpp file? The error says that no InnerClass() definition found.
Because then you have no way to construct blah in the first place. (As others have pointed out, you probably wanted an initializer list to construct blah right in the first place rather than default constructing it and then having to go to contortions to fix it.)
OuterClass::OuterClass() /* default blah constructor is called here */ {
std::cout << "outer class constructing started, blah.a = " << blah.a << std::endl;
blah = InnerClass(3); /* a temporary InnerClass is created */
std::cout << "outer class constructed" << std::endl;
} //temporary InnerClass destroyed (out of scope)
Use initializer list to prevent calling default constructor as ForEveR suggests
When the constructor of OuterClass starts executing the body, all members must be constructed. Since there is a blah InnerClass member, it got constructed.
When you assign to blah in OuterClass::OuterClass(), there is a temporary instance created. Here's the order of things:
A temporary InnerClass(3) is constructed.
It is assigned to blah.
The temporary value is destructed -- that's why there's a destructor call.
Follows from 2: blah has a set to 3 in the assignment mentioned in #2 above.
One'd infer that in C++ if you have a specialized argument-taking constructors declared, the default constructor is not created behind your back by the compiler anymore. I can't bother to look into the relevant place in the standard right now, editors feel free to correct this one.
It'd have helped you if you instrumented InnerClass as follows:
Keep a static instance counter, and copy it to every instance. That way you'd know which instance is reporting things back.
Manually write InnerClass & operator=(const InnerClass &) to see when is it invoked.
(PS: Are you coming from Java World to C++ by any chance?)
Output explained:
inner class constructed, a = 1 //this is unexpected
InnerClass is constructed with default constructor first
outer class constructing started, blah.a = 1 //this should be random data
This is the value of InnerClass constructed in previous step with default ctor
inner class constructed, a = 3
A temporary InnerClass is constructed with the overloaded ctor and assigned to the blah member (the implicit operator= is called - you can check by overloading the operator=)
inner class destructed, a = 3 //this is unexpected
The temporary InerClass created in the previous step is destroyed
outer class constructed
doSth: 3
done
outer class destructed
inner class destructed, a = 3
Answers:
Why is the default constructor of InnerClass called at the start of
OuterClass constructor?
Because you have InnerClass member. You should make it a pointer or use initializer list.
OuterClass::OuterClass()
: blah(3)
{
// automatically: blah = InnerClass();
std::cout << "outer class constructing started, blah.a = " << blah.a << std::endl;
// blah = InnerClass(3);
std::cout << "outer class constructed" << std::endl;
}
What and why is destructed in OuterClass
constructor ("inner class destructed, a = 3 //this is unexpected")?
The temporary InnerClass is destroyed here.
It seems that InnerClass object with a = 3 was destructed in the
OuterClass constructor, than why does method doSth() return 3
instead of random data?
The blah member now has a copy of InnerClass object with value 3
Why does removing an InnerClass()
constructor (from both InnerClass.hpp and InnerClass.cpp files)
result in compile-time error at the OuterClass constructor in
OuterClass.cpp file? The error says that no InnerClass() definition
found.
You mean removing the default constructor. Yes, the error is expected because blah member is first created with default ctor.
Related
Class B expects to receive an instance of shared_ptr<IError>.
Class A implements IError and is passed by value to the constructor of B.
I would like to understand how this scenario is handled. How does the shared_ptr as a template class handle the conversion to IError?
In a simple case where B receives shared_ptr<A> I assume the copy constructor is called and the reference counter is increased. However since IError is pure virtual a normal copy constructor invocation seems not to be case here?
// Example program
#include <iostream>
#include <string>
class IError
{
public:
virtual ~IError(){};
virtual void OnError() = 0;
};
class A : public IError
{
public:
A(){};
void OnError(){std::cout << "Error Occured" << std::endl;}
};
class B
{
public:
B(std::shared_ptr<IError> errorReporter): mErrorReporter(errorReporter){}
void Action(){mErrorReporter->OnError();}
private:
std::shared_ptr<IError> mErrorReporter;
};
int main()
{
auto objA = std::make_shared<A>();
auto objB = std::make_shared<B>(objA);
objB->Action();
}
Debugging time! Let's find out what happens by using the tools we have available as developers.
The memory of the shared_ptr objA looks like this (type &objA in the memory window; it will be replaced by its address):
It has a pointer to the object (000002172badd8e0) and a pointer to the control block.
The control block looks like this (copy and paste the second value into a new memory window):
It has a pointer to the allocator (first 2 columns), the reference count (1) and the weak reference count (0 + offset 1).
After objB has been created, the control block of objA has changed to a reference count of 2:
And the shared_ptr objB looks like this:
It points to the a shared pointer and to the control block.
The shared pointer in objB points to the same object as before (000002172badd8e0), so no copy of the actual object has been made.
The control block of objB indicates that objB only has a reference count of 1:
a normal copy constructor invocation seems not to be case here?
Correct. No copy of the object is made, as we can confirm with a debugger. But a copy of the shared_ptr has been made.
It doesn't.
Copy a shared_ptr doesn't copy it's point-to object, just like normal pointer
std::shared_ptr<A> a = std::make_shared<A>();
std::shared_ptr<IError> i = a;
A* a = new A;
IError* i = a; // no copy A
You are right in that the base class IError is abstract, hence it cannot be instantiated, never mind copied.
The code below has been modified from the original to show how each newly created shared_ptr just increments the reference count of the original shared_ptr. So, a shallow copy.
In your code, as well as in the code below, the underlying object to these shared_ptrs is concrete class A, derived from the abstract IError, so it is legal to shallow copy it.
// Example program
#include <iostream>
#include <string>
#include <memory>
class IError
{
public:
virtual ~IError(){};
virtual void OnError() = 0;
};
class A : public IError
{
public:
A(){std::cout << "A created.\n";};
void OnError(){std::cout << "Error Occured" << std::endl;}
};
class B
{
public:
B(std::shared_ptr<IError> errorReporter): mErrorReporter(errorReporter){
std::cout << "B created from A.\n";
}
void Action(){mErrorReporter->OnError();}
private:
std::shared_ptr<IError> mErrorReporter;
};
int main()
{
auto objA = std::make_shared<A>();
std::cout << "I. Reference count for objA: " << objA.use_count() << '\n';
auto objB = std::make_shared<B>(objA);
std::cout << "II. Reference count for objA: " << objA.use_count() << '\n';
// objB->Action();
auto objBB = std::make_shared<B>(*objB);
std::cout << "Created objBB from objB\n";
std::cout << "III. Reference count for objA: " << objA.use_count() << '\n';
std::cout << "Reference count for objB: " << objB.use_count() << '\n';
std::cout << "Reference count for objBB: " << objBB.use_count() << '\n';
// auto objB_from_base = std::make_shared<B>(IError()); // ERROR: IError is an abstract class
}
with output:
A created.
I. Reference count for objA: 1
B created from A.
II. Reference count for objA: 2
Created objBB from objB
III. Reference count for objA: 3
Reference count for objB: 1
Reference count for objBB: 1
I know when a constructer is being called, then it gets created in the memory, and when it gets out of the block it gets destroyed unless it's static.
Know I have this code:
#include <iostream>
#include <string>
using namespace std;
class CreateSample
{
private:
int id;
public:
CreateSample(int i)
{
id = i;
cout << "Object " << id << " is created" << endl;
}
~CreateSample()
{
cout << "Object " << id << " is destroyed" << endl;
}
};
void fuct()
{
CreateSample o1(1);
static CreateSample o2(2);
}
CreateSample o3(3);
void fuct2(CreateSample o);
int main()
{
fuct();
static CreateSample o4(4);
CreateSample o5(5);
fuct2(o5);
return 0;
}
void fuct2(CreateSample o)
{
CreateSample o6 = o;
}
and my concern is in object o5, why it's getting called once and gets destroyed 3 times?
When you wrote fuct2(o5); you're calling the function fuct2 and passing the argument by value. This means a copy of the argument will be passed to the function using the implicitly defined copy constructor. Thus you get the 2nd destructor call corresponding this object o.
Moreover, in fuct2 you have CreateSample o6 = o; which will also use the implicitly defined copy constructor to create o6. Thus you will get a third call to the destructor corresponding to this o6.
You can confirm this for yourself by adding a copy ctor as shown below:
class CreateSample
{
//other code here
public:
CreateSample(const CreateSample&obj): id(obj.id)
{
std::cout<<"Copy ctor called"<<std::endl;
}
};
And the output you will get is:
Object 5 is created <------ctor called for o5
Copy ctor called <------copy ctor called for parameter o
Copy ctor called <------copy ctor called for object o6
Object 5 is destroyed <------destructor called for o6
Object 5 is destroyed <------destructor called for o
Object 5 is destroyed <------destructor called for o5
Demo
Though in this particular example you don't strictly require a custom copy constructor or a custom copy assignment operator, they may be needed in other situations. Refer to the rule of three.
CreateSample o5(5); calls the constructor CreateSample(int). fuct2(o5); and CreateSample o6 = o; call the implicitly-defined default copy constructor CreateSample(CreateSample const&). All three of these variables (o6, o, and o5) call the destructor ~CreateSample() when their scope is exited.
The fix is to follow the rule of three and also define a copy constructor and copy-assignment operator:
class CreateSample
{
// ...
CreateSample(CreateSample const& o) {
id = o.id;
cout << "Object " << id << " is copy-constructed" << endl;
}
CreateSample& operator=(CreateSample const& o) {
cout << "Object " << id << " is copy-assigned from " << o.id << endl;
id = o.id;
return *this;
}
}
Demo on Compiler Explorer
I have the following code:
#include <iostream>
class foo_class {
std::string value;
public:
foo_class(const foo_class& v) : value{v.value} {
std::cout << "copy constructor" << std::endl;
}
foo_class(foo_class&& v) : value{std::move(v.value)} {
std::cout << "move constructor" << std::endl;
}
~foo_class() {
std::cout << "destructor" << std::endl;
}
foo_class(std::string v) : value{std::move(v)} {
std::cout << "type constructor" << std::endl;
}
};
struct Data {
foo_class a;
foo_class b;
};
int main() {
std::string c = "3";
Data x{c,c+"3"};
return 0;
}
IMPORTANT, I compile it with GCC and Clang (4.8.2 and 3.4 respectively) and the flag -fno-elide-constructors, so we don't elide the copy/move constructors.
The result of the execution is the following one:
type constructor
move constructor
destructor
type constructor
move constructor
destructor
destructor
destructor
Which means that the copy constructor is not being used at all, even when it should be used for the first argument of the constructor of the struct Data.
Next, If I delete the copy constructor, the code is still legal according to my compilers, but in my understanding should be illegal, cause I'm not casting to && (using std::move) when passing the first argument of the constructor of Data.
My question is simple:
Why is this happening?
Am I missing something?
Because the foo_class objects constructed in
Data x{c,c+"3"};
are both temporaries. So they invoke move constructor instead of copy constructor.
type constructor // 1. Construct a temporary foo_class object out of string "c" for a
move constructor // 2. Move the temporary object created above to x.a
destructor // 3. Destruct the temporary foo_class object created in 1
type constructor // 4. Same as 1, but the object is for b, and the string "33"
move constructor // 5. Same as 2, moved to x.b
destructor // 6. Destruct the temporary foo_class object created in 4
destructor // 7. Destruct x.b
destructor // 8. Destruct x.a
When i run the following program in XCode Version 5.1.1,
#include <iostream>
class MyClass
{
public:
MyClass() { std::cout << "MyClass Cons " << this << std::endl;}
~MyClass() { std::cout << "MyClass Dest " << this << std::endl;}
};
void Func(MyClass myClass)
{
}
int main(int argc, const char * argv[])
{
MyClass myClass1;
Func(myClass1);
return 0;
}
The output i get is
MyClass Cons 0x7fff5fbff918
MyClass Dest 0x7fff5fbff910
MyClass Dest 0x7fff5fbff918
Why is the destructor triggering twice and constructor only once?
The object is destroyed once, as you can see from the pointer values. You also see the destruction of another object. This object is a copy of the original.
By passing the object by value, the copy-constructor is invoked. Since this constructor does not print something, you do not see it in your output.
Add it to the class definition:
MyClass(const MyClass & other) { std::cout << "MyClass Copy-Cons " << this << " from " << &other << std::endl;}
And it should appear:
MyClass Cons 0x7fff1beee7ee
MyClass Copy-Cons 0x7fff1beee7ef from 0x7fff1beee7ee
MyClass Dest 0x7fff1beee7ef
MyClass Dest 0x7fff1beee7ee
The copy is created when you enter Func(). The copy is destroyed when it goes out of scope. This happens when you exit Func(). Finally, the original is destroyed when you exit the main() function.
The reason that you are seeing two destructor is because you are pass the arguments by value. Passing by value calls the copy constructor..
First Destructor:
At the end of the func function, the object goes out of scope, hence, it is destructed.
Second Destructor:
When the program end, the object is destructed.
My suggestion to remove the first destructor is to pass the object by reference rather than by value.
For example,
void Func(MyClass &myClass)
{
}
So now the output will have only:
MyClass Cons 0x7fff5fbff918
MyClass Dest 0x7fff5fbff918
Here the object is constructed twice(as everyone mentioned above). Hence 2 constructor + 2 destructor are called. The reason you are seeing 1 constructor is because you have NOT defined the Copy Constructor. CC is called when a copy is made for the pass-by-value operation.
add the copy constructor and then you can see both the constructor called.
Add Copy constructor - MyClass(const MyClass& obj) { std::cout << "MyClass Copy Cons " << this << std::endl;}
I wrote a small test program with a sample class containing also self-defined constructor, destructor, copy constructor and assignment operator. I was surprised when I realized that the copy constructor was not called at all, even though I implemented functions with return values of my class and lines like Object o1; Object o2(o1);
innerclass.hpp:
#include <iostream>
class OuterClass
{
public:
OuterClass()
{
std::cout << "OuterClass Constructor" << std::endl;
}
~OuterClass()
{
std::cout << "OuterClass Destructor" << std::endl;
}
OuterClass(const OuterClass & rhs)
{
std::cout << "OuterClass Copy" << std::endl;
}
OuterClass & operator=(const OuterClass & rhs)
{
std::cout << "OuterClass Assignment" << std::endl;
}
class InnerClass
{
public:
InnerClass() : m_int(0)
{
std::cout << "InnerClass Constructor" << std::endl;
}
InnerClass(const InnerClass & rhs) : m_int(rhs.m_int)
{
std::cout << "InnerClass Copy" << std::endl;
}
InnerClass & operator=(const InnerClass & rhs)
{
std::cout << "InnerClass Assignment" << std::endl;
m_int = rhs.m_int;
return *this;
}
~InnerClass()
{
std::cout << "InnerClass Destructor" << std::endl;
}
void sayHello()
{
std::cout << "Hello!" << std::endl;
}
private:
int m_int;
};
InnerClass innerClass()
{
InnerClass ic;
std::cout << "innerClass() method" << std::endl;
return ic;
}
};
innerclass.cpp:
#include "innerclass.hpp"
int main(void)
{
std::cout << std::endl << "1st try:" << std::endl;
OuterClass oc;
OuterClass oc2(oc);
oc.innerClass().sayHello();
std::cout << std::endl << "2nd try:" << std::endl;
OuterClass::InnerClass ic(oc.innerClass());
ic = oc.innerClass();
}
Output:
1st try:
OuterClass Constructor
OuterClass Copy
InnerClass Constructor
innerClass() method
Hello!
InnerClass Destructor
2nd try:
InnerClass Constructor
innerClass() method
InnerClass Constructor
innerClass() method
InnerClass Assignment
InnerClass Destructor
InnerClass Destructor
OuterClass Destructor
OuterClass Destructor
After some research I read that there is no guarantee that the compiler will use the explicitely defined copy constructor. I do not understand this behavior. Why does the copy constructor even exist then, if we do not know that it is called? How does the compiler decide if it uses it?
Or, even better, is there a way to force the compiler to use the self-defined copy constructor?
Just for completeness with the other answers, the standard allows the compiler to omit the copy constructor in certain situations (what other answers refer to as "Return Value Optimization" or "Named Return Value Optimization" - RVO/NRVO):
12.8 Copying class objects, paragraph 15 (C++98)
Whenever a temporary class object is copied using a copy constructor, and this object and the copy have the same cv-unqualified type, an implementation is permitted to treat the original and the copy as two different ways of referring to the same object and not perform a copy at all, even if the class copy constructor or destructor have side effects. For a function with a class return type, if the expression in the return statement is the name of a local object, and the cv-unqualified type of the local object is the same as the function return type, an implementation is permitted to omit creating the temporary object to hold the function return value, even if the class copy constructor or destructor has side effects. In these cases, the object is destroyed at the later of times when the original and the copy would have been destroyed without the optimization.
So in your innerClass() method, the copy constructor you might think would be called at the return is permitted to be optimized away:
InnerClass innerClass() {
InnerClass ic;
std::cout << "innerClass() method" << std::endl;
return ic; // this might not call copy ctor
}
I agree with Neil, you should not write a class which depends on the copy constructor being called. Namely because the compiler can do things like "Named Return Value Optimization" (link) which completely avoids the copy constructor in many return value scenarios. In order to enforce calling the copy constructor you'd need to write a lot of code aimed at "tricking" the C++ compiler. Not a good idea.
In a particular scenario though if you want to enforce calling the copy constructor, you can do an explicit call.
Object SomeFunc() {
Object o1 = ...
return Object(o1);
}
You should not design your class with a reliance on the copy constructor being called (or not called) in specific circumstances. The compiler is allowed to elide or add copy constructor calls at all sorts of places, and you really don't want to have to keep track of them.
As for why you need one - well, the compiler may decide it needs a copy, and the copy constructor is what it uses to do so. The advantage of copy constructor calls being elided is performance - copying is usually quite an expensive operation.
Is this the problem ?
OuterClass(const OuterClass & rhs)
{
std::cout << "OuterClass Constructor" << std::endl;
==>
std::cout << "OuterClass Copy Constructor" << std::endl;
}
OuterClass & operator=(const OuterClass & rhs)
{
std::cout << "OuterClass Constructor" << std::endl;
==>
std::cout << "OuterClass Assignment operator" << std::endl;
}
A copy paste error!
I would suggest you to debug the code once to see what exactly is happening. Debugging really helps you to find the problems.
EDIT: for inner class issue:
As others have already pointed out this is the case of The Name Return Value Optimization(NRVO).
InnerClass innerClass()
{
InnerClass ic;
std::cout << "innerClass() method" << std::endl;
return ic;
}
the compiler can transforms the function to
void innerClass( InnerClass &namedResult)
{
std::cout << "innerClass() method" << std::endl;
}
Thus it eliminates both the return by value of the class object and the need to invoke the class copy constructor.
Please go through below two links to understand more on NRVO:
MSDN NRVO
Stan Lippman's BLog
Object o1(); doesn't create any objects rather it defines a function prototype with function name o1, void arguments and return type as Object. You need to post some code to find the actual problem.
Post some code. I think you are using some incorrect syntax.
The copy constructor must exactly have the following signature:
MyObject(const MyObject&)
Then you can see if the copy constructor is called with the following code:
MyObject m1;
MyObject m2(m1);
You are not allowed to use MyObject m1(); that is a function declaration.