How can I call an enclosing class's function?

E

Eric A. Johnson

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
 
I

Ivan Vecerina

Eric A. Johnson said:
class ConsoleLib
{
public:
class ConsoleWindow
{
public:
ConsoleWindow(HANDLE Screen, COORD Start, COORD Size, bool Border);
~ConsoleWindow();
// etc.
}

For ConsoleWindow member functions to have access to private members
of ConsoleLib, you may want to make it a friend:
friend class ConsoleWindow;
void OutputString(char *String);
Note that this member function can only be called on an object/instance
of class ConsoleLib:
ConsoleLib object;
object.OutputString("Hello world");
If you want the function to be callable directly, you need
to declare it as static:
static void OutputString(char const* string);
//NB: note also the added 'const', you probably want to have it
// etc.
}
How would I call a ConLib function from a ConsoleWindow constructor
or function?
As usual, but note the above comment about 'static' members.
(if you come from a Java background, note that in C++ "inner classes"
do not store a pointer to an instance of the enclosing class).
On a side note, should the subclass ConsoleWindow be public or private?
private, unless code outside of class ConsoleLib needs to have
access to it.


hth -Ivan
 
C

Calum Grant

Eric said:
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?

In Java, you ConsoleWindow would be a nested class, and it does have
access to ConsoleLib. But this isn't Java.

In C++, you need a reference to your ConsoleLib in your ConsoleWindow.

class ConsoleLib
{
public:
class ConsoleWindow
{
ConsoleLib &m_lib;
public:
ConsoleWindow(ConsoleLib &lib, HANDLE Screen, COORD Start,
COORD Size, bool Border);
~ConsoleWindow();
// etc.
}
void OutputString(char *String);
// etc.
};
On a side note, should the subclass ConsoleWindow be public or private?

That depends on whether anything other than ConsoleLib needs to use it!
 
E

Eric A. Johnson

Ivan Vecerina said:
For ConsoleWindow member functions to have access to private members
of ConsoleLib, you may want to make it a friend:
friend class ConsoleWindow;

Note that this member function can only be called on an object/instance
of class ConsoleLib:
ConsoleLib object;
object.OutputString("Hello world");
If you want the function to be callable directly, you need
to declare it as static:
static void OutputString(char const* string);
//NB: note also the added 'const', you probably want to have it

As usual, but note the above comment about 'static' members.
(if you come from a Java background, note that in C++ "inner classes"
do not store a pointer to an instance of the enclosing class).

private, unless code outside of class ConsoleLib needs to have
access to it.
Great! Thank you very much for all of the tips! :)
 
E

Eric A. Johnson

Calum Grant said:
In Java, you ConsoleWindow would be a nested class, and it does have
access to ConsoleLib. But this isn't Java.

In C++, you need a reference to your ConsoleLib in your ConsoleWindow.

class ConsoleLib
{
public:
class ConsoleWindow
{
ConsoleLib &m_lib;
public:
ConsoleWindow(ConsoleLib &lib, HANDLE Screen, COORD Start, COORD
Size, bool Border);
~ConsoleWindow();
// etc.
}
void OutputString(char *String);
// etc.
};


That depends on whether anything other than ConsoleLib needs to use it!
Okay. Thank you for the advice. It is much appreciated!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top