Topic: When does the compiler make the distinction between constructor calls and assigment operator calls?
Author: sbnaran@bardeen.ceg.uiuc.edu (Siemel Naran)
Date: 1999/04/23 Raw View
>"Ted" <mindtrek@netzero.net> writes:
>> When does the compiler make the distinction between constructor calls and
>> assignment operator calls?
Constructing an object from the result of a function call allows the
object to be constructed directly in the destination space. But
assigning to an object does not allow this optimization. Example:
X function() {
return X(3);
// return value optimization says that return object can be
// created directly in the return space
// otherwise, function creates local X object, then copies
// this object into the return space, then destroys the local
// without optimization: 1 calls to X copy ctor, 1 calls to X dtor
// with optimization: 0 calls to X copy ctor, 0 calls to X dtor
}
int main() {
X x1=function();
// return space of function() may be synonymous with 'x1'
// so if this optimization in effect
// and return value optimization in effect
// then 0 calls to X copy ctor, 0 calls to X dtor
X x2; // create a default object
x2=function();
// return space of function() may not be synonymous with 'x2'
// this is because there is already an object in 'x2'
// so it would need to be destructed before the new object can
// be constructed
// eg, with X==std::vector<int>,
// X() is a vector with space for 0 ints
// X(3) is a vector with space for 3 ints
// so the optimization couldn't possibly apply
// so with return value optimization in effect
// 1 calls to X copy ctor, 1 calls to X dtor
}
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Ted" <mindtrek@netzero.net>
Date: 1999/04/21 Raw View
When does the compiler make the distinction between constructor calls and
assignment operator calls? Also, when does the compiler make the distinction
between overloaded () operator calls and constructor calls with single
parameters?
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/04/22 Raw View
In article <924660335.999.100@news.remarQ.com>, Ted
<mindtrek@netzero.net> writes
>When does the compiler make the distinction between constructor calls and
>assignment operator calls? Also, when does the compiler make the distinction
>between overloaded () operator calls and constructor calls with single
>parameters?
If it needs to construct a new instance it calls a ctor, in all other
cases it must do something else. The only problem is when assignment
style initialisation is called which often requires two ctors (a plain
one and a copy ctor) where the copy ctor (providing it is accessible)
may be elided.
Of course the implementation of an operator <type> () function may
involve one or more ctors. However the difference between these
conversion operators and single parameter ctors is entirely clear, the
former convert from the class type and the latter converts into the
class type. If ever both are available I think you will get ambiguity.
Francis Glassborow Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 1999/04/22 Raw View
"Ted" <mindtrek@netzero.net> writes:
> When does the compiler make the distinction between constructor calls and
> assignment operator calls?
In construction, a new object comes into existance, and its address
starts being valid (i.e. its lifetime starts). Rules are complicated
what can be accessed at what time of construction, how virtual bases
are constructed, and what a virtual function call in a constructor
means.
In assignment, an existing object is changed. The object is completely
initialized throughout the assignment, virtual calls always go to the
most-derived class, and double-assignment of virtual bases should not
be a problem.
> Also, when does the compiler make the distinction between overloaded
> () operator calls and constructor calls with single parameters?
I don't understand. Why should
class X{
...
};
function(X(4));
be the same thing as
X x;
function(x(4));
??? The first calls function, passing a temporary object that was just
constructed; the other one creates an object and calls the object (not
the type), passing the result of that call to function.
Regards,
Martin
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1999/04/22 Raw View
Ted wrote:
>
> When does the compiler make the distinction between constructor calls and
> assignment operator calls? Also, when does the compiler make the distinction
> between overloaded () operator calls and constructor calls with single
> parameters?
struct T
{
T();
T(int);
// The default definitions of the copy constructor,
// assignment operator, and destructor are acceptable.
}
class U : public T
{
private:
T t;
public:
U( const T&t1 )
: T(t1) // Copy constructor
, t(2) // Conversion constructor
{};
}
class V {};
void funcin(T);
T funcout()
{
T a; // Default constructor
T b(a); // Copy constructor
T c=t; // Copy constructor
T d=T(3); // Conversion constructor, followed by copy ctor
T array[3]; // Default constructor
T other[4] = {3,2,1,0}; // Conversion constructor
T* pt;
try
{
pt = new T(4); // Copy constructor
}
catch(V v) // Copy constructor
{
throw(d); // Copy constructor
}
delete pt;
funcin(d); // Copy constructor.
c = static_cast<T>(5); // Conversion constructor
// followed by assignment operator
a = b; // Assignment operator.
return c; // Copy constructor.
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]