Monday, October 21, 2013

A simpler Pimpl Idiom ?

Newsgroup: comp.lang.c++

Subject: A simpler Pimpl Idiom ?

From: mathieu <mathieu.malaterre@...>

Date: Mon, 21 Oct 2013 06:45:01 -0700 (PDT)



I have been reading "The Joy of Pimpls " [1], but this seems over-complicated IMHO. It requires a "back-pointer" just to split object in halves.



I am thinking of providing something similar (at least in my scenario), where I can hide the implementation completely.



I am thinking in something like this:



$ cat demo.cxx

// public stuff:

#include <iosfwd>



struct visible

{

public:

void print(std::ostream & os) const;

protected:

visible(){}

private:

visible(const visible &);

visible & operator = (const visible &);

};



visible * get_one();



// implementation details:

#include <iostream>



struct not_visible : public visible

{

public:

not_visible(int t, const char *v):tag(t),data(v){}

void print_impl(std::ostream & os) const

{

os << tag << " -> " << data << std::endl;

}

private:

int tag;

std::string data;

};



void visible::print(std::ostream & os) const

{

static_cast<const not_visible*>(this)->print_impl(os);

}



visible * get_one()

{

// dummy implementation just for the demo:

static not_visible v(123,"hello");

visible *ptr = &v;

return ptr;

}



int main()

{

visible *u = get_one();

u->print(std::cout);

return 0;

}





Does anyone sees anything wrong with this implementation ? How would one mark the class 'visible' an interface-only (it should not be derived in user applications).



Thanks,

-Mathieu



[1] http://www.gotw.ca/publications/mill05.htm







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?112529-A-simpler-Pimpl-Idiom&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

No comments:

Post a Comment