|
|
How can I call an enclosing class's function?
Hi All,
I have a class, ConsoleWindow, that is a member of another class,
ConsoleLib, like so:
class ConsoleLib
{
public:
class ConsoleWindow
{
public:
ConsoleWindow(HANDLE Screen, COORD Start, COORD Size, bool Border);
~ConsoleWindow();
// etc.
}
void OutputString(char *String);
// etc.
}
How would I call a ConLib function from a ConsoleWindow constructor or
function? I'm trying to call OutputString from ConsoleWindow's constructor.
Is there any way of doing this?
On a side note, should the subclass ConsoleWindow be public or private?
Thanks,
Eric
|
|
0
|
|
|
|
Reply
|
nothere4 (32)
|
3/26/2005 2:05:35 AM |
|
In article <Pr31e.15549$C47.14730@newssvr14.news.prodigy.com>,
nothere@dontlookforme.com says...
> Hi All,
>
> I have a class, ConsoleWindow, that is a member of another class,
> ConsoleLib, like so:
> class ConsoleLib
> {
> public:
> class ConsoleWindow
> {
> public:
> ConsoleWindow(HANDLE Screen, COORD Start, COORD Size, bool Border);
> ~ConsoleWindow();
> // etc.
> }
> void OutputString(char *String);
> // etc.
> }
> How would I call a ConLib function from a ConsoleWindow constructor or
> function? I'm trying to call OutputString from ConsoleWindow's constructor.
> Is there any way of doing this?
Since it is public, and since it's declared in the header (the enclosing
class and the nested class share the same header) you should just be
able to call it - so long as you have an instance of ConsoleLib to call
it for!
Possibly the mistake you are making is that you are trying to simply
call the function without having an instance of ConsoleLib. Try the
following:
void ConsoleWindowFunction
{
ConsoleLib cl;
cl.OutputString( "MyString" );
}
Possibly you want to make OutputString() a static function of ConsoleLib
- in this case the function would be:
void ConsoleWindowFunction
{
ConsoleLib::OutputString( "MyString" );
}
Nested classes are the same as ordinary external classes in most ways.
They don't have any special access priviliges to the functions of the
enclosing class, but they can access public functions as normal. They
are one of C++'s less essential features.
> On a side note, should the subclass ConsoleWindow be public or private?
That depends on what you want it for. If nothing outside ConsoleLib
needs it, it should be private (or protected, if you want it to be
available to classes derived from ConsoleLib).
- Gerry Quinn
|
|
0
|
|
|
|
Reply
|
gerryq (1321)
|
3/27/2005 11:09:14 AM
|
|
|
1 Replies
36 Views
(page loaded in 0.057 seconds)
|
|
|
|
|
|
|
|
|