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

Operator overloading problem

Hello,

I have a problem comparing 2 values of type AnsiString using the ==
operator (C++ Builder 5).

I use another class called TValue, which also defines the == operator.
Now when I
try to compare 2 AnsiString Values the == operator of class TValue is
invoked.

How can I avoid that and force my program to use the operator defined
for the
AnsiString class.

Here you can see a short example of what I mean.

TValue::TValue(AnsiString Val) {
}

bool TValue::operator==(const TValue &iVal2) const {
return true;
}

test () {
AnsiString Val1, Val2;
Val1 = "Test";
Val2 = "Test";

if (Val1 == Val2) {
}
}

Thank you for your help,

Matti
Jul 19 '05 #1
3 4825
Matti wrote:
Hello,

I have a problem comparing 2 values of type AnsiString using the ==
operator (C++ Builder 5).

I use another class called TValue, which also defines the == operator.
Now when I
try to compare 2 AnsiString Values the == operator of class TValue is
invoked.

How can I avoid that and force my program to use the operator defined
for the
AnsiString class.

Here you can see a short example of what I mean.

TValue::TValue(AnsiString Val) {
}

bool TValue::operator==(const TValue &iVal2) const {
return true;
}

test () {
AnsiString Val1, Val2;
Val1 = "Test";
Val2 = "Test";

if (Val1 == Val2) {
}
}

Thank you for your help,


A complete compilable example would really help.

It seems to me that if what your saying is true, your compiler is not
conforming to the standard. In the example, Val1 == Val2, you should
have the AnsiString operator == called. If what I read from the
standard is true, conversion + calling operator == should never be
preferred over just calling operator ==.

Jul 19 '05 #2
"Matti" <xm**@yahoo.de> wrote in message
news:a4**************************@posting.google.c om...

I have a problem comparing 2 values of type AnsiString
using the == operator (C++ Builder 5).
[...]


The best place to ask this is
borland.public.cppbuilder.language.cpp. But before you
ask your question there, reduce your problem to a self-
contained program of 50 lines or less, if you can. Odds
are, by the time you do that, you'll see that you have an
error someplace else.

Dave

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
Jul 19 '05 #3

"Matti" <xm**@yahoo.de> wrote in message
news:a4**************************@posting.google.c om...
Hello,

I have a problem comparing 2 values of type AnsiString using the ==
operator (C++ Builder 5).

I use another class called TValue, which also defines the == operator.
Now when I
try to compare 2 AnsiString Values the == operator of class TValue is
invoked.

How can I avoid that and force my program to use the operator defined
for the
AnsiString class.

Here you can see a short example of what I mean.

TValue::TValue(AnsiString Val) {
}


Use "explicit" keyword in your conversion constructor.

explicit TValue::TValue(AnsiString Val) {
}

Jul 19 '05 #4

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

Similar topics

16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
51
by: Jojo | last post by:
Is there any way to get to the left-hand side of an operator? Consider the following (this is not meant to be perfect code, just an example of the problem): class Matrix { public: int data;...
4
by: hall | last post by:
Hi all. I have run into a problem of overloading a templatized operator>> by a specialized version of it. In short (complete code below), I have written a stream class, STR, which defines a...
7
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...
2
by: allan.mcrae | last post by:
I am having trouble with overloading the += operator when template parameters are used. I have a class holding an array (called "derived" in the following example) which derives from a base class...
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,...
11
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. ...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
24
by: Rahul | last post by:
Hi Everyone, I was just overloading operator = for a class and i have a problem in one case... class A { A& operator=(const A& obj) { return *this;
4
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Operator overloads are just like any other member function, you can make them do whatever you want. However, of course, we might expect them to behave in a certain way. The ++ operator should...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.