G
Guest
I recently decided to do the right thing & write accessor functions for my
class member variables instead of having them public and introduced a rather
nasty bug. The class below shows the error - basically if you forget to add
the parenthesis in the call to function X() then the program compiles ok with
no warnings but gives the wrong answer
I'm using VS2005 Prof edition v2.0.50727
The answer seems to be that if you are writing accessor functions then don't
use overloaded functions - maybe use setX & getX instead - or make sure you
don't forget the parenthesis! It seems to think I want a function pointer(?)
but I think it should at least give a warning.
class Test {
public:
Test() : m_Num(10) {}
void X(const double val) {m_Num = val;}
double X(void) {return m_Num;};
private:
double m_Num;
};
int _tmain(int argc, _TCHAR* argv[])
{
Test t;
double y = 5.0;
double z;
// z = t.X // this will generate an error
z = t.X * y; // this will generate no error or warning but won't call t.X()
return 0;
}
I've googled for this but am surprised I've not seen more issues with this
class member variables instead of having them public and introduced a rather
nasty bug. The class below shows the error - basically if you forget to add
the parenthesis in the call to function X() then the program compiles ok with
no warnings but gives the wrong answer
I'm using VS2005 Prof edition v2.0.50727
The answer seems to be that if you are writing accessor functions then don't
use overloaded functions - maybe use setX & getX instead - or make sure you
don't forget the parenthesis! It seems to think I want a function pointer(?)
but I think it should at least give a warning.
class Test {
public:
Test() : m_Num(10) {}
void X(const double val) {m_Num = val;}
double X(void) {return m_Num;};
private:
double m_Num;
};
int _tmain(int argc, _TCHAR* argv[])
{
Test t;
double y = 5.0;
double z;
// z = t.X // this will generate an error
z = t.X * y; // this will generate no error or warning but won't call t.X()
return 0;
}
I've googled for this but am surprised I've not seen more issues with this