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

Operator overload for generics

Hi there,
I'm trying to apply "less than" and "more than" operators on the
Generics (class template-like) type objects. Below is the code of my
class representing a parameter that can be any type, but only if it's
float or int the comparison results in true/false-like result, else
its INVALID. I get error:
Operator '<' cannot be applied to operands of type 'T' and 'T' at
line:
set { m_Value = value < m_MinValue ? m_MinValue : (value >
m_MaxValue ? m_MaxValue : value); }
Could anyone take a look and help me with that? I'd be grateful.

Best regards
teel

public class Parameter<T>
{
public Parameter(T value, T minValue, T maxValue)
{
m_Value = value;
m_MinValue = minValue;
m_MaxValue = maxValue;
}

public T Value
{
get { return m_Value; }
set { m_Value = value < m_MinValue ? m_MinValue : (value >
m_MaxValue ? m_MaxValue : value); }
}

public static ParameterComparisonResult operator
<(Parameter<Tlhs, Parameter<Trhs)
{
if (lhs is float && rhs is float)
{
if ((float)(object)lhs < (float)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else if (lhs is int && rhs is int)
{
if ((int)(object)lhs < (int)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else
return ParameterComparisonResult.INVALID;
}

public static ParameterComparisonResult operator
>(Parameter<Tlhs, Parameter<Trhs)
{
if (lhs is float && rhs is float)
{
if ((float)(object)lhs (float)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else if (lhs is int && rhs is int)
{
if ((int)(object)lhs (int)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else
return ParameterComparisonResult.INVALID;
}

public enum ParameterComparisonResult
{
INVALID,
TRUE,
FALSE
}

private T m_Value;
private T m_MinValue;
private T m_MaxValue;
}

Sep 26 '07 #1
5 2219
teel,

Since you have no constraints, the only methods/properties that you have
available to you are those that are available to object. Unfortunately,
constraints won't do much for you here, since you can't specify to use
integer OR float.

There isn't really much benefit here to using generics here, since the
constraint system pretty much prevents you from doing what you want.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"teel" <tl****@gmail.comwrote in message
news:11**********************@50g2000hsm.googlegro ups.com...
Hi there,
I'm trying to apply "less than" and "more than" operators on the
Generics (class template-like) type objects. Below is the code of my
class representing a parameter that can be any type, but only if it's
float or int the comparison results in true/false-like result, else
its INVALID. I get error:
Operator '<' cannot be applied to operands of type 'T' and 'T' at
line:
set { m_Value = value < m_MinValue ? m_MinValue : (value >
m_MaxValue ? m_MaxValue : value); }
Could anyone take a look and help me with that? I'd be grateful.

Best regards
teel

public class Parameter<T>
{
public Parameter(T value, T minValue, T maxValue)
{
m_Value = value;
m_MinValue = minValue;
m_MaxValue = maxValue;
}

public T Value
{
get { return m_Value; }
set { m_Value = value < m_MinValue ? m_MinValue : (value >
m_MaxValue ? m_MaxValue : value); }
}

public static ParameterComparisonResult operator
<(Parameter<Tlhs, Parameter<Trhs)
{
if (lhs is float && rhs is float)
{
if ((float)(object)lhs < (float)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else if (lhs is int && rhs is int)
{
if ((int)(object)lhs < (int)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else
return ParameterComparisonResult.INVALID;
}

public static ParameterComparisonResult operator
>>(Parameter<Tlhs, Parameter<Trhs)
{
if (lhs is float && rhs is float)
{
if ((float)(object)lhs (float)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else if (lhs is int && rhs is int)
{
if ((int)(object)lhs (int)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else
return ParameterComparisonResult.INVALID;
}

public enum ParameterComparisonResult
{
INVALID,
TRUE,
FALSE
}

private T m_Value;
private T m_MinValue;
private T m_MaxValue;
}

Sep 26 '07 #2

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:Oa**************@TK2MSFTNGP04.phx.gbl...
teel,

Since you have no constraints, the only methods/properties that you
have available to you are those that are available to object.
Unfortunately, constraints won't do much for you here, since you can't
specify to use integer OR float.

There isn't really much benefit here to using generics here, since the
constraint system pretty much prevents you from doing what you want.
Actually for this case, generics work very well. Constrain the type T to
implement IComparable, and then use IComparable.CompareTo instead of <,=,>

It's with actual math (+, -, *, /, etc) that generics don't work -- at all!
>
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"teel" <tl****@gmail.comwrote in message
news:11**********************@50g2000hsm.googlegro ups.com...
>Hi there,
I'm trying to apply "less than" and "more than" operators on the
Generics (class template-like) type objects. Below is the code of my
class representing a parameter that can be any type, but only if it's
float or int the comparison results in true/false-like result, else
its INVALID. I get error:
Operator '<' cannot be applied to operands of type 'T' and 'T' at
line:
set { m_Value = value < m_MinValue ? m_MinValue : (value >
m_MaxValue ? m_MaxValue : value); }
Could anyone take a look and help me with that? I'd be grateful.

Best regards
teel

public class Parameter<T>
{
public Parameter(T value, T minValue, T maxValue)
{
m_Value = value;
m_MinValue = minValue;
m_MaxValue = maxValue;
}

public T Value
{
get { return m_Value; }
set { m_Value = value < m_MinValue ? m_MinValue : (value >
m_MaxValue ? m_MaxValue : value); }
}

public static ParameterComparisonResult operator
<(Parameter<Tlhs, Parameter<Trhs)
{
if (lhs is float && rhs is float)
{
if ((float)(object)lhs < (float)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else if (lhs is int && rhs is int)
{
if ((int)(object)lhs < (int)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else
return ParameterComparisonResult.INVALID;
}

public static ParameterComparisonResult operator
>>>(Parameter<Tlhs, Parameter<Trhs)
{
if (lhs is float && rhs is float)
{
if ((float)(object)lhs (float)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else if (lhs is int && rhs is int)
{
if ((int)(object)lhs (int)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else
return ParameterComparisonResult.INVALID;
}

public enum ParameterComparisonResult
{
INVALID,
TRUE,
FALSE
}

private T m_Value;
private T m_MinValue;
private T m_MaxValue;
}


Sep 26 '07 #3
On 27 Wrz, 00:13, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Actually for this case, generics work very well. Constrain the type T to
implement IComparable, and then use IComparable.CompareTo instead of <,=,>
It's with actual math (+, -, *, /, etc) that generics don't work -- at all!
Thanks a lot both of you guys!!
It's working with the IComparable :)

Best regards,
teel

Sep 26 '07 #4
"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:eG**************@TK2MSFTNGP03.phx.gbl...
>
Actually for this case, generics work very well. Constrain the type T to
implement IComparable, and then use IComparable.CompareTo instead of <,=,>
Nice brainwork. I thought the problem was unsolvable.
I had never thought of using IComparable.

- Michael Starberg
Sep 27 '07 #5

"teel" <tl****@gmail.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
Ok guys, may I ask you another question? My final Parameter class
looks like this:
answered in your new post
Sep 27 '07 #6

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

Similar topics

5
by: bsaucer | last post by:
I am creating a class with operator overloads. It makes references to another class I've created, but do not wish to modify. I try to overload an operator having arguments having the other class...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
4
by: Chiller | last post by:
Ok, thanks to some good assistance/advice from people in this group I've been able to further develop my Distance class. Since previous posts I've refined my code to accept the unit measurement...
7
by: Sean | last post by:
Can someone help me see why the following "operator=" overloading doesn't work under g++? and the error message is copied here. I see no reason the compiler complain this. Thanks, $ g++...
17
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
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...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
17
by: My interest | last post by:
How can I use * operator in C# generics? e.g. class foo<T_> { T_ v1, v2: public T_ squar() { return v1 * v2; /// wrong! }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.