overload on return type

S

Shea Martin

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
 
K

KV Rani

It compiled on my g++.
g++ (GCC) 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125).
 
J

Jakob Bieling

Shea Martin said:
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
 
R

Rolf Magnus

Shea said:
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.
 
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

Shea said:
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
 
S

Shea Martin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top