Connecting Tech Pros Worldwide Forums | Help | Site Map

Calling constructor explicitely

Gonçalo Rodrigues
Guest
 
Posts: n/a
#1: Jul 23 '05
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

Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Calling constructor explicitely


Gonçalo Rodrigues wrote:[color=blue]
> Is it possible to call a constructor of a class, call it Object,
> explicitely?[/color]

No.
[color=blue]
> 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?[/color]

Yes, it's called "placement new". Please read about it.
[color=blue]
> [..][/color]

V
Alf P. Steinbach
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Calling constructor explicitely


* Gonçalo Rodrigues:[color=blue]
>
> Is it possible to call a constructor of a class, call it Object,
> explicitely?[/color]

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

[color=blue]
> 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?[/color]

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".

[color=blue]
> If it's not possible or a "don't do that" it's no big deal.[/color]

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

There are numerous pitfalls.

Even experts get it wrong.

[color=blue]
> It is more
> of a curiosity question -- it would make a piece of my code simpler --
> than an actual specific need.[/color]

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!

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Alf P. Steinbach
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Calling constructor explicitely


* Victor Bazarov:[color=blue]
> Gonçalo Rodrigues wrote:[color=green]
> > Is it possible to call a constructor of a class, call it Object,
> > explicitely?[/color]
>
> No.[/color]

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

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Ioannis Vranos
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Calling constructor explicitely


Alf P. Steinbach wrote:
[color=blue]
> If you absolutely insist on construction in *ptr then you can
> include <new>, I think it was, and then write
>
> ::new(ptr) Object;[/color]


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



--
Ioannis Vranos

http://www23.brinkster.com/noicys
Alf P. Steinbach
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Calling constructor explicitely


* Ioannis Vranos:[color=blue]
> Alf P. Steinbach wrote:
>[color=green]
> > If you absolutely insist on construction in *ptr then you can
> > include <new>, I think it was, and then write
> >
> > ::new(ptr) Object;[/color]
>
>
> Why are you using the scope resolution operator here? Placement new is
> not hidden by another new in some local scope.[/color]

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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Gonçalo Rodrigues
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Calling constructor explicitely


On Wed, 23 Feb 2005 17:38:07 -0500, Victor Bazarov
<v.Abazarov@comAcast.net> wrote:
[color=blue]
>Gonçalo Rodrigues wrote:[color=green]
>> Is it possible to call a constructor of a class, call it Object,
>> explicitely?[/color]
>
>No.
>[color=green]
> > 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?[/color]
>
>Yes, it's called "placement new". Please read about it.
>[/color]

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

Best regards,
G. Rodrigues
Ron Natalie
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Calling constructor explicitely


Alf P. Steinbach wrote:[color=blue]
> * Gonçalo Rodrigues:
>[color=green]
>>Is it possible to call a constructor of a class, call it Object,
>>explicitely?[/color]
>
>
> Yes, but that is _not_ what you then clarify you're asking for.[/color]

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.
Alf P. Steinbach
Guest
 
Posts: n/a
#9: Jul 23 '05

re: Calling constructor explicitely


* Ron Natalie:[color=blue]
> Alf P. Steinbach wrote:[color=green]
> > * Gonçalo Rodrigues:
> >[color=darkred]
> >>Is it possible to call a constructor of a class, call it Object,
> >>explicitely?[/color]
> >
> >
> > Yes, but that is _not_ what you then clarify you're asking for.[/color]
>
> 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.[/color]

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)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Ron Natalie
Guest
 
Posts: n/a
#10: Jul 23 '05

re: Calling constructor explicitely


Alf P. Steinbach wrote:
[color=blue]
>
> 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,
>[/color]
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).
Closed Thread