473,761 Members | 10,280 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 27600
"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::Nest ed nested;
or
Nested nested;

or is it unavailable?

Thanks,
Jeremy
Jul 19 '05 #3
"Jeremy Cowles" <jeremy.cowle s[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::Nest ed 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.cowle s[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
4747
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
2673
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 option. Let's call the id of a class "cid" (for "class id"). The function signature should look like this: ******************************************
7
2192
by: David Sobey | last post by:
hi Here's some code: void SomeFunc(childClass arg); .... parentClass a; a=new childClass(); SomeFunc(a);
9
2084
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
7965
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 Child object goes out of scope? Specifically, I am dealing with a C library that provides a function that must be called to "destruct" a particular struct (this struct is dynamically allocated by another provided function). To avoid memory
2
5310
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 pointer to the parent node, the program will be inefficient and slow. It works fine at first. However, when I started to build the remove function, it destroys the tree when I delete a node. I already changed the parent pointer whenever I delete a...
3
11878
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 to-be-developed-function cmdOkay_Click() function as described below. 1. Create an user control that contains an OK button as below Public Class MyButton:System.Windows.Form.UserControl
3
1191
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( ). I want to create a child class of this parent class that does not have an Add( ) method (while possibly another child class does). I think I figured it out while writing this, so tell me if this is the 'standard method'. Make the parent class...
5
2094
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 reference that object. #include <string> #include <vector> #include <iostream>
0
9345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7332
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.