473,569 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Temporary & default c'tor

I understand that a compiler synthesises a default constructor if none
is provided by the user ( of course depending on the situation if
synthesis of such c'tor is actually needed in the program e.g if vptr
is needed, default c'tor must be synthesised by compiler).

In the class Temporary below, there is no reason why a compiler should
synthesise a default c'tor.

class Temporary
{
public:
Temporary(const Temporary& a) { printf("copy ctor of Temporary invoked
\n"); }
Temporary(int a, int b){printf("in user defined ctor\n");}
~Temporary() { printf("in dtor of Temporary\n");}
};

Now, if I write a function like:

Temporary induceTemporary Generation()
{
Temporary t(8,9);
return t;
}

I get the output as:
[
in user defined ctor
copy ctor of Temporary invoked
in dtor of Temporary
in dtor of Temporary
]

In this scenario, I know that dtor is called twice because of a
temporary that is created by compiler, but what I dont understand is :
What type of CONSTRUCTOR does compiler use to create this temporary? I
have not provided any default c'tor here so I was hoping that compiler
will give me an error "No default c'tor found in class Temporary", but
this does not happen in Visual Studio 8 VC++ compiler. Why? Why? Why?

I thought of what would be going behind the scene based on the output
generated, and this is what I think might be happening: Please let me
know if my understanding is correct or not.

//Does compiler change it like this?
Temporary induceTemporary Generation( const Temporary& aTmp )
{
Temporary t(8,9); // in user defined ctor
aTmp.Temporary: :Temporary(t); //COMPILER ADDED?
return t;
}

//Does compiler augment the function like this?
void test_Temporary( )
{
Temporary tmp;
tmp.Temporary:: Temporary(); //Which constructor will be run on tmp
here?
Temporary f = induceTemporary Generation(tmp) ;
f.Temporary::Te mporary(tmp); // copy ctor of Temporary invoked
tmp.Temporary:: ~Temporary(); // in dtor of Temporary
f.Temporary::~T emporary(); // in dtor of Temporary
}

Thanks for your replies.
~Viren

May 17 '07 #1
3 2256
On 2007-05-17 08:27, RainBow wrote:
I understand that a compiler synthesises a default constructor if none
is provided by the user ( of course depending on the situation if
synthesis of such c'tor is actually needed in the program e.g if vptr
is needed, default c'tor must be synthesised by compiler).

In the class Temporary below, there is no reason why a compiler should
synthesise a default c'tor.

class Temporary
{
public:
Temporary(const Temporary& a) { printf("copy ctor of Temporary invoked
\n"); }
Temporary(int a, int b){printf("in user defined ctor\n");}
~Temporary() { printf("in dtor of Temporary\n");}
};

Now, if I write a function like:

Temporary induceTemporary Generation()
{
Temporary t(8,9);
return t;
}

I get the output as:
[
in user defined ctor
copy ctor of Temporary invoked
in dtor of Temporary
in dtor of Temporary
]

In this scenario, I know that dtor is called twice because of a
temporary that is created by compiler, but what I dont understand is :
What type of CONSTRUCTOR does compiler use to create this temporary?
Your output is quite clear, first an object is created using you user-
defined ctor, then a copy of that one is made using the copy-ctor. Both
of those objects are then destructed. No ctor other than those that you
have defined are used. The copy is made because you are returning t by
value, so a copy is made in the function that called
induceTemporary Generation().

Notice that as soon as you define *any* ctor the compiler will refrain
from creating default ones. So if you create a class with just a copy
ctor then you can't create an instance of this class since there's no
normal ctro.

--
Erik Wikström
May 17 '07 #2
On May 17, 2:27 am, RainBow <drvir...@gmail .comwrote:
class Temporary
{
public:
Temporary(const Temporary& a) { printf("copy ctor of Temporary invoked
\n"); }
Temporary(int a, int b){printf("in user defined ctor\n");}
~Temporary() { printf("in dtor of Temporary\n");}

};

Now, if I write a function like:

Temporary induceTemporary Generation()
{
Temporary t(8,9);
return t;

}

I get the output as:
[
in user defined ctor
copy ctor of Temporary invoked
in dtor of Temporary
in dtor of Temporary
]

In this scenario, I know that dtor is called twice because of a
temporary that is created by compiler, but what I dont understand is :
What type of CONSTRUCTOR does compiler use to create this temporary?
The destructor is being called twice - once in
induceTemporary Generation when t goes out of scope, and once in the
calling function when the returned object goes out of scope. The
returned object is a copy of t, generated by the copy constructor you
defined.

May 17 '07 #3
On May 17, 2:27 am, RainBow <drvir...@gmail .comwrote:
I understand that a compiler synthesises a default constructor if none
is provided by the user ( of course depending on the situation if
synthesis of such c'tor is actually needed in the program e.g if vptr
is needed, default c'tor must be synthesised by compiler).

In the class Temporary below, there is no reason why a compiler should
synthesise a default c'tor.

class Temporary
{
public:
Temporary(const Temporary& a) { printf("copy ctor of Temporary invoked
\n"); }
Temporary(int a, int b){printf("in user defined ctor\n");}
~Temporary() { printf("in dtor of Temporary\n");}

};

Now, if I write a function like:

Temporary induceTemporary Generation()
{
Temporary t(8,9);
return t;

}

I get the output as:
[
in user defined ctor
copy ctor of Temporary invoked
in dtor of Temporary
in dtor of Temporary
]

In this scenario, I know that dtor is called twice because of a
temporary that is created by compiler, but what I dont understand is :
What type of CONSTRUCTOR does compiler use to create this temporary? I
have not provided any default c'tor here so I was hoping that compiler
will give me an error "No default c'tor found in class Temporary", but
this does not happen in Visual Studio 8 VC++ compiler. Why? Why? Why?
The compiler generates a local using your parametized ctor and then
returns a copy by value. No other construction takes place. A copy
ctor is a constructor (and therefore will eventually induce
destruction). Try it without invoking the copy ctor.

Temporary induceTemporary Generation()
{
return Temporary(8,9);
}
>
I thought of what would be going behind the scene based on the output
generated, and this is what I think might be happening: Please let me
know if my understanding is correct or not.

//Does compiler change it like this?
Temporary induceTemporary Generation( const Temporary& aTmp )
{
Temporary t(8,9); // in user defined ctor
aTmp.Temporary: :Temporary(t); //COMPILER ADDED?
no
return t;
How is this value t returned? answer: by copy
}

//Does compiler augment the function like this?
void test_Temporary( )
{
Temporary tmp;
tmp.Temporary:: Temporary(); //Which constructor will be run on tmp
here?
Temporary f = induceTemporary Generation(tmp) ;
f.Temporary::Te mporary(tmp); // copy ctor of Temporary invoked
tmp.Temporary:: ~Temporary(); // in dtor of Temporary
f.Temporary::~T emporary(); // in dtor of Temporary

}

Thanks for your replies.
~Viren

May 17 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2856
by: solartimba | last post by:
Basic question: I am using STL for the first time, and I want to put the following class into a vector. What member functions/overloaded operators do I have to include? In general, when using STL, how do I answer this question? //****************** class cStockPrice { private: string date;
3
420
by: al | last post by:
class Base { public: //Base() {} Base(int m) {} }; class Derive : public Base {
12
4732
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
1
327
by: developereo | last post by:
Hi folks, Can somebody shed some light on this problem? class Interface { protected: Interface() { ...} virtual ~Interface() { ... } public:
22
2550
by: gillisct | last post by:
I'd like to say I have a basic control of the C language. Is there any reccomended reading upon completion of K&R? Are there any projects someone new to C could study/watch over/ and eventually contribute to in order to get a grip on the development process and/or expand their knowledge? Thank you.
43
3778
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because "there is no difference between a struct and a class except the public/private access specification" (and a few minor other things). When I create a...
12
7246
by: MC | last post by:
Hi all, I have write a simple program in c# that change proxy setting for IE7 by edit these registry keys: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings] "ProxyEnable"=dword:00000001 "ProxyServer"="localhost:8118" "DefaultConnectionSettings"=hex:46,00,00,00,43,05,00,00,03,00,00,00,0e,
26
2090
by: rajiv.battula | last post by:
Hey everyone, I am a Java programmer, somewhere between novice to advanced. I wanted to know if it is still recommended to read "The C Programming Language" 2nd Edition? This edition was released in 1988, it is now 2007, almost 20 years. Also, since it is so old, is there some supplemental tutorial recommend to go along with it? Thanks for...
10
2399
by: JosephLee | last post by:
In Inside C++ object Model, Lippman said there are four cases in which compile will sythesize a default constructor to initialize the member variables if the constructor is absent: 1. there is a virtual function; 2. virtual inheritance; 3.base class with explicit default constructor;
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8132
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7982
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.