Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) PimplExample2

 

pimpl example 1: Lizard implementation in multiple files using boost::shared_ptr is a pimpl example.

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppPimplExample2/CppPimplExample2.pro

 

include(../../ConsoleApplication.pri)
include(../../Libraries/Boost.pri)
#include(../../Libraries/GeneralConsole.pri)

SOURCES += main.cpp \
    lizard.cpp

HEADERS += \
    lizard.h

 

 

 

 

 

./CppPimplExample2/lizard.h

 

#ifndef LIZARD_H
#define LIZARD_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <boost/shared_ptr.hpp>
#pragma GCC diagnostic pop

enum class Gender { male, female };

struct Lizard
{
  Lizard(const Gender gender) noexcept;
  Gender GetGender() const noexcept;
  private:
  struct LizardImpl;
  const std::shared_ptr<const LizardImpl> m_impl;
};

#endif // LIZARD_H

 

 

 

 

 

./CppPimplExample2/lizard.cpp

 

#include "lizard.h"

struct Lizard::LizardImpl
{
  LizardImpl(const Gender gender) noexcept;
  Gender GetGender() const noexcept;

  private:
  const Gender m_gender;
};

Lizard::Lizard(const Gender gender) noexcept
  : m_impl(std::make_shared<LizardImpl>(gender) )
{

}

Lizard::LizardImpl::LizardImpl(const Gender gender) noexcept
  : m_gender(gender)
{

}

Gender Lizard::LizardImpl::GetGender() const noexcept
{
  return m_gender;
}

Gender Lizard::GetGender() const noexcept
{
  return m_impl->GetGender(); //Forward to implementation
}

 

 

 

 

 

./CppPimplExample2/main.cpp

 

#include "lizard.h"

int main()
{
  const Lizard male(Gender::male);
  assert(male.GetGender() == Gender::male);
  const Lizard female(Gender::female);
  assert(female.GetGender() == Gender::female);
}

 

 

 

 

 

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