- 浏览: 389177 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
A class member operator new() can be overloaded. Provided that each delcaration has a unique paramter list. The first parameter of any class operator new() must always be a parameter of type size_t, for example.
class Screen { public : void *operator new (size_t); void *operator new(size_t, Screen*); };
and how you can use the the placement operator as follow.
void func(Screen * start) { Screen *p = new (Start) Screen; //... }
NOTE: it is also possible to overload the class member operaotr delete(). However, such an operator is never inovked from a delete expression. An overloaded operator delete() is only called implicitly by the implementation if the constructor called by a new expression (yes, this is not a typo) throw an expression.
given the expression,
Screen *ps = new (Start) Screen;
are as follow.
- It calls the class operaor new (size_t, Screen *)
- It then calls the default constructor for class Screen to initialize the object
- It then initializes ps with the address of the Screen object
Screen * ps = new (Start) Screen;
void operator delete(void *, Screen *)
If this operator is found under class Screen, it is called to deallocate the storage if the constructor called by the new expression throws an exception. If this operator delete is not found(), then no operator delete() is called.
Let 's see the
/** * file * Screen.h * * description: * this is the header file definition of the screen and the ScreenPtr */ class Screen { public: void move(int x, int y); /** * the placement new and delete operator * */ void *operator new(size_t ); void *operator new(size_t, Screen* ); /** the placement delete operator * * */ void operator delete(void *); void operator delete (void * , Screen *); Screen() { }; Screen(Screen *) { throw "Screen(Screen *); ";} private: protected: };
and below is the implementation code.
/** * file * Screen.h * * description: * this is the header file definition of the screen and the ScreenPtr */ #include "stdafx.h" #include "Screen.h" #include <iostream> #include <exception> using std::cout; using std::endl; using std::cerr; using std::exception; void * Screen::operator new(size_t size) { cout << "void * Screen::opreator new (size_t size) " << endl; return ::new char[size]; } void * Screen::operator new(size_t size, Screen* start) { cout << "void * Screen::operator new (size_t size, Screen* start) " << endl; if (start == NULL) // if you provide a placement new operator and the placement new operaor allocate memory, then you should provide a overloaded delete operator { throw new exception("bad argument - " "start"); } return start; } void Screen::operator delete(void * ptr) { cout << "void Screen::operator delete(void* start) " << endl; ::delete ptr; } void Screen::operator delete(void * ptr, Screen * start) { cout << "void * Screen::operator delete (void * ptr, Screen* start) " << endl; // do nothing... }
Below is the test code
/** * func(Screen* start) */ // what is steps of the calls of Screen* ps = new (start) Screen; // 1. it calls the class operaor new (size_t, Screen *) // 2. it then calls the default constructor for class Screen to initialize the object // 3. it then initializes ps with the address of the Screen object void func(Screen * start) { Screen* ps = new (start) Screen; delete ps; // it calles void Screen::operator delete(void * ptr) , not void Screen::operator delete(void * ptr, Screen start) } void func2() { Screen *ps = new Screen; delete ps; // it calls void Screen::operator delete(void * ptr) } void func3(Screen * start) { try { Screen *ps = new (start) Screen(start); delete ps; // this is never reached. } catch (const char *) { // you will probably see the "void Screen::operator delete(void * ptr, Screen * start)" called. } } void func4() { try { Screen *ps = new Screen(NULL); delete ps; // this line of code is never reached. } catch (const char *) { // you will probably see the "void Screen::operator delete(void * ptr)" called. } } int _tmain(int argc, _TCHAR* argv[]) { //test_use_of_ptr_overload(); Screen * screen = new Screen(); func(screen); func2(); screen = new Screen(); func3(screen); delete screen; func4(); return 0; }
and the output is like this:
void * Screen::opreator new (size_t size) void * Screen::operator new (size_t size, Screen* start) void Screen::operator delete(void* start) void * Screen::opreator new (size_t size) void Screen::operator delete(void* start) void * Screen::opreator new (size_t size) void * Screen::operator new (size_t size, Screen* start) void * Screen::operator delete (void * ptr, Screen* start) void Screen::operator delete(void* start) void * Screen::opreator new (size_t size) void Screen::operator delete(void* start)
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15283首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 964RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 744It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 919As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1031typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 735There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 783we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 913As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 721There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 930Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 923As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 949As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1285in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 902class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 956template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1180There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 800the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 553The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1133In my previous dicuss, we have ... -
c++ - overloaded subscript operator - []
2012-09-23 08:50 1126You can overload the subscript ...
相关推荐
OPTIMIZATION-of-the-APFs-Placement-Based-on-Instantaneous-Reactive-Power-Theory-by-GENETIC-ALGORITHM-master.zip 基于瞬时功率的全网布局无功补偿遗传算法优化
python库。 资源全名:osc-placement-1.5.0.tar.gz
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
new operator/delete operator就是new和delete操作符,而operator new/operator delete是函数。 new operator(1)调用operator new分配足够的空间,并调用相关对象的构造函数(2)不可以被重载 operator new(1)只...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装