BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #92207同步于 2016/6/13
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

真是让这个new给弄乱了

byr10086
2016/6/13镜像同步17 回复
先来个链接 http://www.cplusplus.com/reference/new/operator%20new/ 这是声明: throwing (1) void* operator new (std::size_t size); nothrow (2) void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) noexcept; placement (3) void* operator new (std::size_t size, void* ptr) noexcept; 这是example: // operator new example #include <iostream> // std::cout #include <new> // ::operator new struct MyClass { int data[100]; MyClass() {std::cout << "constructed [" << this << "]\n";} }; int main () { std::cout << "1: "; MyClass * p1 = new MyClass; // allocates memory by calling: operator new (sizeof(MyClass)) // and then constructs an object at the newly allocated space std::cout << "2: "; MyClass * p2 = new (std::nothrow) MyClass; // 这里是第18行 // allocates memory by calling: operator new (sizeof(MyClass),std::nothrow) // and then constructs an object at the newly allocated space std::cout << "3: "; new (p2) MyClass; //这里是第23行 // does not allocate memory -- calls: operator new (sizeof(MyClass),p2) // but constructs an object at p2 // Notice though that calling this function directly does not construct an object: std::cout << "4: "; MyClass * p3 = (MyClass*) ::operator new (sizeof(MyClass)); // allocates memory by calling: operator new (sizeof(MyClass)) // but does not call MyClass's constructor delete p1; delete p2; delete p3; return 0; } 第18行和第23行的使用new的方式和声明中的不一样啊??? 而且第23行的使用方式我在好多地方都遇到了,这到底是怎么回事啊???!!!
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
nuanyangyang机器人#1 · 2016/6/13
哪里不一样? 第23行是placement new
byr10086机器人#2 · 2016/6/13
【 在 nuanyangyang 的大作中提到: 】 : 哪里不一样? : 第23行是placement new new (p2) MyClass; 和 void* operator new (std::size_t size, void* ptr) noexcept; 的形式一样吗?
nuanyangyang机器人#3 · 2016/6/13
一样的。那个p2就作为ptr这个参数传入那个函数。
byr10086机器人#4 · 2016/6/13
【 在 nuanyangyang 的大作中提到: 】 : 一样的。那个p2就作为ptr这个参数传入那个函数。 但这里的new需要两个参数啊 p2只是作为第2个参数ptr 还有第一个参数size 呢?
nuanyangyang机器人#5 · 2016/6/13
【 在 byr10086 的大作中提到: 】 : : 但这里的new需要两个参数啊 p2只是作为第2个参数ptr 还有第一个参数size 呢? 隐含的。就是sizeof(MyClass)
xiaobing307机器人#6 · 2016/6/13
为了看得舒服点,贴一下好了 // operator new example #include <iostream> // std::cout #include <new> // ::operator new struct MyClass { int data[100]; MyClass() {std::cout << "constructed [" << this << "]\n";} }; int main () { std::cout << "1: "; MyClass * p1 = new MyClass; // allocates memory by calling: operator new (sizeof(MyClass)) // and then constructs an object at the newly allocated space std::cout << "2: "; MyClass * p2 = new (std::nothrow) MyClass; // 这里是第18行 // allocates memory by calling: operator new (sizeof(MyClass),std::nothrow) // and then constructs an object at the newly allocated space std::cout << "3: "; new (p2) MyClass; //这里是第23行 // does not allocate memory -- calls: operator new (sizeof(MyClass),p2) // but constructs an object at p2 // Notice though that calling this function directly does not construct an object: std::cout << "4: "; MyClass * p3 = (MyClass*) ::operator new (sizeof(MyClass)); // allocates memory by calling: operator new (sizeof(MyClass)) // but does not call MyClass's constructor delete p1; delete p2; delete p3; return 0; } 【 在 byr10086 的大作中提到: 】 : 先来个链接 http://www.cplusplus.com/reference/new/operator%20new/ : 这是声明: : throwing (1) void* operator new (std::size_t size); : ...................
byr10086机器人#7 · 2016/6/13
【 在 xiaobing307 的大作中提到: 】 : 为了看得舒服点,贴一下好了 : [code=c] : // operator new example : ................... 怎么做到的啊? 求教[ema11]
byr10086机器人#8 · 2016/6/13
new (p2) MyClass; 那这里后边怎么还跟了个 MyClass, 原型也不是这样的啊?
nuanyangyang机器人#9 · 2016/6/13
【 在 byr10086 的大作中提到: 】 : new (p2) MyClass; 那这里后边怎么还跟了个 MyClass, 原型也不是这样的啊? 这有什么问题吗?这就是语法啊 new (位置) 类名 或者 new (位置) 类名(构造函数参数)