Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use an overloaded op in a class constructor? 1

Status
Not open for further replies.

mheppler

Programmer
Feb 6, 2006
8
0
0
US
I have a class Card. I have overloaded the + operarator such that +(Card Card, int X). Can I use this + in the Card constructor? For example I want to add 5 to each card as it is being constructed. "this" doesn't seem to work.

Thanks,
Mark
 
what?

Post your card class so we can see what you're talking about!
 
Why are you overloading the addition operator? This is generally considered a bad thing, as it results in behavior which other people may not expect. I would suggest writing an Add method instead, as that is more intuitive.

Also - not all .net languages support operator overloading, so if CLS compliance is one of your design goals, you'd need to add an alternate method anyway.

And there are two possible reasons why it's not working in the constructor:

1. Operators are always public static yet depend on instance data.

2. The instance you're trying to add cards to (this) hasn't been fully constructed at that time.

Chip H.

____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top