473,473 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

accessing class members from within a subclass

hows it done? :/
i cant seem to access things in a "config" class i made
the setup is something like this

class IRCBot {
OPTIONS Config
IRCCONN Irc
....
}

each of these is a class of their own
such as

class OPTIONS {
std::string Quitmsg;
....
}

class IRCCONN {
// winsocks stuff in here
}
i can access things in the Config object easy enough from within functions
in IRCBot via Config.Quitmsg ... , but how do i access them from functions
within Irc? ( IRCCONN object )

Jul 22 '05 #1
3 1895
Philip Parker wrote:
hows it done? :/
i cant seem to access things in a "config" class i made
the setup is something like this

class IRCBot {
OPTIONS Config
IRCCONN Irc
...
}

each of these is a class of their own
such as

class OPTIONS {
std::string Quitmsg;
...
}

class IRCCONN {
// winsocks stuff in here
}
i can access things in the Config object easy enough from within
functions in IRCBot via Config.Quitmsg ... , but how do i access them
from functions within Irc? ( IRCCONN object )


You need to have pointers to the parent struct:
class IRCBot {
OPTIONS Config
IRCCONN Irc
IRCBot(){config.parent = this; Irc.parent = this;}
....
}

class OPTIONS {
std::string Quitmsg;
IRCBot* parent;
....
}

class IRCCONN {
// winsocks stuff in here
IRCBot* parent;
}

IRCCON::something()
{
std::cout << parent->Config.Quitmsg;
}

- pete
Jul 22 '05 #2
ive tried that but cant seem to get it to work, is there any special thing i
need to be doing, or just adding those lines as you gave? since it doesnt
seem to work :/
"Pete C." <x@x.x> wrote in message
news:Ra****************@newsread1.news.pas.earthli nk.net...
Philip Parker wrote:
hows it done? :/
i cant seem to access things in a "config" class i made
the setup is something like this

class IRCBot {
OPTIONS Config
IRCCONN Irc
...
}

each of these is a class of their own
such as

class OPTIONS {
std::string Quitmsg;
...
}

class IRCCONN {
// winsocks stuff in here
}
i can access things in the Config object easy enough from within
functions in IRCBot via Config.Quitmsg ... , but how do i access them
from functions within Irc? ( IRCCONN object )


You need to have pointers to the parent struct:
class IRCBot {
OPTIONS Config
IRCCONN Irc
IRCBot(){config.parent = this; Irc.parent = this;}
...
}

class OPTIONS {
std::string Quitmsg;
IRCBot* parent;
...
}

class IRCCONN {
// winsocks stuff in here
IRCBot* parent;
}

IRCCON::something()
{
std::cout << parent->Config.Quitmsg;
}

- pete

Jul 22 '05 #3
(please don't top post. rearranged.)
<snip>>>
You need to have pointers to the parent struct:

<snip>

Philip Parker wrote: ive tried that but cant seem to get it to work, is there any special
thing i need to be doing, or just adding those lines as you gave?
since it doesnt seem to work :/


Here is a complete compilable example illustrating it:

#include <iostream>
#include <string>

struct Parent;

struct Child1
{
Parent* parent;
void DoSomething();
};
struct Child2
{
Parent* parent;
std::string str;
};

struct Parent
{
Child1 c1;
Child2 c2;
Parent() {c1.parent = this; c2.parent = this;}
};

void Child1::DoSomething()
{
std::cout << parent->c2.str << std::endl;
}

int main()
{
Parent par;
par.c2.str = "Hello there.";
par.c1.DoSomething();

return 0;
}

- Pete
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
3
by: Jeff | last post by:
Hey asp.net 2.0 In the source I posted below, there is a GridView (look at the bottom of the script): <asp:GridView ID="gvwOnline" runat="server"> </asp:GridView> I'm trying to assign a...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
0
by: Hans Koller | last post by:
Hello group, I design a class to bind it to a property grid for easy modification of some settings. My problem is now that I want to raise an event when a settings has been changed. Thats not a...
6
by: Me | last post by:
I need to be able to acces non-virtual members of sublcasses via a base class pointer...and without the need for an explicit type cast. I thought a pure virtual getPtr() that acts as a type cast...
6
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a...
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
0
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,...
0
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
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...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.