473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to get parent class' pointer

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
5 27563
"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
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
"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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jacek Dziedzic | last post by:
Hi! Say a have a class called black_box which, among other things, contains an array of cells like this: class black_box { private: // ... public: black_box_details_struct details;
16
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an...
7
by: David Sobey | last post by:
hi Here's some code: void SomeFunc(childClass arg); .... parentClass a; a=new childClass(); SomeFunc(a);
9
by: jon wayne | last post by:
OK! I had this nagging doubt Consider (without worrying abt access specifiers) class Kid : public Parent{...}; Parent::someFunc() { Kid k; }
6
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a...
2
by: dannielum | last post by:
Hi all, I am trying to write a Binary Search Tree that each of its node will have 3 node pointers: left, right and parent. I need a parent pointer for some the purpose of my project. Without the...
3
by: David N | last post by:
Hi All, I just wonder if in C#, I can develop a user defined control that can call its parent function which is not yet developed. For example, how do I make my user control call a...
3
by: Peteroid | last post by:
Is it possible to make a public parent class method unavailable (i.e., generate an error at compile time) to a particular child class? For example, say a parent class has a public method Add( )....
5
by: cmk128 | last post by:
Hi All Is there any alternative way to let parent class hold a vector of its child class's objects? I meant when i create an object from the child class, in the parent class, i am able to...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.