Topic: Suggestion: more flexible member funct
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/07/06 Raw View
In article 95Jul6162046@dopey.hut.fi, tkorvola@dopey.hut.fi (Timo Korvola) writes:
>
>I wonder if people think this extension would be useful enough to be
>submitted to the working group, and if so, how to submit it.
I've already shown how to get exactly the effect of adding private, hidden,
non-virtual member functions without affecting the class declaration
and without making those functions generally visible. No language
extension is needed. It isn't even less convenient. Here it is again:
class NoneOfYourBusiness; // all member functions and data are static
class MyClass {
friend NoneOfYourBusiness;
...
};
You can modify class NoneOfYourBusiness to your heart's content, adding
and deleting static members without requiring any change to the header
file for MyClass. Users of MyClass never know or care about the details
of NoneOfYourBusiness, or what changes occur. If you are concerned
about polluting the global namespace, make NoneOfYourBusiness a
nested class.
All the member functions of NoneOfYourBusiness, no matter what they are,
are friends of MyClass, and so have acces to all the same things that a
private member of MyClass does.
The only difference is that instead of calling these functions with the syntax
m.func();
you call them with the syntax
NoneOfYourBusiness::func(m);
That syntax is hidden from everything but the MyClass implementation
functions. The generated code for the two cases is identical for
most C++ implementations, except for the mangled names of the functions.
---
Steve Clamage, stephen.clamage@eng.sun.com
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/06/20 Raw View
>|> In article <TKORVOLA.95Jun8155440@dopey.hut.fi>,
>|> Timo Korvola <Timo.Korvola@hut.fi> wrote:
>|> >
>|> >I would like to hear whether people think the following extension to
>|> >C++ would be useful.
>|> >I think it should be made possible to declare private non-virtual member
>|> >functions outside of the class definition. E.g.
I thought about this problem, and it seems to me that you can already
get the desired effect. Declare a fake class as a friend of the class.
You can then add, remove, or modify static member functions of the fake class
however you want without affecting the declaration of the real class.
Since you only want to call these functions from the implementation file of
the real class, no other code ever needs to see the fake class definition
or know anything about it.
Example:
file MyClass.h:
===============
// this never needs to change
class MyClass {
friend class MyFriend;
int foo(int);
...
public:
...
};
MyClass implementation file:
============================
#include "MyClass.h"
class MyFriend {
public:
static int f1(MyClass*, int); // helper function
...
};
int MyClass::foo(int i)
{
return MyFriend::f1(this, i);
}
---
Steve Clamage, stephen.clamage@eng.sun.com
Author: linhd@bnr.ca (Linh Dang)
Date: 1995/06/20 Raw View
Steve Clamage (clamage@Eng.Sun.COM) wrote:
: >|> In article <TKORVOLA.95Jun8155440@dopey.hut.fi>,
: >|> Timo Korvola <Timo.Korvola@hut.fi> wrote:
: >|> >
: >|> >I would like to hear whether people think the following extension to
: >|> >C++ would be useful.
: >|> >I think it should be made possible to declare private non-virtual member
: >|> >functions outside of the class definition. E.g.
: I thought about this problem, and it seems to me that you can already
: get the desired effect. Declare a fake class as a friend of the class.
: You can then add, remove, or modify static member functions of the fake class
: however you want without affecting the declaration of the real class.
: Since you only want to call these functions from the implementation file of
: the real class, no other code ever needs to see the fake class definition
: or know anything about it.
I know another useful trick for non-ADT objects i.e. objects that are
always on the heap and copy semantic doesn't make sense (e.g. a node
in a tree). I've used it for a longtime, but now ppl start calling
their tricks `patterns' and give them funny name, so I decided to
called this `pattern':
THE ABSTRACT-CONCRETE CLASS
//
// This pattern is intended to enhance info-hiding for C++ concrete
// classes.
//
/* A.h */
class A : public Abase
{
A(A&);
protected:
A();
public:
virtual void foo() = 0;
virtual void bar() = 0;
static A* New();
};
/* A.cc */
class A_imp : public A
{
public:
virtual void foo();
virtual void bar();
//.... many more members
};
A*
A::New() { return new A_imp; }
... this trick is not very useful for other modern languages.
Modula-3's opaque-types make it completely useless...
---------
L.D.
: Example:
: file MyClass.h:
: ===============
: // this never needs to change
: class MyClass {
: friend class MyFriend;
: int foo(int);
: ...
: public:
: ...
: };
: MyClass implementation file:
: ============================
: #include "MyClass.h"
: class MyFriend {
: public:
: static int f1(MyClass*, int); // helper function
: ...
: };
: int MyClass::foo(int i)
: {
: return MyFriend::f1(this, i);
: }
: ---
: Steve Clamage, stephen.clamage@eng.sun.com