小编Cur*_*is2的帖子

放置C++中的新功能

在阅读C++ Primer Plus时,我几乎没有关于新位置的问题.

书中的示例代码如下:

class JustTesting{
private:
  string words;
  int number;
public:
  JustTesting(const string & s = "Just Testing", int n = 0){
    number = n;
    words = s;
    //some code here
  }

  ~JustingTesting(){}
};


char * buffer = new char[BUF];   //get a block of memory
JustTesting *pc1, *pc2;

pc1 = new (buffer) JustTesting;  //Place object in buffer
pc2 = new JustTesting("Heap1",20); //Place object on heap

//some code

JustTesting *pc3, *pc4;

pc3 = new (buffer) JustTesting("Bad Idea", 6);
pc4 = …
Run Code Online (Sandbox Code Playgroud)

c++

4
推荐指数
1
解决办法
284
查看次数

为什么智能指针模板需要在构造函数中禁用异常抛出

我在阅读C++ primer plus中的智能指针模板类时发现了这样的问题.这本书给出了一个如何实现auto_ptr类的例子,像这样,

template<class X> class auto_ptr {
public:
    explicit auto_ptr(X* p = 0) throw();
...};
Run Code Online (Sandbox Code Playgroud)

构造函数结束时的throw()意味着此构造函数不会抛出异常.我知道这已被弃用,但我不知道为什么它需要禁用它的异常抛出.

c++ templates smart-pointers

1
推荐指数
1
解决办法
100
查看次数

标签 统计

c++ ×2

smart-pointers ×1

templates ×1