By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,619 Members | 2,698 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,619 IT Pros & Developers. It's quick & easy.

overload on return type

P: n/a
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.

~S
Apr 8 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
It compiled on my g++.
g++ (GCC) 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125).

Apr 8 '06 #2

P: n/a
Shea Martin <sh***@void.nil> wrote:
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.


It is valid and actually pretty common. Note that you do not
overload on the return type. You cannot overload a function solely on
its return type. In your case, both of your functions have different
number of arguments, thus it is fine.

hth
--
jb

(reply address in rot13, unscramble first)
Apr 8 '06 #3

P: n/a
Shea Martin wrote:
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.


What version of g++ is that? It should compile. The return type is just not
considered in function overloading, and since the two functions differ in
both the parameter list and the cv-qualifiers, they are valid overloads.

Apr 8 '06 #4

P: n/a
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
Apr 8 '06 #5

P: n/a
Thanks for the replies. I thought I had done it before. It compiles
now. I must have had an error somewhere else, and not read the error
that closely. I am not that efficient at reading gcc error messages, as
I used to use Sun's compilers for my job, now I use MSVC++ for my job...

GCC is for my hobby stuff.

Thanks.

~S
Apr 9 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.