![]() |
| | #1 | |
| Registered User Join Date: Aug 2007 Location: U.K.
Posts: 147
| Basic operator overloading question I'm just starting to try out Operator Overloading and wrote a basic definition for the equality operator ==. base.h : Code: class Base
{
public:
Base (int a);
int getNo() const { return m_a; }
private:
int m_a;
};
Base::Base(int a)
{
m_a = a;
}
bool operator==(const Base& leftparameter, const Base& rightparameter)
{
leftparameter.getNo() == rightparameter.getNo();
return true;
}
main.cpp : Code: #include <iostream>
#include "base.h"
using namespace std;
int main()
{
Base obj1(1);
Base obj2(1);
if(obj1 == obj2)
{
cout << "Objects match" << endl;
}
else
{
cout << "Objects don't match" << endl;
}
system ("pause");
return 0;
}
Quote:
I don't see why would it think I meant to assign one object to another. Should I not worry about it? Or am I missing the point of the warning? Thanks for any info! | |
| Swerve is offline | |
| | #2 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,315
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #3 |
| Registered User Join Date: Jan 2005
Posts: 7,251
| It successfully compares the two objects, but you ignore the result of the comparison. The warning thinks you made a different mistake, but there definitely is something you need to fix. |
| Daved is offline | |
| | #4 |
| Registered User Join Date: Aug 2007 Location: U.K.
Posts: 147
| Ahh Ok. Code: bool operator==(const Base& leftparameter, const Base& rightparameter)
{
if(leftparameter.getNo() == rightparameter.getNo())
{
return true;
}
else
{
return false;
}
}
Seems to work fine now. Thanks guys, just wanted to make sure I was getting the fundamentals right. |
| Swerve is offline | |
| | #5 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,315
| Indeed, but you could have written: Code: bool operator==(const Base& leftparameter, const Base& rightparameter)
{
return leftparameter.getNo() == rightparameter.getNo();
}
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is offline | |
| | #6 |
| Registered User Join Date: Aug 2007 Location: U.K.
Posts: 147
| Thanks laserlight! Great tips! |
| Swerve is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| A basic math programming question | hebali | C Programming | 38 | 02-25-2008 04:18 PM |
| Basic question about GSL ODE func RK4 | cosmich | Game Programming | 1 | 05-07-2007 02:27 AM |
| Basic question about RK4 | cosmich | C++ Programming | 0 | 05-07-2007 02:24 AM |
| A very basic question | AshFooYoung | C Programming | 8 | 10-07-2001 03:37 PM |