Connecting Tech Pros Worldwide Help | Site Map

How to get parent class' pointer

Paul
Guest
 
Posts: n/a
#1: Jul 19 '05
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
Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 19 '05

re: How to get parent class' pointer


"Paul" <pchoi181@yahoo.com> wrote...[color=blue]
> 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?[/color]

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


Jeremy Cowles
Guest
 
Posts: n/a
#3: Jul 19 '05

re: How to get parent class' pointer


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:
[color=blue]
> 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.[/color]

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


Victor Bazarov
Guest
 
Posts: n/a
#4: Jul 19 '05

re: How to get parent class' pointer


"Jeremy Cowles" <jeremy.cowles[nosp@m]asifl.com> wrote...[color=blue]
> I am moving from VB to C++, and I am just starting to learn about C++
> Classes.[/color]

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.
[color=blue]
> I am not sure I understand the following:
>[color=green]
> > 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.[/color]
>
> So, this does _not_ mean that Nested is in the scope of Enclosing?[/color]

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'.
[color=blue]
> So from
> global/file scope (not from inside the class Enclosing), how would you
> declare a variable of type Nested?
>
> Enclosing::Nested nested;[/color]

Sure. Qualified name should work anywhere.
[color=blue]
> or
> Nested nested;[/color]

Yes, if you're inside the scope of 'Enclosing' class.
[color=blue]
>
> or is it unavailable?[/color]

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

Victor


Victor Bazarov
Guest
 
Posts: n/a
#5: Jul 19 '05

re: How to get parent class' pointer


"Mike Smith" <mike_UNDERSCORE_smith@acm.DOT.org> wrote...[color=blue]
> Victor Bazarov wrote:
>[color=green]
> > "Paul" <pchoi181@yahoo.com> wrote...
> >[color=darkred]
> >>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?[/color]
> >
> >
> > 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.[/color]
>
> 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".[/color]

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


Victor Bazarov
Guest
 
Posts: n/a
#6: Jul 19 '05

re: How to get parent class' pointer


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

Good info. Off-topic, but good info, thanks.


Closed Thread