why does a class declaration include private members?

C

Corno

Hi all,

I was just wondering why a class declaration also includes the private
members of a class. Is this information needed by other classes for
compiling and/or linking or is this just an historical issue?
I can see why subtypes and friends need this information but what about
other related classes.
I would imagine that by declaring the members in a source file or at least
in another header file the 'public' header file would be more fit for the
role of abstract interface.

Corno
 
R

Rolf Magnus

Corno said:
Hi all,

I was just wondering why a class declaration also includes the private
members of a class. Is this information needed by other classes for
compiling and/or linking or is this just an historical issue?

Where would you put them instead? A second declaration for the private
part of the class? What about protected members? A third declaration?
I can see why subtypes and friends need this information but what
about other related classes.
I would imagine that by declaring the members in a source file or at
least in another header file the 'public' header file would be more
fit for the role of abstract interface.

It not only needed for subtypes and friends. Even parts of your code
that only use the public iterface need it. How else could e.g. a
function allocate the needed space for a local variable, if it doesn't
know the size of the class?
 
K

Klaus Eichner

Corno said:
Hi all,

I was just wondering why a class declaration also includes the private
members of a class. Is this information needed by other classes for
compiling and/or linking or is this just an historical issue?

A private declaration is just an access-restriction for the program, it
does not hide information. The compiler sees and requires private
members in exactly the same way as it sees and requires public
members.
I can see why subtypes and friends need this information but what about
other related classes.
I would imagine that by declaring the members in a source file or at least
in another header file the 'public' header file would be more fit for the
role of abstract interface.

Yes, abstract interfaces can be implemented in C++ by using a so called
"Pimpl-idiom", see http://www.gotw.ca/gotw/024.htm
 
C

Corno

Rolf Magnus said:
Where would you put them instead? A second declaration for the private
part of the class? What about protected members? A third declaration?


It not only needed for subtypes and friends. Even parts of your code
that only use the public iterface need it. How else could e.g. a
function allocate the needed space for a local variable, if it doesn't
know the size of the class?
Good point :)
Overlooked that obvious one!

Thanx,

Corno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,138
Messages
2,570,804
Members
47,349
Latest member
jojonoy597

Latest Threads

Top