getting a type ID during new operator overloading

B

bittercold

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
 
V

Victor Bazarov

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
 
B

bittercold

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

Victor Bazarov

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
 
B

bittercold

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.
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top