Calling constructor explicitely

Discussion in 'C++' started by Victor Bazarov, Feb 23, 2005.

  1. Gonçalo Rodrigues wrote:
    > 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
     
    Victor Bazarov, Feb 23, 2005
    #1
    1. Advertising

  2. 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
     
    Gonçalo Rodrigues, Feb 23, 2005
    #2
    1. Advertising

  3. * 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!

    --
    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, Feb 23, 2005
    #3
  4. Alf P. Steinbach wrote:

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



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Feb 23, 2005
    #4
  5. * Victor Bazarov:
    > Gonçalo Rodrigues wrote:
    > > Is it possible to call a constructor of a class, call it Object,
    > > explicitely?

    >
    > No.


    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?
     
    Alf P. Steinbach, Feb 23, 2005
    #5
  6. * Ioannis Vranos:
    > Alf P. Steinbach wrote:
    >
    > > 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.


    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?
     
    Alf P. Steinbach, Feb 23, 2005
    #6
  7. On Wed, 23 Feb 2005 17:38:07 -0500, Victor Bazarov
    <> wrote:

    >Gonçalo Rodrigues wrote:
    >> 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.
    >


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

    Best regards,
    G. Rodrigues
     
    Gonçalo Rodrigues, Feb 23, 2005
    #7
  8. Victor Bazarov

    Ron Natalie Guest

    Alf P. Steinbach wrote:
    > * 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.


    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.
     
    Ron Natalie, Feb 23, 2005
    #8
  9. * Ron Natalie:
    > Alf P. Steinbach wrote:
    > > * 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.

    >
    > 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)

    --
    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, Feb 23, 2005
    #9
  10. Victor Bazarov

    Ron Natalie Guest

    Alf P. Steinbach wrote:

    >
    > 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).
     
    Ron Natalie, Feb 24, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amelyan
    Replies:
    0
    Views:
    391
    Amelyan
    May 24, 2005
  2. Phaedrus

    Explicitely calling Thread.run

    Phaedrus, Jul 5, 2003, in forum: Java
    Replies:
    3
    Views:
    478
    Roedy Green
    Jul 6, 2003
  3. mathieu
    Replies:
    4
    Views:
    404
    Kai-Uwe Bux
    Aug 12, 2006
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,323
  5. jacob navia
    Replies:
    3
    Views:
    575
    Nick Keighley
    Feb 24, 2010
Loading...

Share This Page