[Qt-interest] struct & class
Ross Driedger
ross at earz.ca
Mon Nov 16 21:33:02 CET 2009
On Mon, 16 Nov 2009 14:32:24 -0500, <qt-interest-request at trolltech.com>
wrote:
> Message: 4
> Date: Mon, 16 Nov 2009 11:32:11 -0800 (PST)
> From: navid navid <n_nnavid at yahoo.com>
> Subject: [Qt-interest] struct & class
> To: qt-interest at trolltech.com
> Message-ID: <699383.41515.qm at web65609.mail.ac4.yahoo.com>
> Content-Type: text/plain; charset=us-ascii
> Hello All,
> Because of unavailability of operator == or != for structure comparison,
> I had to change {struct of variables} to {class of variables with
> operator definition}.
You can overload the operators for a struct.
> but now, the functions that returned SETPOINT, can't return SetPoint;
> and show "error: `SetPoint' does not name a type"
I'm not too sure what your issue is, here, but SETPOINT and SetPoint are
two different types.
> struct SETPOINT
> {
> double a;
> int b;
> double c;
> int d;};
> class SetPoint
> {
> public:
> double a;
> int b;
> double c;
> int d;
A few C++ (not Qt) issues here:
> bool operator!=(SetPoint other)
Your argument should be a const reference to the other object and the
operator should be declared as const:
bool operator!=(const SetPoint& other) const
C++ defaults to pass-by-copy, so unless you specify the argument as a
reference the overhead could slow down your program. It should also be
const so you can apply the operator to both const and non-const objects
> {
> return ((a!=other.a) ||
> (b!=other.b)||
> (c!=other.c) ||
This is a source on many, many bugs. The equality operator applied to any
floating point number is a flood of calls to customer support, waiting to
happen. Arithmetic on doubles and floats is not always precise. A decent
book on C programming and scinetific programming will have a solution to
this problem.
> (d!=other.d));
> }
> };
> what is solution?
Refactor you code, remove your class, add the operators to your struct and
see what happens.
This works:
struct a
{
int _x;
bool operator==(const a& other) const
{
return _x == other._x;
}
bool operator!=(const a& other) const
{
return !(*this == other);
}
};
--
"So Einstein was wrong when he said "God does not play dice".
Consideration of black holes suggests, not only that God does play dice,
but that He sometimes confuses us by throwing them where they can't be
seen."
Stephen Hawking
Ross Driedger
ross_at_earz.ca
More information about the Qt-interest-old
mailing list