"Paul" <pc******@yahoo.com> wrote...
Here is what I am trying to do. I have a parent class calling a child
class. when one function in child class is called, i need to call
parent class' function. How can I get parent class' pointer?
First of all, try to understand that you're not in Java any more.
There is no concept of "parent class" in C++. If you're talking
of a nested class and enclosing class, then they are unrelated.
In the code:
struct Enclosing {
struct Nested {
void foo();
};
};
an object of type Enclosing does not contain a subobject Nested.
The relationship between them is purely descriptional. In the
code:
struct Enclosing {
struct Nested {
void foo();
};
Nested nested;
void bar();
};
'nested' is a data member of type Nested in an Enclosing object.
If, when executing Enclosing::bar(), you need to execute the
Nested::foo, and in that function you need to get to the object
of type Enclosing that called it, simply pass it as an argument:
void Enclosing::bar()
{
nested.foo(this);
}
[of course, 'Nested::foo' has to be corrected to accept one
argument, of type Enclosing* :
struct Enclosing {
struct Nested {
void foo(Enclosing*);
};
Nested nested;
void bar();
};
Victor