Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) SmartPointerExample1

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppSmartPointerExample1/CppSmartPointerExample1.pro

 

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Werror
SOURCES += main.cpp

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

 

 

 

 

 

./CppSmartPointerExample1/main.cpp

 

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

struct Test { int m_x; };

int main()
{
  {
    std::unique_ptr<Test> p;
    assert(!p);
    p->m_x = 42; //Danger: uninitialized pointer not detected by std::unique_ptr
  }
  {
    std::shared_ptr<Test> p;
    assert(!p);
    p->m_x = 42; //Danger: uninitialized pointer not detected by std::shared_ptr
  }
  {
    boost::shared_ptr<Test> p;
    assert(!p);
    p->m_x = 42; //Good: uninitialized pointer detected by boost::shared_ptr
  }
  {
    boost::scoped_ptr<Test> p;
    assert(!p);
    p->m_x = 42; //Danger: uninitialized pointer not detected by boost::scoped_ptr
  }
}

 

 

 

 

 

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