M
mathieu
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:stream & 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:stream & os) const
{
os << tag << " -> " << data << std::endl;
}
private:
int tag;
std::string data;
};
void visible:rint(std:stream & 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
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:stream & 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:stream & os) const
{
os << tag << " -> " << data << std::endl;
}
private:
int tag;
std::string data;
};
void visible:rint(std:stream & 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