getting a type ID during new operator overloading

Discussion in 'C++' started by bittercold@gmail.com, Mar 15, 2005.

  1. Guest

    Hi, guys,

    I need to overload global "new" operator but I have to know the type id
    "new" is begin invoked on.
    For example if I overload it like this

    void *operator new(size_t n)
    {
    ...body...
    }

    I can only use "n" to get the size of object of whatever type is being
    instantiated.
    Is it possible to know the type?

    thanks in advance
     
    , Mar 15, 2005
    #1
    1. Advertising

  2. <> wrote...
    > I need to overload global "new" operator but I have to know the type id
    > "new" is begin invoked on.


    Why? Isn't that what class-specific operator new is for?

    > For example if I overload it like this
    >
    > void *operator new(size_t n)
    > {
    > ...body...
    > }
    >
    > I can only use "n" to get the size of object of whatever type is being
    > instantiated.
    > Is it possible to know the type?


    No. Unless, of course, you somehow ensure that all your classes have
    different sizes...

    V
     
    Victor Bazarov, Mar 15, 2005
    #2
    1. Advertising

  3. Guest

    >Why? Isn't that what class-specific operator new is for?
    Class specific operator is no use here. Part of my program uses memory
    manager allocating memory by my_new(Type)(...constructor params...) for
    example
    my_new(int), my_new(MyClass)(123,"123")

    But part is just using new. It is too big to go and start changing it
    by hand, so I wanted to overload global new.

    void *operator new(size_t n)
    {
    my_new( <type should goes here> )
    }

    Problem is - I need type.
     
    , Mar 15, 2005
    #3
  4. wrote:
    >>Why? Isn't that what class-specific operator new is for?

    >
    > Class specific operator is no use here. Part of my program uses memory
    > manager allocating memory by my_new(Type)(...constructor params...) for
    > example
    > my_new(int), my_new(MyClass)(123,"123")


    Is this a macro of some kind? Are you implementing memory debugging that
    way? There are tools for that, you know...

    > But part is just using new. It is too big to go and start changing it
    > by hand,


    That's what search-and-replace functionality in modern text editors is
    for, isn't it?

    > so I wanted to overload global new.
    >
    > void *operator new(size_t n)
    > {
    > my_new( <type should goes here> )
    > }
    >
    > Problem is - I need type.
    >


    Sorry, no such thing available. I believe you're going to be better off
    doing the "changing by hand" thing. You need to only do it once, and when
    done, you can continue using your macro.

    V
     
    Victor Bazarov, Mar 15, 2005
    #4
  5. Guest


    >Is this a macro of some kind? Are you implementing memory debugging

    that
    >way? There are tools for that, you know...
    >That's what search-and-replace functionality in modern text editors is


    >for, isn't it?


    Whatever I am implementing, that was just an example.
    Don't try to be a smart ass and spare me your sarcasm. I asked a simple
    question. No need to tell me about text editors functionality. Thank
    you.
     
    , Mar 15, 2005
    #5
  6. <> wrote...
    > [...] Thank you.
    >


    You're most welcome.
     
    Victor Bazarov, Mar 16, 2005
    #6
    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. John Smith
    Replies:
    2
    Views:
    452
    Ivan Vecerina
    Oct 6, 2004
  2. Replies:
    11
    Views:
    770
    James Kanze
    May 16, 2007
  3. hurcan solter
    Replies:
    3
    Views:
    759
    Cholo Lennon
    Aug 29, 2007
  4. Martin T.
    Replies:
    7
    Views:
    840
    Martin T.
    Mar 10, 2008
  5. Replies:
    11
    Views:
    587
Loading...

Share This Page