frined and operator new problem

Started by
6 comments, last by mike25025 19 years, 6 months ago
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?
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.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
im not trying to create a foo
Foo::New will call malloc
can someone please tell me why this doesnt work?
Works fine for me. What compiler are you using?
Kippesoep
msvc++ 2003
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.
Kippesoep
im stupid
i forgot to type unsigned in the definition of new

This topic is closed to new replies.

Advertisement