- How can I call an enclosing class's function?
- Posted by Eric A. Johnson on March 26th, 2005
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
- Posted by Gerry Quinn on March 27th, 2005
In article <Pr31e.15549$C47.14730@newssvr14.news.prodigy.com> ,
nothere@dontlookforme.com says...
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.
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
- Now that's what I call... (Computers & Technology) by Kadaitcha Man
- pure virtual function call (Computers & Technology) by Dave
- Re: Messenger - Runtime error! R6025 -pure virtual function call. (MSN Messenger) by bbbruce
- calloc's, call by ref and function returning in C (Programming) by fade
- Re: 2nd call for help (Computers & Technology) by Boomer

