Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to get parent class' pointer

Reply
Thread Tools

How to get parent class' pointer

 
 
Paul
Guest
Posts: n/a
 
      08-11-2003
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-11-2003
"Paul" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Jeremy Cowles
Guest
Posts: n/a
 
      08-11-2003
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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-11-2003
"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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-11-2003
"Mike Smith" <(E-Mail Removed)> wrote...
> Victor Bazarov wrote:
>
> > "Paul" <(E-Mail Removed)> 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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-11-2003
"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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments