Advertisement Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

122 Neutral

About SecondBest

  • Rank
  1. I want my global operator new to do it's stuff and then call whatever is supplied by the system (i.e. default new, other overloaded global new operators, class specific new operator).
  2. Hello All I overloaded the global operator new to act in a certain manner when provided with additional arguments. class key{}; void* operator new(size_t sz, key&) { //do some stuff return ::operator new(sz); }; This works fine, except for classes for which operator new is already overloaded. class test { public: void* operator new(size_t sz) { cout << "using custom new"<<endl; return ::operator new(sz); }; }; int main(int argc, char* argv[]) { k k1; int* p_int = new(k1) int;\\ok test* p_test1 = new test;\\ok test* p_test2 = new(k1) test;\\error test* p_test3 = ::new(k1) test;\\ok, but doesn't use test's operator new return 0; } I am compiling using VC++ 6 and the error I get is: error C2660: 'new' : function does not take 2 parameters Is there a way to support creating my custom new without affecting user class defined new? thanks
  3. SecondBest

    Inlined Singleton Getter

    If I understand your goal correctly then what you're trying to achieve can be done using a handle class: template<class T> class singleton_handle { T* get() { static T k_val; return &k_val; } public: operator T*(){return get();}; T* operator->(){return get();}; T& operator*(){return *get();}; }; struct X { int x; }; typedef singleton_handle<X> X_singleton; int main() { X_singleton A; A->x = 0; X_singleton B; ++(*B).x; // here A and B actually refer to the same object cout<< A->x<<endl;//should print 1; return 0; } The template functions should be easy for the compiler to inline, so it should not incur much (if any) overhead. This method allows you to USE class X as a singleton, and still allow others to use it in whatever manner they see fit. make sure to specialize the template for classes with no default constructors. I wrote this code off the top of my head to demonstrate the technique. I haven't really tested it so it might contain an error or seven. Use at your own risk. Peace Out!
  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. 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!