How to get parent class' pointer

P

Paul

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
 
V

Victor Bazarov

Paul said:
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
 
J

Jeremy Cowles

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
 
V

Victor Bazarov

Jeremy Cowles said:
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:


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
 
V

Victor Bazarov

Mike Smith said:
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
 
V

Victor Bazarov

Jeremy Cowles said:
[...]
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top