From 8624761690912169840
X-Google-Thread: f78e5,f6130266038a8c4d
X-Google-Attributes: gidf78e5,public
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.vmunix.org!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: Re: Private Methodes declared outside of the class
Date: Sat, 21 Aug 2004 22:19:52 GMT
Lines: 177
Sender: mail2news@demon.net
Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++)
Message-ID: <2opvdhFdeej3U1@uni-berlin.de>
References: <2oblfaF8nds3U1@uni-berlin.de> <cfvuts$sen$1@news1nwk.SFbay.Sun.COM> <2oih41Fb5pb5U1@uni-berlin.de> <8b42afac.0408200500.48e13c94@posting.google.com>
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 1093126797 27663 158.152.254.254 (21 Aug 2004 22:19:57 GMT)
X-Complaints-To: abuse@demon.net
NNTP-Posting-Date: Sat, 21 Aug 2004 22:19:57 +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.64-mulga_r1 (2004-01-11) on 
	mulga.cs.mu.OZ.AU
X-Orig-X-Trace: news.uni-berlin.de iA1zEv7Os3fCP1D25yb3XwMvqQws33KO3Btun19Hu3GqZgyfuo
X-Spam-Status: No, hits=-4.2 required=5.0 tests=BAYES_00,FROM_ENDS_IN_NUMS 
	autolearn=no version=2.64-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 i7LMJqig016179;
	Sun, 22 Aug 2004 08:19:52 +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++:1990

James Kuyper wrote:
>>>If you could declare member functions outside the class definition,
> 
> anyone could hijack the class (for good or bad purposes), or break the
> class invariants without anyone knowing.
I explained this already several times (including in the original post) 
but here goes again. A privat Methode can only be called from a public 
or protected one. A method that is public or protected is not private 
and therefor has to be in the class declaration. Here's a bit of code:

class A{
   int a;
};
void A::foo(){
   a=4;
}
int main(){
   A a;
   //a.foo();  error foo is private
}


 >But if it didn't have access to the class's private parts, it wouldn't
 >really be a member of that class.

I never said that you wouldn't have access to a class's private parts. 
Here is what I suggest:
1)A method can be declared inside a class declaration private, protected 
or public. Also it may be declared virtual, static or inline.
2)If a method is not declared inside the class but declared (or defined) 
outside of the class it is automaticaly assumed to be private, non 
virtual, non static and non inline.
3)The syntax for declaring/defining a method outside of the class is 
similar to declaring/defining a normal function except that the name of 
the method is preceded by class_name:: .
4)A friend may access all methods that have been declared when the 
compiler reaches the definition of the friend. If the friend is a class 
then it may access all of the methods that have been declared when the 
compiler reaches the definition of the method.

This means the follwing code would work:

class A{
   int a;
public:
   void do_stuff();
};
void A::foo();
void A::do_stuff(){
   foo();
}
void A::foo(){
   a=5;
}

and the following:

class B;
class A{
   friend class B;
   int a;
};
class B{
public:
   void do(A&);
}
void A::foo(){
   a=5;
}
void B::do(A&a){
   a.foo();
}

but not the following:

class A{
   int a;
};
void A::foo(){
   a=4;
}
int main(){
   A a;
   //a.foo();  error foo is private
}

the following is ok but does it do anything?:

class A{
   int a;
};
void A::foo(){
   a=4;
}
int main(){
   A a;
}

There's no public or protected method so foo can not be called. (You can 
program a hacking functions but you can not execute it.)

With this allowed you can minize the changes that are needed to a header 
when the implementation is changed:

//Header
class String{
   char*str;
   unsigned alloced_chars;
public:
   String();
   ~String();
   set_size(unsigned size);
};

//Old implementation unit
String::String():
   str(new char[1]),
   alloced_chars(1){
   str[0]=0;
}
String::~String(){
   delete[] str;
}
void String::set_size(unsigned size){
   delete[]str;
   alloced_chars=size+1;
   str=new char[alloced_chars];
   str[size]=0;
}

//New implementation
String::String():
   str(new char[1000]),
   alloced_chars(1000){
   str[0]=0;
}
String::~String(){
   delete[] str;
}
unsigned String::get_size(){
   unsigned size=0;
   while(str[size]!=0)
     ++size;
   return size;
}
void String::enlarge(){
   unsigned size=get_size();
   delete[]str;
   alloced_chars*=2;
   str=new char[alloced_chars];
   str[size]=0;
}
void String::set_size(unsigned new_size){
   if(alloced_chars<=new_size)
     enlarge();
   else
     str[new_size]=0;
}

(I know that that string class doesn't copy the string when enlarging 
but I want to keep the example small and simple)
And the whole thing without changing a single charachter in the whole 
header. Meaning no other implementation unit will be recompiled because 
we changed the implementation of the string class. Also the header does 
not contain more info than is needed. This makes parsing it a bit faster 
and makes it a bit simpler for the eye to read (less useless junk to 
ignore).

The alternativ would be to put everything into set_size, so that it 
would do more than just one thing and things would start to get messy.

---
[ 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                       ]



