inline void* __cdecl operator new(unsigned int size,char* file,
unsigned int line);
class Foo
{
protected:
static void* New(unsigned int size,char* file,
unsigned int line);
friend inline void* __cdecl operator new(unsigned int size,
char* file,unsigned int line);
};
inline void* __cdecl operator new(unsigned int size,char* file,
unsigned int line)
{
return Foo::New(size,file,line);
}
frined and operator new problem
when i try to compile this code i get the error:
Foo::New cannot access protected member in class Foo
why doesnt this work?
Yikes, that's not a good thing to do. You're overloading the global new to only create a Foo, so if you call new(__FILE__,__LINE__) NotFoo; you'll still be making a Foo. I think this is what you're actually after:
As for why what you have doesn't work, I'd say it's related to the fact that you're trying to declare operator new as __cdecl. I'd be surprised if that's valid.
class Foo{public: void* operator new(unsigned int size, char* file,unsigned int line);};inline void* Foo::operator new(unsigned int size,char* file, unsigned int line){ // Do stuff that was in Foo::New()}
As for why what you have doesn't work, I'd say it's related to the fact that you're trying to declare operator new as __cdecl. I'd be surprised if that's valid.
I've tried on MSVC6 and MSVC2K5 (Express beta). Both compile and run it just fine. Either 2K3 is doing something odd that its predecessor and successor don't, or there's something wrong in another part of your code.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement