Re: Is it necessary to declare the private section of a C++ class?
James Kanze wrote:
On Sep 16, 8:21 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
Ian Collins wrote:
The same basic rules apply to C++ and C, declare in headers,
define in compilation units.
Moreover: Declare *the public interface* of your module in the
header, put everything else in the compilation unit.
The whole point of the question, I think, is that C++ doesn't
allow this. You can't reopen a class in the compilation unit in
order to add private members.
Well, I said "whenever possible and feasible". For technical reasons
private member variables cannot be moved to the compilation unit, but
there are many other situations where it is possible to do so.
For example, rather than doing this:
// MyClass.hh
class MyClass
{
private:
class InnerClass
{
// large declaration here
};
InnerClass* innerClassPtr;
};
you can instead do this:
// MyClass.hh
class MyClass
{
private:
class InnerClass;
InnerClass* innerClassPtr;
};
// MyClass.cc
class MyClass::InnerClass
{
// large declaration here
};
(Naturally the above is possible only if 'innerClassPtr' is a pointer
or reference. If it's a member object, then that technique cannot be
used, for technical reasons.)
Likewise the nameless namespace should be used as much as possible and
feasible, rather than putting everything in the header file.