Jump to content
  • Advertisement
Sign in to follow this  
mike25025

frined and operator new problem

This topic is 5463 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!