473,791 Members | 2,947 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(H ANDLE Screen, COORD Start, COORD Size, bool Border);
~ConsoleWindow( );
// etc.
}
void OutputString(ch ar *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
4 1692
"Eric A. Johnson" <no*****@dontlo okforme.com> wrote in message
news:6r******** **********@news svr14.news.prod igy.com...
class ConsoleLib
{
public:
class ConsoleWindow
{
public:
ConsoleWindow(H ANDLE 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(ch ar *String); Note that this member function can only be called on an object/instance
of class ConsoleLib:
ConsoleLib object;
object.OutputSt ring("Hello world");
If you want the function to be callable directly, you need
to declare it as static:
static void OutputString(ch ar 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
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(H ANDLE Screen, COORD Start, COORD Size, bool Border);
~ConsoleWindow( );
// etc.
}
void OutputString(ch ar *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(C onsoleLib &lib, HANDLE Screen, COORD Start,
COORD Size, bool Border);
~ConsoleWindow( );
// etc.
}
void OutputString(ch ar *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

"Ivan Vecerina" <IN************ *************@v ecerina.com> wrote in message
news:d2******** **@news.hispeed .ch...
"Eric A. Johnson" <no*****@dontlo okforme.com> wrote in message
news:6r******** **********@news svr14.news.prod igy.com...
class ConsoleLib
{
public:
class ConsoleWindow
{
public:
ConsoleWindow(H ANDLE 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(ch ar *String);

Note that this member function can only be called on an object/instance
of class ConsoleLib:
ConsoleLib object;
object.OutputSt ring("Hello world");
If you want the function to be callable directly, you need
to declare it as static:
static void OutputString(ch ar 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

"Calum Grant" <ca****@no.onet el.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(H ANDLE Screen, COORD Start, COORD Size, bool
Border);
~ConsoleWindow( );
// etc.
}
void OutputString(ch ar *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(C onsoleLib &lib, HANDLE Screen, COORD Start, COORD
Size, bool Border);
~ConsoleWindow( );
// etc.
}
void OutputString(ch ar *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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
5033
by: Charles Blaquière | last post by:
While noodling around, looking for a good layout for an "events calendar" page, I came upon a problem that has me stymied. Have a look at http://kpuc.org/events/upcoming-2.html . The basic structure of each event is: Event div Left-floated image Event header div
5
32729
by: Nik | last post by:
I keep getting the following error 'member names cannot be the same as their enclosing type' with the code below. public class HelpText { private string _textToDisplay; private string _cssClass; public string TextToDisplay { get {return _textToDisplay;}
1
24253
by: | last post by:
in sub class i can't define a constructor it give me error when i want it to... "category member names cannot be the same as their enclosing type" namespace Database { public class Structure { public class category {
6
1403
by: Karl Hungus | last post by:
Im using an xsl transformation so its not practical to use controls in the usual way, but I would still like to call methods in my codebehind class. usually Id just use onclick="<eventHandler>" say for something like a button control is there a way to manually call a method defined in a codebehind class, but is not called by a standard control? I looked at the __doPostBack() function that .net generates but it seemed impenetrable.
2
1342
by: MLH | last post by:
SendMailPlusWithOE Subject, Message, Recipients, Files, "08" I've seen the above syntax used to call the sub SendMailPlusWithOE Is it correct/incorrect/good practice/bad practice or just a matter of personal preference?
5
1473
by: blisspikle | last post by:
I figure that someone good at dotnet can look at this and give me a clue on how to easily organize this code? If there is a unique identifier like "Publisher" with a bunch of "Book" that are published under them (I used the arraylist class in the publisher class). How should the code be organized, and how can the books properties like "Name" be easily called in the main code, or searched for in the main code?
16
2615
by: DaTurk | last post by:
Hi, I have a c# application that needs to access c++ libraries, so it does this by using a managed layer of c++ CLI. Anyway, in the CLI function call, that calls the unmanaged function it expects a boolean return value, which it gets, but the value changes! I walked throuhg the code, and the unmanged code is returning false, but when I look at the variable in the managed code it's return value it true. Even after I initialized the...
11
2029
by: johnbrown105 | last post by:
Hello All, I am doing another exercise (I repeat, *exercise*). The (irrelevant to this discussion) point is to show that "You can inject a friend declaration into a namespace by declaring it within an enclosed class". I have done this successfully, but please consider the following program:
17
7150
by: =?Utf-8?B?SmltIFJvZGdlcnM=?= | last post by:
I am trying to replace a huge chunck of code that currently I incorporate with an #Include directive. The program rarely has to flow through that code, so I thought it would be better if I used Server.Execute. I found I can pass data TO the called asp file by creating an HTML <input> and erading it after I get there. However, I also need to read back ONE variable that the called file must set. I cannot figuer out how to do that. My...
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5430
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.