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 induceTemporaryGeneration()
{
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 induceTemporaryGeneration( 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 = induceTemporaryGeneration(tmp);
f.Temporary::Temporary(tmp); // copy ctor of Temporary invoked
tmp.Temporary::~Temporary(); // in dtor of Temporary
f.Temporary::~Temporary(); // in dtor of Temporary
}
Thanks for your replies.
~Viren 3 2248
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 induceTemporaryGeneration()
{
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
induceTemporaryGeneration().
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
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 induceTemporaryGeneration()
{
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
induceTemporaryGeneration 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.
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 induceTemporaryGeneration()
{
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 induceTemporaryGeneration()
{
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 induceTemporaryGeneration( 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 = induceTemporaryGeneration(tmp);
f.Temporary::Temporary(tmp); // copy ctor of Temporary invoked
tmp.Temporary::~Temporary(); // in dtor of Temporary
f.Temporary::~Temporary(); // in dtor of Temporary
}
Thanks for your replies.
~Viren
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: al |
last post by:
class Base
{
public:
//Base() {}
Base(int m) {}
};
class Derive : public Base
{
|
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() {}
};
|
by: developereo |
last post by:
Hi folks,
Can somebody shed some light on this problem?
class Interface {
protected:
Interface() { ...}
virtual ~Interface() { ... }
public:
|
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...
|
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...
|
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]...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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,...
| |