Topic: Constructor calls.
Author: Ivan Strougatski <strougatski@clara.net>
Date: 1999/10/15 Raw View
Steve Clamage wrote:
>
> Ivan Strougatski <strougatski@clara.net> writes:
>
> >Sorry, I did not make it exactly clear that all I want is to
> >apply a constructor on _already existing_ object in order to
> >bring it into original state. That, I believe, is impossible
> >to do in C++.
>
> You can do so with valid and well-defined code.
> Destroy the object, then use placement new:
>
> class T { ... };
>
> void foo()
> {
> T t1(args); // auto object
> t1.~T();
> new (&t1) T(args);
>
> static T t2(args); // static object
> t2.~T();
> new (&t2) T(args);
>
> T* p = new T(args); // heap object
> p->~T();
> new (p) T(args);
> }
As far as I can understand, this approach would not work, if
constructor has to be called from within the object.
[ 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: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/10/15 Raw View
Ivan Strougatski wrote:
>
> "Siemel B. Naran" wrote:
> >
> > On 13 Oct 1999 16:07:58 GMT, Ivan Strougatski <strougatski@clara.net> wrote:
> >
> > >C(x,y,z) would just call a method, while !!a.C(x,y,z) would create
> > >an new object a and initialize it with method C.
> >
> > What would the definition of member function or constructor C look like?
> >
> > void C::C(Thing x, Thing y, Thing z) : d_x(x), d_y(y), d_z(z) { }
> > // if C a constructor
> >
> > void C::C(Thing x, Thing y, Thing z) { d_x=x; d_y=y; d_z=z; }
> > // if C a member function
> >
> > We see that there are two definitions of C::C depending on whether
> > C is used as a member function or as a constructor.
> >
> > Notice that the second version actually works if C is used as a member
> > function or as a constructor! However, it is inefficient when used as
> > a constructor: the compiler default initializes d_x and d_y and d_z,
> > then copies x into d_x and y into d_y and z into d_z. Besides, what
> > would we do if d_x and d_y and d_z were const?
>
> Are you sure that this is part of the standard? Basically all my
> constructors are in the second form, yet, if called within
> the object, they create new instance. e.g
>
> class foo
> {
> foo(){...};
> foo(x,y,z){...);
> f1()
> {
> ...
> foo() // here new object is created
> ...
> }
> }
Then you obviously never used a member that was const, had no
default constructor or was a reference.
Try to write the Bar constructor given the following:
class Foo
{
public:
Foo(int) {}
};
class Bar
{
public:
Bar();
private:
Foo foo;
};
Or write the Foobar constructor:
class Foobar
{
public:
Foobar();
private:
int const i;
};
Note that you may not modify the class definitions.
---
[ 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: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/10/15 Raw View
Ivan Strougatski wrote:
>
> Steve Clamage wrote:
> >
> > Ivan Strougatski <strougatski@clara.net> writes:
> >
> > >Sorry, I did not make it exactly clear that all I want is to
> > >apply a constructor on _already existing_ object in order to
> > >bring it into original state. That, I believe, is impossible
> > >to do in C++.
> >
> > You can do so with valid and well-defined code.
> > Destroy the object, then use placement new:
> >
> > class T { ... };
> >
> > void foo()
> > {
> > T t1(args); // auto object
> > t1.~T();
> > new (&t1) T(args);
> >
> > static T t2(args); // static object
> > t2.~T();
> > new (&t2) T(args);
> >
> > T* p = new T(args); // heap object
> > p->~T();
> > new (p) T(args);
> > }
>
> As far as I can understand, this approach would not work, if
> constructor has to be called from within the object.
class T
{
void foo();
// ...
};
void T::foo()
{
this->~T();
new(this) T(args);
};
[ 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: clamage@eng.sun.com (Steve Clamage)
Date: 1999/10/14 Raw View
Ivan Strougatski <strougatski@clara.net> writes:
>Sorry, I did not make it exactly clear that all I want is to
>apply a constructor on _already existing_ object in order to
>bring it into original state. That, I believe, is impossible
>to do in C++.
You can do so with valid and well-defined code.
Destroy the object, then use placement new:
class T { ... };
void foo()
{
T t1(args); // auto object
t1.~T();
new (&t1) T(args);
static T t2(args); // static object
t2.~T();
new (&t2) T(args);
T* p = new T(args); // heap object
p->~T();
new (p) T(args);
}
--
Steve Clamage, stephen.clamage@sun.com
[ 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: Ivan Strougatski <strougatski@clara.net>
Date: 1999/10/15 Raw View
"Siemel B. Naran" wrote:
>
> On 13 Oct 1999 16:07:58 GMT, Ivan Strougatski <strougatski@clara.net> wrote:
>
> >C(x,y,z) would just call a method, while !!a.C(x,y,z) would create
> >an new object a and initialize it with method C.
>
> What would the definition of member function or constructor C look like?
>
> void C::C(Thing x, Thing y, Thing z) : d_x(x), d_y(y), d_z(z) { }
> // if C a constructor
>
> void C::C(Thing x, Thing y, Thing z) { d_x=x; d_y=y; d_z=z; }
> // if C a member function
>
> We see that there are two definitions of C::C depending on whether
> C is used as a member function or as a constructor.
>
> Notice that the second version actually works if C is used as a member
> function or as a constructor! However, it is inefficient when used as
> a constructor: the compiler default initializes d_x and d_y and d_z,
> then copies x into d_x and y into d_y and z into d_z. Besides, what
> would we do if d_x and d_y and d_z were const?
Are you sure that this is part of the standard? Basically all my
constructors are in the second form, yet, if called within
the object, they create new instance. e.g
class foo
{
foo(){...};
foo(x,y,z){...);
f1()
{
...
foo() // here new object is created
...
}
}
> --------------
> siemel b naran
> --------------
> ---
> [ 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 ]
---
[ 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: Ivan Strougatski <strougatski@clara.net>
Date: 1999/10/13 Raw View
I probably raised this question before, but I do insist that it
is rather important.
C(x,y,x) where C is a constructor would create new instance of a
class C. However, we must not forget that constructors are used not
only to reserve memory for new instance, but also bring this new
instance into valid state. So, would it not be reasonable to separate
these two actions (creation and initialization) or at least provide
some sort of syntax to allow this separation. Indeed, Eiffel, which
follows OO paradigm much closer then C++, have this separation:
C(x,y,z) would just call a method, while !!a.C(x,y,z) would create
an new object a and initialize it with method C.
[ 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: Ron Natalie <ron@sensor.com>
Date: 1999/10/13 Raw View
Ivan Strougatski wrote:
> C(x,y,x) where C is a constructor would create new instance of a
> class C. However, we must not forget that constructors are used not
> only to reserve memory for new instance, but also bring this new
> instance into valid state.
Constructors do not "reserve memory for a new instance." The memory
must already be in place before the constructor is called.
> So, would it not be reasonable to separate
> these two actions (creation and initialization) or at least provide
> some sort of syntax to allow this separation.
To a limited extent, placement new does this (allows initialization
of a suitable hunk of memory). Of course, you're on your own to
allocate such memory.
> Indeed, Eiffel, which> follows OO paradigm much closer then C++,
You think it is more OO to allow the existence of instances that
have not been initialized?
> C(x,y,z) would just call a method, while !!a.C(x,y,z) would create
> an new object a and initialize it with method C.
I'm confused. "just call a method" on what object. You don't call
constructors in C++ directly anyhow. For those who need to separate
some action from object creation, the answer is simple. Put the
separate action in a member function other than the 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 ]
Author: Hyman Rosen <hymie@prolifics.com>
Date: 1999/10/14 Raw View
Ivan Strougatski <strougatski@clara.net> writes:
> I probably raised this question before, but I do insist that it
> is rather important.
No, it is not.
> C(x,y,x) where C is a constructor would create new instance of a
> class C. However, we must not forget that constructors are used not
> only to reserve memory for new instance, but also bring this new
> instance into valid state. So, would it not be reasonable to separate
> these two actions (creation and initialization) or at least provide
> some sort of syntax to allow this separation. Indeed, Eiffel, which
> follows OO paradigm much closer then C++, have this separation:
> C(x,y,z) would just call a method, while !!a.C(x,y,z) would create
> an new object a and initialize it with method C.
C++ can do this as well, but it's not necessary very often.
Allocate first then construct using placement new -
template <typename T>
T *allocate_then_construct()
{
void *p = ::operator new(sizeof T); // allocate
return new(p) T(); // construct
}
---
[ 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: sbnaran@uiuc.edu (Siemel B. Naran)
Date: 1999/10/14 Raw View
On 13 Oct 1999 16:07:58 GMT, Ivan Strougatski <strougatski@clara.net> wrote:
>C(x,y,z) would just call a method, while !!a.C(x,y,z) would create
>an new object a and initialize it with method C.
What would the definition of member function or constructor C look like?
void C::C(Thing x, Thing y, Thing z) : d_x(x), d_y(y), d_z(z) { }
// if C a constructor
void C::C(Thing x, Thing y, Thing z) { d_x=x; d_y=y; d_z=z; }
// if C a member function
We see that there are two definitions of C::C depending on whether
C is used as a member function or as a constructor.
Notice that the second version actually works if C is used as a member
function or as a constructor! However, it is inefficient when used as
a constructor: the compiler default initializes d_x and d_y and d_z,
then copies x into d_x and y into d_y and z into d_z. Besides, what
would we do if d_x and d_y and d_z were const?
--
--------------
siemel b naran
--------------
---
[ 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: Ivan Strougatski <strougatski@clara.net>
Date: 1999/10/14 Raw View
Hyman Rosen wrote:
>
> Ivan Strougatski <strougatski@clara.net> writes:
> > I probably raised this question before, but I do insist that it
> > is rather important.
>
> No, it is not.
>
> > C(x,y,x) where C is a constructor would create new instance of a
> > class C. However, we must not forget that constructors are used not
> > only to reserve memory for new instance, but also bring this new
> > instance into valid state. So, would it not be reasonable to separate
> > these two actions (creation and initialization) or at least provide
> > some sort of syntax to allow this separation. Indeed, Eiffel, which
> > follows OO paradigm much closer then C++, have this separation:
> > C(x,y,z) would just call a method, while !!a.C(x,y,z) would create
> > an new object a and initialize it with method C.
>
> C++ can do this as well, but it's not necessary very often.
> Allocate first then construct using placement new -
>
> template <typename T>
> T *allocate_then_construct()
> {
> void *p = ::operator new(sizeof T); // allocate
> return new(p) T(); // construct
> }
Sorry, I did not make it exactly clear that all I want is to
apply a constructor on _already existing_ object in order to
bring it into original state. That, I believe, is impossible
to do in C++.
[ 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 ]