Shea Martin schrieb:
I have a struct like so:
struct MyStruct
{
public:
void Value( int newValue ) { mValue = newValue; }
int Value() const { return mValue; }
private:
int mValue;
};
This is a compile error on g++, as it overloads on return type. But I
think it compiles on MSVC++. I am at home now, and don't have access to
a windows/MSVC++, so I can't verify this. Can someone tell me if I am
losing my mind? I kind of like the semantics of having my get set
methods like this. I don't use g++ a lot, but if it is not std C++, or
if it won't be in the future, then I'll drop it.
on the Linux platform gcc is the most used compiler
and I never had problems with it so far
I started using it from 2.95 Version up to 4.0.2 now
but in general it's always a good idea to try your code
with (newer) different compilers
as other pointed out you don't overload on the return type
void foo(){} //
int foo(){} // here is what you assumed
but this is an error and will be flaged by compiler
you even could go so far and overload methods (non static class
functions) on their cv qualifiers. cv stads for const volatile
#include <iostream>
#include <cstdlib>
using std::cout;
using std::endl;
#define P(msg) cout << #msg << endl;
struct Q {
void Value(int x) { xx = x; }
int & Value() { P(ref); return xx; }
int Value()const { P(val); return xx; }
int Value()volatile { P(volatile); return xx; }
int Value()const volatile { P(const-volatile); return xx; }
Q(int x=0x0ABCDEF0) : xx(x) {}
int xx;
};
int main()
{
Q q = 1;
q.Value(2);
cout << q.Value() << endl;
Q const qq(3);
//qq.Value(4); // only const-qualified methods can be called
on const object
cout << qq.Value() << endl;
Q volatile qqq;
cout << qqq.Value() << endl;
const Q /*const*/ volatile qqqq;
cout << qqqq.Value() << endl;
return EXIT_SUCCESS;
}
hth, Daniel