Calling constructor explicitely

V

Victor Bazarov

Gonçalo Rodrigues said:
Is it possible to call a constructor of a class, call it Object,
explicitely?
No.

e.g. suppose you have a void pointer ptr pointing to a
block of memory big-enough to hold an Object. Is there a way to
initialize the region pointed to by ptr by somehow calling one of
Object's constructors?

Yes, it's called "placement new". Please read about it.

V
 
G

Gonçalo Rodrigues

Hi all,

Is it possible to call a constructor of a class, call it Object,
explicitely? e.g. suppose you have a void pointer ptr pointing to a
block of memory big-enough to hold an Object. Is there a way to
initialize the region pointed to by ptr by somehow calling one of
Object's constructors?

If it's not possible or a "don't do that" it's no big deal. It is more
of a curiosity question -- it would make a piece of my code simpler --
than an actual specific need.

With my best regards,
G. Rodrigues
 
A

Alf P. Steinbach

* Gonçalo Rodrigues:
Is it possible to call a constructor of a class, call it Object,
explicitely?

Yes, but that is _not_ what you then clarify you're asking for.

e.g. suppose you have a void pointer ptr pointing to a
block of memory big-enough to hold an Object. Is there a way to
initialize the region pointed to by ptr by somehow calling one of
Object's constructors?

Yes, there are two ways.

If you're not picky about having that pointer in the first place, but
any region of memory is okay, then you can use std::vector's "safe"
functionality for this, namely the default value argument which invokes
your object's copy constructor.

If you absolutely insist on construction in *ptr then you can
include <new>, I think it was, and then write

::new(ptr) Object;

which in common-speak is called "placement new".

If it's not possible or a "don't do that" it's no big deal.

It's a "don't do that".

There are numerous pitfalls.

Even experts get it wrong.

It is more
of a curiosity question -- it would make a piece of my code simpler --
than an actual specific need.

Describe your problem and/or post your code; it's very likely that there
is at least one coding or design level solution that's infinitely better!
 
I

Ioannis Vranos

Alf said:
If you absolutely insist on construction in *ptr then you can
include <new>, I think it was, and then write

::new(ptr) Object;


Why are you using the scope resolution operator here? Placement new is
not hidden by another new in some local scope.
 
A

Alf P. Steinbach

* Victor Bazarov:

I especially like the standard's phrasing, "explicit constructor
calls do not yield lvalues": it must be true, if they don't exist.
 
A

Alf P. Steinbach

* Ioannis Vranos:
Why are you using the scope resolution operator here? Placement new is
not hidden by another new in some local scope.

Well I'm not absolutely sure about that, but what I was thinking of was
the possibility for class Object to define a void* placement new operator; it
can do that because only the global one is forbidden fruit.
 
G

Gonçalo Rodrigues

Yes, it's called "placement new". Please read about it.

Thanks, it's exactly what I was looking for. I just had a "Duh!"
moment.

Best regards,
G. Rodrigues
 
R

Ron Natalie

Alf said:
* Gonçalo Rodrigues:



Yes, but that is _not_ what you then clarify you're asking for.

Actually, it is NOT possible to call the constructor explicitly
at all, either for the this or any other purpose. Constructors
are called for you by the implementation as part of object creation.
They don't participate in name lookup, you can't get a pointer, to
them, there's no syntax to call them.
 
A

Alf P. Steinbach

* Ron Natalie:
Actually, it is NOT possible to call the constructor explicitly
at all, either for the this or any other purpose. Constructors
are called for you by the implementation as part of object creation.
They don't participate in name lookup, you can't get a pointer, to
them, there's no syntax to call them.

I'm happy with non-religious terminology, and it doesn't really matter
much to me that the standard also employs it (see reply to Victor);
now please stop confusing readers of this ng with religious stuff.

Cheers,

- Alf (nothing personal, just business)
 
R

Ron Natalie

Alf said:
I'm happy with non-religious terminology, and it doesn't really matter
much to me that the standard also employs it (see reply to Victor);
now please stop confusing readers of this ng with religious stuff.

Cheers,
Cheers. It's not "religious terminology", it is being
correct. And it is confusing to users to imply that they can call
the constructor directly (as this user already seemed to think that
it was possible).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top