Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) SmartPointerExample2

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppSmartPointerExample2/CppSmartPointerExample2.pro

 

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra
unix {
  QMAKE_CXXFLAGS += -Werror
}

win32 {
  INCLUDEPATH += ../../Libraries/boost_1_54_0
}

SOURCES += main.cpp

 

 

 

 

 

./CppSmartPointerExample2/main.cpp

 

#include <memory>
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>

struct Test { int m_x; };

int main()
{
  {
    std::shared_ptr<Test> p { new Test };
    assert(p);
    //p.reset(nullptr); //Invalid: std::shared_ptr cannot be reset to nullptr
    //assert(!p);
  }
  {
    std::unique_ptr<Test> p { new Test };
    assert(p);
    p.reset(nullptr); //Valid: std::unique_ptr can be reset to nullptr
    assert(!p);
  }
  {
    boost::shared_ptr<Test> p { new Test };
    assert(p);
    //p.reset(nullptr); //Invalid: boost::shared_ptr cannot be reset to nullptr
    //assert(!p);
  }
  {
    boost::scoped_ptr<Test> p { new Test };
    assert(p);
    p.reset(nullptr); //Valid: boost::scoped_ptr can be reset to nullptr
    assert(!p);
  }
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml