new ã§ããªãã¯ã©ã¹ãä½ããã
new ã§ããªãã£ãããã©ããã£ã¦ä½¿ãã®ãã£ã¦?
CannotNew o; // 使ãã CannotNew* p = new CannotNew; // 使ããªã (ããã«ããã) CannotNew* p = new CannotNew[3]; // 使ããªã (ããã«ããã)
ã¹ã¿ãã¯ä¸ã«ä½ãã®ã¯ãããã©ããã¼ãã«ä½ãã®ã¯ããã¦æ¬²ããã
C++ ã«ã¯æ¼ç®åã®å¤éå®ç¾©ã¨ããã¨ã°ãæ©è½ããã£ã¦ãæ¼ç®åãå®ç¾©ãç´ããã
new ãæ¼ç®åãªã®ã§å¤éå®ç¾©ã§ããã
class CanNew public: void* operator new(size_t sz) { return ::operator new(sz); } void* operator new[](size_t sz) { return ::operator new[](sz); } };
å¤éå®ç¾©ããåã¨åãåãããããã«ã¯ä¸ã®ããã«æ¸ã (ãã¶ã)
- æ»ãå¤ã¯
CanNew*
ã§ã¯ãªãvoid*
- å¼æ°ã¯
size_t
new CanNew
ããããã³ã³ãã¤ã©ããµã¤ãºãæ±ãã¦æ¸¡ãã¦ããã (ãã¶ã)
new ãå¼ã¹ãªãããã«ã¯ããããªæãã«æ¸ãã
class CannotNew { private: void* operator new(size_t); // å®ä½ã¯å®ç¾©ããªã void* operator new[](size_t); // å®ä½ã¯å®ç¾©ããªã };
ãã㧠new CannotNew
ããã¨ãã³ã³ãã¤ã©ããprivate ã ããå¼ããããã¡ã ããã£ã¦è¨ã£ã¦æãã¦ãããã
ã§ãã!!
â¦â¦ã¨æããã ::new CannotNew
ããã¨æ®éã« new ã§ãã¦ãã¾ãã
ããããã¢ã³ãããã®ã§è«¦ããã