Archived

This topic is now archived and is closed to further replies.

Krakken

Classes

Recommended Posts

Krakken    122
Hi, I have a similar situation to the one below. Better Example: ------------------------------------ CHANGED class PARENT { public: class NESTED { public: AccessParentClass() { ClassToAccess->Function(); }; }; protected: CLASSEX *ClassToAccess; }; ------------------------------------ CHANGED What I want to know is, how do I access "VariableOne" from "FunctionTwo()"? Thanks. [edited by - Krakken on August 7, 2003 12:10:42 PM]

Share this post


Link to post
Share on other sites
UberGeek    138
First, it seems you have a class declaration inside a seperate class declaration....which is rather interesting. But anyways...you use the friend keyword, which grants a function/class the ability to acces protected and private members. Example:

class myClass
{
protected:
int myProtectedVar;

friend int main();
}

int main()
{
myClass test;

test.myProtectedVar = 1;
}

Normally I wouldn''t be able to access that variable in int main(), but I can because I''ve declared it a friend. You can declare an entire class as a friend too. Just use friend class [name]. Note that the class you are declaring a friend must be already declared.

Note: A class is automatically friends with itself. So if I have a class member that accepts a instance of my own class as an argument, I can access the arguments private members.

Share this post


Link to post
Share on other sites
superpig    1825
Nesting classes like that is fine - it helps avoid cluttering the global namespace - but aside from the way you reference the class (ONE::TWO rather than just TWO), they''re normal classes and should be seen as such. ONE cannot automatically access protected/private members of TWO and vice versa.

You need friend declarations, or public getter/setter methods. Depends on what you want to do with the variable.

The alternative is that you store the variable in a third class, completely out of the public eye, and then somehow setup TWO with a pointer to it. If you then force TWOs to be created through a ONE, that creation function could construct TWO with a pointer to that ONE''s ''private block.'' Uhm. Like this:


class THREE
{
public:
int someVar;
};

class ONE
{
public:
class TWO
{
public:
TWO(THREE &thr) : t(thr) {}
int SomeFunc() { t.someVar++; }
private:
THREE &t;
}
TWO CreateTwo() { return new TWO(t); }
private:
THREE t;
}

void main()
{
ONE o;
TWO *t=o.CreateTwo();
t->SomeFunc();
delete t;
}


Something like that. It''s not pretty.

Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.
Enginuity1 | Enginuity2 | Enginuity3 | Enginuity4

Share this post


Link to post
Share on other sites
Krakken    122
Hi thanks for our replies but I don''t think I understand this fully. Could you please give me a complete example as I would need it? By the way the thing I need to access isn''t actually a variable it is indeed another class. :/

Better Example:

class PARENT {
public:

class NESTED {
public:
AccessParentClass() { ClassToAccess->Function(); };
};

protected:
CLASSEX *ClassToAccess;
};


That is almost exactly what I am trying to do. If someone could pleas just fill in the gaps to make it work?

Share this post


Link to post
Share on other sites
rypyr    252
You don't need friend declarations if the inner class does not need to access non-public methods of the parent class. What you need is to provide the inner class object with a pointer to the parent class.


class Parent {
class Inner {
Parent* m_pParent;
public:
Inner() : m_pParent(0) {}
Inner(Parent* parent) : m_pParent(parent) {cout << "Inside Inner(parent)" << endl;}
~Inner() { cout << "Inside ~Inner()" << endl; }
void blah() {
cout << "Inside Inner::blah()" << endl;
m_pParent->bloo();
}
};

void bloo() { cout << "Inside Parent::bloo()" << endl; }
void func() {
Inner inner1(this);
inner1.blah();
}
};



Now try this:

Parent p;
p.func();

Regards,
Jeff

[edited by - rypyr on August 7, 2003 12:36:53 PM]

Share this post


Link to post
Share on other sites
Krakken    122
I don''t really want to do that if possible. I want my wrapper to take care of everything without having to reference things etc.

Share this post


Link to post
Share on other sites
Sneftel    1788
You do have access to that member variable. HOWEVER, your example makes no sense. Here''s why.



class Car
{
public:
class Ignition
{
void TurnOnCar() { engineRunning = true; }
};
private:
bool engineRunning;
};

So what''s wrong with this picture? Here''s what''s wrong. Watch this code:

Car::Ignition myIgnition;
myIgnition.TurnOnCar();

Well, great..... but WHAT car? Objects of a class B declared within a class A can''t access member variables of a particular object of class A, simply because there''s no particular A to bind to. In this case, we never even made a Car.

Try something like this:

class Car
{
public:
class Ignition
{
public:
Ignition(Car &myCar) : car(myCar) { }
void TurnOnCar() { car.engineRunning = true; }
private:
Car &car;
};
private:
bool engineRunning;
};


Now an Ignition MUST be bound to a particular car at creation time. Notice that Ignition automatically gets access to engineRunning because it''s declared within Car; no friend specifier is necessary.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites