Re: public headers
iesvs@free.fr wrote:
I know it, I just think that if a member is private it must not be
present in headers.
I haven't read this whole back-and-forth, but I totally understand where
iesvs is coming from here. I've often wished I could declare just the public
part of a class in a header, and the private part elsewhere. And there's no
deep reason I shouldn't be able to do that; it's just a missing language
feature. C++ already supports incomplete struct/class types, which
correspond to the special case where everything is hidden. It could just as
well support partially complete types. I can even think of a reasonably
intuitive syntax for it that doesn't require any new keywords:
// header
class A {
public:
float f;
void method();
... // <- that's a literal '...'
};
// private implementation
class A : public Base {
public:
float f; // must match (enforced by implementation)
void method();
private:
int x;
};
void A::method() { ... }
The current syntax "class A;" would be identical to "class A { ... };".
I suspect, though, that if I proposed this to the standards committee,
they'd say "use virtual functions". And indeed you can get exactly the same
effect with virtual functions:
// header
class A {
public:
float f;
virtual void method() = 0;
};
// private implementation
class A_impl : public A, public Base {
public:
void method();
private:
int x;
};
void A_impl::method() { ... }
So this is what I recommend to the original poster. As far as I can tell, it
does exactly what you want. The only advantage of incomplete class
declarations would be efficiency: in a typical implementation they'd save
you a machine word per object and a hard-to-predict indirect jump per method
call.
-- Ben