Sign in to follow this  
mike25025

frined and operator new problem

Recommended Posts

mike25025    494
when i try to compile this code i get the error: Foo::New cannot access protected member in class Foo
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);
}

why doesnt this work?

Share this post


Link to post
Share on other sites
joanusdmentia    1060
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:


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.

Share this post


Link to post
Share on other sites
Kippesoep    892
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this