Inheritance Question

  • Thread starter Thread starter Eric
  • Start date Start date
E

Eric

Hi,

I have a base class with a method that need to determine which derived class
(child class) called it. How can I do this?

Thanks
 
Hi,

I have a base class with a method that need to determine which
derived class (child class) called it. How can I do this?

Eric,

"this" is the instance of the calling class, and "this.GetType()"
will return the type of the calling class.


Hope this helps.

Chris.
 
Hi Eric,

Though you can do as Chris describes, you might like to consider whether you
have correctly encapsulated your child class functionality. Code that
explicitly examines type can often be generalised via a virtual method.

Cheers

Doug Forster
 
Doug said:
Hi Eric,

Though you can do as Chris describes, you might like to consider whether you
have correctly encapsulated your child class functionality. Code that
explicitly examines type can often be generalised via a virtual method.

Cheers

Doug Forster
I agree. If you need to find the "derived" class from a base class, you
have something in base class that is not generalized enough. If you can,
you might want to also attempt delegation pattern instead of inheritence
which gives you better control over things.

Or wait till generics come out. If its implemented in any manner like
VC++, you can probably use the great ATL trick of using the child class
as an input template argument..
 
Thanks for all your posts. I will have to look at my design and see if I
can change it. Here is what I am doing: I have a overrided method called
for each derived class and in each class which does something particular to
the derived class and it then needs to call a method in base which needs to
know which derived class called it.

Thanks
 
Eric said:
Thanks for all your posts. I will have to look at my design and see if I
can change it. Here is what I am doing: I have a overrided method called
for each derived class and in each class which does something particular to
the derived class and it then needs to call a method in base which needs to
know which derived class called it.

Thanks

Doug Forster wrote:


you

I agree. If you need to find the "derived" class from a base class, you
have something in base class that is not generalized enough. If you can,
you might want to also attempt delegation pattern instead of inheritence
which gives you better control over things.

Or wait till generics come out. If its implemented in any manner like
VC++, you can probably use the great ATL trick of using the child class
as an input template argument..

That sounds like (sorta) Visitor pattern. You might want to look up that
pattern and see if that might help you out in your design.
 
HI Eric,

Has your problem been resolved?
If you still have any unclear, please feel free to let me know.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Eric" <[email protected]>
| References: <#[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: Inheritance Question
| Date: Fri, 17 Oct 2003 09:23:26 -0600
| Lines: 54
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 64.207.45.37
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:192106
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks for all your posts. I will have to look at my design and see if I
| can change it. Here is what I am doing: I have a overrided method called
| for each derived class and in each class which does something particular
to
| the derived class and it then needs to call a method in base which needs
to
| know which derived class called it.
|
| Thanks
|
| | > Doug Forster wrote:
| >
| > > Hi Eric,
| > >
| > > Though you can do as Chris describes, you might like to consider
whether
| you
| > > have correctly encapsulated your child class functionality. Code that
| > > explicitly examines type can often be generalised via a virtual
method.
| > >
| > > Cheers
| > >
| > > Doug Forster
| > >
| > > | > >
| > >>Hi,
| > >>
| > >>I have a base class with a method that need to determine which derived
| > >
| > > class
| > >
| > >>(child class) called it. How can I do this?
| > >>
| > >>Thanks
| > >>
| > >>
| > >
| > >
| > >
| > I agree. If you need to find the "derived" class from a base class, you
| > have something in base class that is not generalized enough. If you can,
| > you might want to also attempt delegation pattern instead of inheritence
| > which gives you better control over things.
| >
| > Or wait till generics come out. If its implemented in any manner like
| > VC++, you can probably use the great ATL trick of using the child class
| > as an input template argument..
| >
| > --
| > Girish Bharadwaj
| >
|
|
|
 
Back
Top