Sign in to follow this  

frined and operator new problem

This topic is 4823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

This topic is 4823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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