By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,188 Members | 977 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,188 IT Pros & Developers. It's quick & easy.

How can I call an enclosing class's function?

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Eric A. Johnson" <no*****@dontlookforme.com> wrote in message
news:6r******************@newssvr14.news.prodigy.c om...
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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 23 '05 #2

P: n/a
Eric A. Johnson wrote:
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!
Thanks,
Eric

Jul 23 '05 #3

P: n/a

"Ivan Vecerina" <IN*************************@vecerina.com> wrote in message
news:d2**********@news.hispeed.ch...
"Eric A. Johnson" <no*****@dontlookforme.com> wrote in message
news:6r******************@newssvr14.news.prodigy.c om...
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.

Great! Thank you very much for all of the tips! :-)
hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form

Jul 23 '05 #4

P: n/a

"Calum Grant" <ca****@no.onetel.spam.com> wrote in message
news:rz***************@newsfe3-gui.ntli.net...
Eric A. Johnson wrote:
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!

Okay. Thank you for the advice. It is much appreciated!
Thanks,
Eric

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.