c++ - Pimpl, private class forward declaration, scope resolution operator -
consider these 2 classes employ pimpl idiom:
classa: pimpl class forward declaration , variable declaration on separate lines
classa.h:
#include <memory> class classa { public: classa(); ~classa(); void setvalue( int value ); int getvalue() const; private: class classa_impl; // ^^^^^^^^^^^^^^ class forward declaration on own line std::unique_ptr<classa_impl> m_pimpl; // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ variable declaration on own line //edit: //even if use raw pointer instead of smart pointer, //i.e. instead of declaring smart pointer above, if declare: classa_impl *m_pimpl; //the situation in *.cpp files , questions (below) same. };
classa.cpp:
#include "classa.h" class classa::classa_impl { public: void setvalue( int value ); int getvalue() const; private: int value_; }; // private class implementation void classa::classa_impl::setvalue( int value ) { value_ = value; } int classa::classa_impl::getvalue() const { return value_; } // classa implementation classa::classa() : m_pimpl( new classa_impl() ) {} classa::~classa() {} void classa::setvalue( int value ) { m_pimpl->setvalue( value ); } int classa::getvalue() const { return m_pimpl->getvalue(); }
classb: pimpl class forward declaration , variable declaration on 1 line
classb.h:
#include <memory> class classb { public: classb(); ~classb(); void setvalue( int value ); int getvalue() const; private: std::unique_ptr<class classb_impl> m_pimpl; // ^^^^^^^^^^^^^^^^^^ class forward declaration // combined variable declaration on 1 line, // in 1 shot. //edit: //even if use raw pointer instead of smart pointer, //i.e. instead of declaring smart pointer above, if declare: class classb_impl *m_pimpl; //the situation in *.cpp files , questions (below) same. };
classb.cpp:
#include "classb.h" class classb_impl { public: void setvalue( int value ); int getvalue() const; private: int value_; }; // private class implementation void classb_impl::setvalue( int value ) { value_ = value; } int classb_impl::getvalue() const { return value_; } // classb implementation classb::classb() : m_pimpl( new classb_impl() ) {} classb::~classb() {} void classb::setvalue( int nvalue ) { m_pimpl->setvalue( nvalue ); } int classb::getvalue() const { return m_pimpl->getvalue(); }
questions:
why combining forward declaration , variable declaration on 1 line in classb.h require
classb_impl
"unscoped" in implementation of private class in classb.cpp?i.e. in classa.cpp, private class method definitions begin with
void classa::classa_impl::foo() {...
but in classb.cpp, private class method definitions begin with
void classb_impl::foo() {...
what implications of each method? 1 better?
(follow-up question in response galik's answer)
when combine forward declaration of class , declaration of variable of class in 1 statement...
//one-liner class classb_impl *m_pimpl;
...what called? there name kind of combined statement? , why doesn't
classb_impl
become inner class ofclassb
result of such statement?compare to...
//two-liner class classa_impl; classa_impl *m_pimpl;
...in case
classa_impl
does become inner class ofclassa
.why one-liner put
classb_impl
global namepace, while two-liner putsclassa_impl
classa
's namespace? why different?
why combining forward declaration , variable declaration on 1 line in classb.h require classb_impl "unscoped" in implementation of private class in classb.cpp?
because in first example declare classa_impl
inner class of classa
.
when declare classb_impl
in template parameter not part of classb
.
what implications of each method? 1 better?
this matter of opinion. think inner classes messy , harder work little reward.
my preferred method uses separate interface class helps reduce number of times have redeclare interface.
Comments
Post a Comment