By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 1,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

How to get parent class' pointer

P: n/a
Hi all,
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?
Thx
Paul
Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"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
Jul 19 '05 #2

P: n/a
Victor,

I am moving from VB to C++, and I am just starting to learn about C++
Classes. I am not sure I understand the following:
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.


So, this does _not_ mean that Nested is in the scope of Enclosing? So from
global/file scope (not from inside the class Enclosing), how would you
declare a variable of type Nested?

Enclosing::Nested nested;
or
Nested nested;

or is it unavailable?

Thanks,
Jeremy
Jul 19 '05 #3

P: n/a
"Jeremy Cowles" <jeremy.cowles[nosp@m]asifl.com> wrote...
I am moving from VB to C++, and I am just starting to learn about C++
Classes.
Sorry about my note on Java, then. There is a significant difference
in nested classes' implementation between those two. I suppose that
between VB and C++ there might be a similar one.
I am not sure I understand the following:
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.
So, this does _not_ mean that Nested is in the scope of Enclosing?


In scope, yes. However, unlike in Java (and I don't know about VB),
when you create an instance of 'Enclosing', C++ doesn't automatically
create an instance of 'Nested'.
So from
global/file scope (not from inside the class Enclosing), how would you
declare a variable of type Nested?

Enclosing::Nested nested;
Sure. Qualified name should work anywhere.
or
Nested nested;
Yes, if you're inside the scope of 'Enclosing' class.

or is it unavailable?


It's available alright. I described the difference above.

Victor
Jul 19 '05 #4

P: n/a
"Mike Smith" <mi*******************@acm.DOT.org> wrote...
Victor Bazarov wrote:
"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.


ICBW but I got the impression that what he's calling "parent" and
"child" would translate into "base" and "derived" in C++, not
"enclosing" and "nested".


I agree that "base-derived" is a more commonly accepted meaning of
"parent-child" than "enclosing-nested", however, there are others:
in a tree a node closer to the root is often called 'a parent', in
a process communication the one that initiates the other is a parent,
the initiated is the child, in windows-driven UI, windows are often
placed in a tree-like hierarchy as well, perhaps since there exist
so many different applications of the terms "parent-child", the C++
language definition doesn't make any use of it.

Victor
Jul 19 '05 #5

P: n/a
"Jeremy Cowles" <jeremy.cowles[nosp@m]asifl.com> wrote...
[...]
Just FYI, VB does not automatically create an instance for you, and based on what you said, VB works the same in terms of scope as well.


Good info. Off-topic, but good info, thanks.
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.