I am having a problem with overloading. An example:
Public Class Base
...
End Class
Public Class Derived Inherits Base
...
End Class
Public Class Client
Public Sub MethodX()
Dim obj as Base = new Derived()
MethodY(obj)
End Sub
Public Sub MethodY(arg as Base)
...
End Sub
Public Sub MethodY(arg as Derived)
...
End Sub
End Class
MethodX calls the MethodY that has a Base as a parameter, even though obj is
really a Derived object. The MethodY that has a Derived as a parameter would
seem to be a closer match to me. In my actual situation, I am getting the
Derived object elsewhere and do not know what type it is other than it is a
subclass of Base, so I cannot cast it more specifically. I am essentially
trying to do a select...case on the real object type polymorphically . Any
suggestions?
Thanks,
Brian Haynes 15 2521
=?Utf-8?B?QnJpYW4gSGF 5bmVz?= <Br*********@di scussions.micro soft.com>
wrote in news:36******** *************** ***********@mic rosoft.com: Public Sub MethodY(arg as Base) ... End Sub
Public Sub MethodY(arg as Derived) ... End Sub End Class
MethodX calls the MethodY that has a Base as a parameter, even though obj is really a Derived object. The MethodY that has a Derived as a
This is normal. Overloads are resolved at build time based on the type of the reference, not the
type of the instance. To resolve at run time would be very slow.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
Make your ASP.NET applications run faster http://www.atozed.com/IntraWeb/
Brian,
In addition to the other comments.
Consider moving MethodY directly or indirectly into Base as overridable
(directly).
Something like:
| Public Class Base
| ...
Public Overridable Sub MethodY()
' do work for Base's Method Y here.
End Sub
| End Class
|
| Public Class Derived Inherits Base
| ...
Public Overrides Sub MethodY()
' do work for Derived's Method Y here.
End Sub
| End Class
|
| Public Class Client
| Public Sub MethodX()
| Dim obj as Base = new Derived()
obj.MethodY()
| End Sub
|
| End Class
You may need to pass Client as a parameter above if MethodY needs behavior
from Client...
If MethodY "needs" to be in Client, then consider a double dispatch
(indirectly).
| Public Class Base
| ...
Public Overridable Sub MethodY(theClie nt As Client)
theClient.Metho dY(Me)
End Sub
| End Class
|
| Public Class Derived Inherits Base
| ...
Public Overrides Sub MethodY(theClie nt As Client)
theClient.Metho dY(Me)
End Sub
| End Class
|
| Public Class Client
| Public Sub MethodX()
| Dim obj as Base = new Derived()
obj.MethodY(Me)
| End Sub
|
| Public Sub MethodY(arg as Base)
' do work for Base's Method Y here.
| End Sub
|
| Public Sub MethodY(arg as Derived)
' do work for Derived's Method Y here.
| End Sub
| End Class
Using either method may introduce coupling between the classes that is not
desirable, in which case I would consider introducing an Interface that Base
or Client implements to help decouple the classes...
Hope this helps
Jay
"Brian Haynes" <Br*********@di scussions.micro soft.com> wrote in message
news:36******** *************** ***********@mic rosoft.com...
|I am having a problem with overloading. An example:
|
| Public Class Base
| ...
| End Class
|
| Public Class Derived Inherits Base
| ...
| End Class
|
| Public Class Client
| Public Sub MethodX()
| Dim obj as Base = new Derived()
| MethodY(obj)
| End Sub
|
| Public Sub MethodY(arg as Base)
| ...
| End Sub
|
| Public Sub MethodY(arg as Derived)
| ...
| End Sub
| End Class
|
| MethodX calls the MethodY that has a Base as a parameter, even though obj
is
| really a Derived object. The MethodY that has a Derived as a parameter
would
| seem to be a closer match to me. In my actual situation, I am getting the
| Derived object elsewhere and do not know what type it is other than it is
a
| subclass of Base, so I cannot cast it more specifically. I am essentially
| trying to do a select...case on the real object type polymorphically . Any
| suggestions?
|
| Thanks,
| Brian Haynes
"Chad Z. Hower aka Kudzu" wrote: This is normal. Overloads are resolved at build time based on the type of the reference, not the type of the instance. To resolve at run time would be very slow.
Example:
Dim i as Object = new Integer
Debug.WriteLine (i.ToString())
The output is 0, not "System.Object" . Obviously the ToString call is being
resolved based on the type of the object, not the reference. Isn't it a
little inconsistent for overloads to be resolved on reference type and
overrides to be resolved on object type?
"Jay B. Harlow [MVP - Outlook]" wrote: Brian, In addition to the other comments.
Consider moving MethodY directly or indirectly into Base as overridable (directly).
If MethodY "needs" to be in Client, then consider a double dispatch (indirectly).
Unfortunately, doing either would be much worse than what I am trying to
avoid. My actual situation is a little more complicated than my example. It
looks like the simplest solution to my problem is going to be doing something
like this:
If typeof obj is Derived Then
MethodY(CType(o bj, Derived))
ElseIf typeof obj is Derived2 Then
MethodY(CType(o bj, Derived2))
....
Else
MethodY(CType(o bj, Base))
End If
Thanks for the suggestions though.
-Brian
=?Utf-8?B?QnJpYW4gSGF 5bmVz?= <Br*********@di scussions.micro soft.com>
wrote in news:68******** *************** ***********@mic rosoft.com: Dim i as Object = new Integer Debug.WriteLine (i.ToString())
The output is 0, not "System.Object" . Obviously the ToString call is being resolved based on the type of the object, not the reference. Isn't it a little inconsistent for overloads to be resolved on reference type and overrides to be resolved on object type?
Because ToString is overridden, which is different than overloading. You are comparing two totally
different concepts.
Overloading compares parameters.
Overriding is on actaul methods of the class.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
Develop ASP.NET applications easier and in less time: http://www.atozed.com/IntraWeb/
"Chad Z. Hower aka Kudzu" wrote: Because ToString is overridden, which is different than overloading. You are comparing two totally different concepts.
Overloading compares parameters.
Overriding is on actaul methods of the class.
Yes, I realize that. But isn't the override resolution done at run time in
that case? If we have time to resolve the overrides at run time, why can't
we spare time for the overloads? It only really becomes an issue when the
overloads contain types that are related in an ancestor-descendant
relationship by inheritance/implementation. The cases where the overloads
are not related this way could be safely done at runtime.
-Brian
=?Utf-8?B?QnJpYW4gSGF 5bmVz?= <Br*********@di scussions.micro soft.com>
wrote in news:44******** *************** ***********@mic rosoft.com: Overloading compares parameters.
Overriding is on actaul methods of the class. Yes, I realize that. But isn't the override resolution done at run time in that case? If we have time to resolve the overrides at run
No, overloads are NEVER done at run time. Overrides are.
Overloads - always at build time.
Overrides - always at compile time.
time, why can't we spare time for the overloads? It only really
Because it would not only be slower, it would require a lot more memory to track it, a VMT of sorts
and it also isnt wanted. The overloads are compile time and thats what its designed for.
becomes an issue when the overloads contain types that are related in an ancestor-descendant relationship by inheritance/implementation. The cases where the overloads are not related this way could be safely done at runtime.
It wouldnt be proper though. Its not a short coming or for performance reasons, its by design.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
Make your ASP.NET applications run faster http://www.atozed.com/IntraWeb/
Brian Haynes wrote: "Jay B. Harlow [MVP - Outlook]" wrote:
Brian, In addition to the other comments.
Consider moving MethodY directly or indirectly into Base as overridable (directly).
If MethodY "needs" to be in Client, then consider a double dispatch (indirectly).
Unfortunately, doing either would be much worse than what I am trying to avoid. My actual situation is a little more complicated than my example. It looks like the simplest solution to my problem is going to be doing something like this:
If typeof obj is Derived Then MethodY(CType(o bj, Derived)) ElseIf typeof obj is Derived2 Then MethodY(CType(o bj, Derived2)) ... Else MethodY(CType(o bj, Base)) End If
Thanks for the suggestions though. -Brian
If that is what you want then that is what overriding is for! Define
MethodY in Base, define Overrides MethodY in each Derivedn, then just
MethodY(obj)
will call the appropriate one...
--
Larry Lard
Replies to group please
"Brian Haynes" <Br*********@di scussions.micro soft.com> wrote in message
news:36******** *************** ***********@mic rosoft.com...
.. . . I am getting the Derived object elsewhere and do not know what type it is other than it is a subclass of Base, so I cannot cast it more specifically.
Yes, you can, so long as you first ask it what type it actually is!
This might get you a bit closer to where you want to be ...
Dim obj as Base = GetPossiblyDeri vedObject()
If TypeOf obj Is Derived Then
MethodY( DirectCast( obj, Derived ) )
Else
MethodY( obj )
End If
HTH,
Phill W. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Terje Slettebø |
last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been
discussed before, I'd appreciate a pointer to it. I again haven't found it
in a search of the PHP groups.
The PHP manual mentions "overloading"
(http://no.php.net/manual/en/language.oop5.overloading.php), but it isn't
really overloading at all... Not in the sense it's used in other languages
supporting overloading (such as C++ and Java). As one of the...
|
by: Dave Theese |
last post by:
Hello all,
I'm trying to get a grasp of the difference between specializing a function
template and overloading it. The example below has a primary template, a
specialization and an overload. Note that the overload is identical to the
specialization except, of course, for the missing "template <>".
I don't know if my questions will be a bit too broad or not, but I thought
I'd give it shot... When is overloading preferable to...
|
by: bq |
last post by:
Hello,
This post is really two questions.
Question 1:
What is the current status on a revision of ISO C++, specifically
regarding plans for overloading composite operators? Some people in
this group probably would know. By "overloading composite operators" I
mean conversion of an expression like
A = B * C;
into a single function call (instead of three calls; one to "*",
another to copy and another to "="). Here A, B and C are of a
|
by: Kamran |
last post by:
Hi
I have very little experience of C++, nevertheless I have
been asked to write a gui using QT/QWT.
I know that I should direct the question to the relevant
mailing list and I have done that but I think my problem
has to do with my lack of understandign of some issues in C++.
There is a class in QWT called QwtPicker which allows one to
make rubberbands and select part of the drawing canvas, very
useful in zooming etc.
Function...
|
by: KL |
last post by:
I am working on a school assignment, so please don't tell me the
solution. I just want some direction.
I am supposed to overload the >, <, ==, !=, >=, and <= operators using
bool. I am having a bit of a problem in seeing what needs to happen
here. I am just not sure how I do this. Will the overloading function
recognize a < and a usual <? Do I do an
IF (a.letters < b.letters){
return true
}
| |
by: Eckhard Lehmann |
last post by:
Hi,
I try to recall some C++ currently. Therefore I read the "Standard C++
Bible" by C. Walnum, A. Stevens and - of course there are chapters about
operator overloading.
Now I have a class xydata with operator- overloaded:
class xydata {
public:
|
by: learning |
last post by:
hi I am trying to make some simple app to learn exception safety
coding. I know that new and delete can throw bad_alloc but how can I
force them to throw by a flag "change" at run time?
I am overloading new, new, delete and delete, change is set to 1
then it calls the custom operators. If 0, it calls the c++. But how can
I overload delete and delete?
The original library signature has const std::nothrow_ and throw()
If I overload this,...
|
by: lordkain |
last post by:
is it possible to do some kind of function overloading in c? and that
the return type is different
|
by: Pramod |
last post by:
I have one question. Can I catch exception in function overloading.In
my programm i want to create two function with same signature and same
return type. I know its not possible...can i use try.catch .block to
hand this exception.
|
by: Matthew |
last post by:
Am I correct in thinking there is no method/function overloading of any
kind in any version of PHP?
Thanks,
Matthew
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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,...
|
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...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |