accessing class members from within a subclass

P

Philip Parker

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 )
 
P

Pete C.

Philip said:
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
 
P

Philip Parker

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 :/
 
P

Pete C.

(please don't top post. rearranged.)

<snip>

Philip said:
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
 

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
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top