From -2132609240777633037
X-Google-Thread: f78e5,f6130266038a8c4d,start
X-Google-Attributes: gidf78e5,public
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news1.google.com!news1.google.com!news.glorb.com!fr.ip.ndsoftware.net!proxad.net!proxad.net!194.159.246.34.MISMATCH!peer-uk.news.demon.net!kibo.news.demon.net!news.demon.co.uk!demon!stump.algebra.com!devnull
From: ben04_01@freenet.de (Ben Strasser)
Newsgroups: comp.std.c++
Subject: Private Methodes declared outside of the class
Date: Mon, 16 Aug 2004 14:48:32 GMT
Lines: 71
Sender: mail2news@demon.net
Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++)
Message-ID: <2oblfaF8nds3U1@uni-berlin.de>
NNTP-Posting-Host: news.news.demon.net
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.demon.co.uk 1092667723 27551 158.152.254.254 (16 Aug 2004 14:48:43 GMT)
X-Complaints-To: abuse@demon.net
NNTP-Posting-Date: Mon, 16 Aug 2004 14:48:43 +0000 (UTC)
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-User-Agent: Mozilla Thunderbird 0.7.3 (Windows/20040803)
X-Spam-Checker-Version: SpamAssassin 2.60-mulga_r1 (1.212-2003-09-23-exp) on 
	mulga.cs.mu.OZ.AU
X-Orig-X-Trace: news.uni-berlin.de LFbH46K4nNDkfkAgL7GYfg9pUwdLQX7zp3iA5jcim9U/5KpbHn
X-Spam-Status: No, hits=-4.2 required=5.2 tests=BAYES_00,FROM_ENDS_IN_NUMS 
	autolearn=no version=2.60-mulga_r1
X-Accept-Language: en-us, en
X-Received: (from fjh@localhost)
	by mulga.cs.mu.OZ.AU (8.12.10+Sun/8.12.9/Submit) id i7GEmWN1001068;
	Tue, 17 Aug 2004 00:48:32 +1000 (EST)
X-Path: comp-std-cpp-robomod!not-for-mail
X-Spam-Level: 
X-Delivered-To: std-c++@ucar.edu
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Newsgroups: comp.std.c++
Xref: g2news1.google.com comp.std.c++:1723

Hello,

I was wondering why you can not declare private methods outside of the 
class declaration?

When you write a C like interface, you declare the functions inside a 
header and implement them inside a compilation unit. These functions may 
get big and therefore it's a good idea to spilt them up into a few 
helper functions. These helpers are not part of the interface and 
therefore they are often static or in a namespace{} block. If the 
implementation of the functions change, some helper functions may no 
longer be needed or new ones are added. This is no problem because they 
are not declared in the header and therefore no other compilation units 
knows about them and therefore do not need to be recompiled.

Now when you are writing a C++ like interface (meaning a class) you put 
the declaration of the class and it's members inside the header. If they 
are public or protected they need to be there because those members are 
part of the interface. Private members are not part of the interface but 
part of the implementation, so other compilation units shouldn't know 
about them. They although have to know about data members because they 
need to know the total size of the class. Private methods although don't 
affect the size, so why are they there.

There are few situation where they have to be in the header. For example 
if they are virtual, or a friend of the class uses them or when a public 
inline method uses them. But what about plain ordinary helper methods 
for plain ordinary public methods which are defined in the 
implementation unit? These don't have to be known in each compilation 
unit that includes the header.

They even force recompilation when one is not needed. If the 
implementation of a class is changed but the data members, the public 
and protected methods stay the same then there is no need to recompile 
every unit that uses that class. If the implementation changes it is 
likely that new other helper methods are needed, these although are 
declared in the header so the header file has to be changed. Now the 
date of the last modification to the header file is changed and when 
compiling again, the compiler will think that the class has really been 
changed (because of the date of the header file) and recompile 
everything that includes that header.

So why not allow something like:

//header
class A{
public:
   void do_stuff();
};
//cpp
void A::helper(){
   //...
}
void A::do_stuff(){
   //...
   helper();
   //...
}

helper has not been declared in the declaration of A so it is 
automatically assumed to be private. This wouldn't allow to break into 
the class in an other compilation unit, you can declare and define a 
private method everywhere although you can only call it from a protected 
/ public method which has to be declared in the class declaration.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]



