473,386 Members | 1,702 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,386 software developers and data experts.

Implicit Reference to parent class?

Is there any way in a generic class object to implicitly get a reference to
the class that instantiated that object ? Obviously I can pass it a
reference to store but it would be nice if there was an implicit Parent
property or something similar that gave you it "for free".
Nov 15 '05 #1
5 9287
JezB <je*************@blueyonder.co.yk> wrote:
Is there any way in a generic class object to implicitly get a reference to
the class that instantiated that object ? Obviously I can pass it a
reference to store but it would be nice if there was an implicit Parent
property or something similar that gave you it "for free".


Nope, there's nothing like that - good job too, otherwise it would be a
waste of space for the vast majority of cases, which don't require it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
I see your point ! I just wanted to be sure I wasnt missing something ...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
JezB <je*************@blueyonder.co.yk> wrote:
Is there any way in a generic class object to implicitly get a reference to the class that instantiated that object ? Obviously I can pass it a
reference to store but it would be nice if there was an implicit Parent
property or something similar that gave you it "for free".


Nope, there's nothing like that - good job too, otherwise it would be a
waste of space for the vast majority of cases, which don't require it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3
Actually, you can. In your constructor call:

System.Diagnostics.StackFrame frame = new StackFrame(2);
Type callingClass = frame.GetMethod().DeclaringType;

Note that this won't give you the instance that called your constructor, only the class. But that's what you asked for, so hopefully this will work for you. If this is a high-performance application, using Diagnostics may carry a higher cost than you want to pay. But otherwise, this should be fine. BTW, you may need to up the stack frame count (the argument to the StackFrame constructor) if there are more method calls between your constructor and the class that's creating it (e.g., if multiple constructors are called due to subclassing, etc). Hope this helps.

- Ben Blair
{My Name} {at} acm.org

----- JezB wrote: -----

I see your point ! I just wanted to be sure I wasnt missing something ...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
JezB <je*************@blueyonder.co.yk> wrote:
Is there any way in a generic class object to implicitly get a reference to the class that instantiated that object ? Obviously I can pass it a
reference to store but it would be nice if there was an implicit Parent
property or something similar that gave you it "for free".
Nope, there's nothing like that - good job too, otherwise it would be a

waste of space for the vast majority of cases, which don't require it.
--

Jon Skeet - <sk***@pobox.com>> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #4
Actually, you can. In your constructor call:

System.Diagnostics.StackFrame frame = new StackFrame(2);
Type callingClass = frame.GetMethod().DeclaringType;

Note that this won't give you the instance that called your constructor, only the class. But that's what you asked for, so hopefully this will work for you. If this is a high-performance application, using Diagnostics may carry a higher cost than you want to pay. But otherwise, this should be fine. BTW, you may need to up the stack frame count (the argument to the StackFrame constructor) if there are more method calls between your constructor and the class that's creating it (e.g., if multiple constructors are called due to subclassing, etc). Hope this helps.

- Ben Blair
{My Name} {at} acm.org

----- JezB wrote: -----

I see your point ! I just wanted to be sure I wasnt missing something ...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
JezB <je*************@blueyonder.co.yk> wrote:
Is there any way in a generic class object to implicitly get a reference to the class that instantiated that object ? Obviously I can pass it a
reference to store but it would be nice if there was an implicit Parent
property or something similar that gave you it "for free".
Nope, there's nothing like that - good job too, otherwise it would be a

waste of space for the vast majority of cases, which don't require it.
--

Jon Skeet - <sk***@pobox.com>> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #5
Ben Blair <an*******@discussions.microsoft.com> wrote:
Actually, you can. In your constructor call:

System.Diagnostics.StackFrame frame = new StackFrame(2);
Type callingClass = frame.GetMethod().DeclaringType;

Note that this won't give you the instance that called your
constructor, only the class. But that's what you asked for,
so hopefully this will work for you.


Except it might not work, because of inlining. All in all, it's a
pretty dodgy thing to rely on, IMO. Far better is to either remove the
need from the design in the first place, or get the caller to pass in a
reference to themselves (or their type, if that's all that's required).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Paul | last post by:
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'...
5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
9
by: Martin Herbert Dietze | last post by:
Hello, I would like to implement a callback mechanism in which a child class registers some methods with particular signatures which would then be called in a parent class method. In...
2
by: Jim Red | last post by:
hello first of all, i know, there are no classes in javascript. but i will use that word for better understanding of my question. here we go. i have three classes and need a reference to the...
6
by: jalkadir | last post by:
Let's say that I have this class: class Parent{ private: char* str; public: const char* getStr(){return str;} }; And then I create a child class class Child{ private: std::string str;...
6
by: marco_segurini | last post by:
Hi, the following sample code shows a compiler error I get trying to build some old code with the last CL compiler (vers 13.10.3077): //----- begin #include <iostream> namespace ns {
7
by: msxkim | last post by:
How to execute functions in the parent class first and then functions in the child class? For example, I have a parent class with functions 'ONE' and 'TWO' and child class has a function 'THREE'. ...
6
by: Sashi | last post by:
class parent{ Parent(){}; ~Parent(){}; } Child: public Parent{ Child(){}; ~Child(){};
1
by: =?Utf-8?B?R2FsYWhhZA==?= | last post by:
I have an ascx file that inherits an abstract class. Within this abstract class are protected properties. I can access these properties within the page_load event of the parent class, however when...
4
by: Fuzz13 | last post by:
I'm trying to instantiate an object of an inherited class "Chimp" from the parent class "Animal". The Animal class has a constructor that allows for an int legcount, string skintype, string talk, and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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.