| View previous topic :: View next topic |
| Author |
Message |
Risto Lankinen Guest
|
Posted: Wed Apr 21, 2004 3:41 pm Post subject: RFD: Enhanced class declarations |
|
|
Hi!
Classes can be declared without defining them:
class C;
Such a declared name can be used as a type name in other
declarations. It will also allow simple manipulation of actual
objects of that type, thru the use of pointers and references:
class C;
C *CreateC();
C *myfunc()
{
C *result = CreateC();
return result;
}
Sometimes it might enhance the information hiding if a
name could be declared to inherit a know interface that
also could be used to manipulate the incomplete types.
Another example follows:
class Base
{
public:
virtual void f();
};
class C : public Base; // This is the proposed feature
C *CreateC();
C *myfunc()
{
C *result = CreateC();
result->f(); // This would become possible
return result;
}
This way, the actual definition of class C could be entirely
hidden from the client application, provided that its actual
implementation eventually inherits from class Base.
Thoughts?
- Risto -
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
llewelly Guest
|
Posted: Thu Apr 22, 2004 3:17 pm Post subject: Re: RFD: Enhanced class declarations |
|
|
"Risto Lankinen" <rlankine (AT) hotmail (DOT) com> writes:
[snip]
| Quote: | class Base
{
public:
virtual void f();
};
class C : public Base; // This is the proposed feature
C *CreateC();
C *myfunc()
{
C *result = CreateC();
result->f(); // This would become possible
return result;
}
This way, the actual definition of class C could be entirely
hidden from the client application, provided that its actual
implementation eventually inherits from class Base.
Thoughts?
|
Right now we can do this:
/* Base.hh */
#ifndef BASE_HH
#define BASE_HH
class Base
{
public:
virtual void f()=0;
virtual ~Base();
};
class C : public Base{};
C *CreateC();
#endif
//BASE_HH
/* Base.cc */
#include"Base.hh"
#include<iostream>
#include<ostream>
Base::~Base(){}
class C_implementation : public C
{
/* Here is the implementation of C .
It is entirely hidden from the code in main.cc .*/
virtual void f(){std::cout << "C_implementation::f()" << std::endl;}
};
C *CreateC() { return new C_implementation;}
/* main.cc */
#include"Base.hh"
C *myfunc()
{
C *result = CreateC();
result->f();
return result;
}
int main()
{
myfunc();
}
So what advantages does your suggestion have over the above?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Krügler (nee Spang Guest
|
Posted: Fri Apr 23, 2004 12:27 am Post subject: Re: RFD: Enhanced class declarations |
|
|
Hello, Risto Lankinen!
Risto Lankinen schrieb:
[..]
| Quote: | Sometimes it might enhance the information hiding if a
name could be declared to inherit a know interface that
also could be used to manipulate the incomplete types.
Another example follows:
class Base
{
public:
virtual void f();
};
class C : public Base; // This is the proposed feature
C *CreateC();
C *myfunc()
{
C *result = CreateC();
result->f(); // This would become possible
return result;
}
This way, the actual definition of class C could be entirely
hidden from the client application, provided that its actual
implementation eventually inherits from class Base.
Thoughts?
|
I really don't see the advantage of your proposal. The usual C++
solution is as
follows:
class Base
{
public:
// Don't forget the virtual destructor here!!
virtual void f();
};
Base* CreateBase(); // Might create a Base* or any X*, where X
// inherits publicly from Base. But that fact need not to be mentioned
// explicitely.
Base *myfunc()
{
Base *result = CreateBase();
result->f(); // This is already possible
return result;
}
You could even make Base an abstract class by introduction of pure
virtual members to
express the impossibility to instantiate an Base object directly.
Your proposal shows a 'pattern' which does not decide what to do: Shall
I publish
an interface (The solution shown by me) or shall I publish a concrete
type? Usually you
either think in interfaces or in concrete types. I have never seen a
real-life example where
mixing of both techniques in the way you showed us, would provide
something much
more useful. Can you show us such a beast?
Greetings from Bremen,
Daniel Krügler
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jerald Fijerald Guest
|
Posted: Fri Apr 23, 2004 10:42 am Post subject: Re: RFD: Enhanced class declarations |
|
|
"Risto Lankinen" <rlankine (AT) hotmail (DOT) com> wrote
| Quote: | Sometimes it might enhance the information hiding if a
name could be declared to inherit a know interface that
also could be used to manipulate the incomplete types.
Another example follows:
class Base
{
public:
virtual void f();
};
class C : public Base; // This is the proposed feature
C *CreateC();
C *myfunc()
{
C *result = CreateC();
result->f(); // This would become possible
return result;
}
Thoughts?
|
Why not have:
B *CreateC (); // create a 'C' and upcast it to 'B'
B *myfunc ()
{
B *result = CreateC();
result->f();
return result;
}
The fact that the pointer is really a 'C' matters only for
translation units that know the declaration of class 'C'.
These units can say:
C *c = (C*) myfunc();
Where they use downcasting to convert the B to C.
Gerald
------
p[0].x=[0]p.x
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|