473,466 Members | 1,366 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Overloaded Functions with same header ?

Hello,

Is it possible in C# to have 2 overloaded functions with

- same names
- same parameters
- different return type values

If no, is it possible in another language ?
Regards,
Cybertof.
Nov 15 '05 #1
3 2233
cybertof <cy****************@gmx.net> wrote:
Is it possible in C# to have 2 overloaded functions with

- same names
- same parameters
- different return type values
No.
If no, is it possible in another language ?


Not if it wants to remain CLS-compliant. The spec is quite interesting
on this point. Quoting from section 10.2 of partition I:

<quote>
Note: The CTS, while it describes inheritance, object layout, name
hiding, and overriding of virtual methods, does not discuss overloading
at all. While this is surprising, it arises from the fact that
overloading is entirely handled by compilers that target the CTS and
not the type system itself. In the metadata, all references to types
and type members are fully resolved and include the precise signature
that is intended. This choice was made since every programming language
has its own set of rules for coercing types and the VES does not
provide a means for expressing those rules.

Following the rules of the CTS, it is possible for duplicate names to
be defined in the same scope as long as they differ in either kind
(field, method, etc.) or signature. The CLS imposes a stronger
restriction for overloading methods. Within a single scope, a given
name may refer to any number of methods provided they differ in any of
the following:

o Number of parameters
o Type of each argument

Notice that the signature includes more information but CLS-compliant
languages need not produce or consume classes that differ only by that
additional information (see Partition II for the complete list of
information carried in a signature):

o Calling convention
o Custom modifiers
o Return type
o Whether a parameter is passed by value or by reference (i.e. as a
managed pointer or by-ref)

There is one exception to this rule. For the special names op_Implicit
and op_Explicit described in clause 10.3.3 methods may be provided that
differ only by their return type. These are marked specially and may be
ignored by compilers that don't support operator overloading.

Properties shall not be overloaded by type (that is, by the return type
of their getter method), but they may be overloaded with different
number or types of indices (that is, by the number and types of the
parameters of its getter method). The overloading rules for properties
are identical to the method overloading rules.

CLS Rule 37: Only properties and methods may be overloaded.

CLS Rule 38: Properties, instance methods, and virtual methods may be
overloaded based only on the number and types of their parameters,
except the conversion operators named op_Implicit and op_Explicit which
may also be overloaded based on their return type.

Note:
CLS (consumer): May assume that only properties and methods are
overloaded, and need not support overloading based on return type
unless providing special syntax for operator overloading. If return
type overloading isnt supported, then the op_Implicit and op_Explicit
may be ignored since the functionality shall be provided in some other
way by a CLS-compliant framework.

CLS (extender): Should not permit the authoring of overloads other than
those specified here. It is not necessary to support operator
overloading at all, hence it is possible to entirely avoid support for
overloading on return type.

CLS (framework): Shall not publicly expose overloading except as
specified here. Frameworks authors should bear in mind that many
programming languages, including Object- Oriented languages, do not
support overloading and will expose overloaded methods or properties
through mangled names. Most languages support neither operator
overloading nor overloading based on return type, so op_Implicit and
op_Explicit shall always be augmented with some alternative way to gain
the same functionality.

</quote>

--
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
Hi cybertof,

No it is not possible.
Maybe some languages provide such a overloading, I don't know any of them,
though.
This is not a technical issue. Compilers knows everything for the method you
want to override (at compile time), so it is not a problem to offer overring
on return value's type only. But this is considered as error prone and is
excluded as an option by design.

For example take a look at C++.
The C++ compilers decorate internaly the names of all methods with coded
information about the class the method is decalred in (if any), all of their
parameters' type and the type of the returning value. However, what
decoration should be used is not standardized and is left to the compiler's
vendors to choose their own. This is one of the main reason for c++ library
and obj file incompatibility.

I gave you that example just to back up my assertion here that this has
never been a technical issue. It is rather a language design issue and I
believe there is no language among the most popular ones that offers this
functionality.
Of course I might be wrong.

---
B\rgds
100

"cybertof" <cy****************@gmx.net> wrote in message
news:MP***********************@msnews.microsoft.co m...
Hello,

Is it possible in C# to have 2 overloaded functions with

- same names
- same parameters
- different return type values

If no, is it possible in another language ?
Regards,
Cybertof.

Nov 15 '05 #3
Thanks for your answers Jon & Stoitcho.

Regards,
Cybertof.
Nov 15 '05 #4

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

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
12
by: Jack Daly | last post by:
I've inherited some code which uses an undocumented feature of a third-party vendor's library. Essentially, this vendor has kept the details of an interface struct secret, but we can pass a pointer...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
1
by: prakash.mirji | last post by:
Hello, I am trying to compile C++ code which uses rogue wave 9.0 classes on RHEL 4.0. I use gnu g++ compiler to compile the code. Compiler is not able to match the right type of parameters to...
2
by: desktop | last post by:
If a function should work with different types you normally overload it: void myfun(int a){ // do int stuff } void myfun(std::string str){ // do string stuff }
4
by: king kikapu | last post by:
Hi, i am trying, to no avail yet, to take a C#'s overloaded functions skeleton and rewrite it in Python by using closures. I read somewhere on the net (http://dirtsimple.org/2004/12/python-is-...
3
by: jerry.teshirogi | last post by:
I have the following class and main: ////////////////////////////////////////////////////////// #include <iostream.h> class myVector { public: double x, y, z:
9
by: Lawrence Krubner | last post by:
Possibly a dumb question but is type hinting allowed with overloaded methods? Can one class have these two methods? public function doSelect(Criteria $c) { // some code here } public...
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,...
1
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...
0
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.