473,385 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

overload on return type

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
5 2947
It compiled on my g++.
g++ (GCC) 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125).

Apr 8 '06 #2
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: bsaucer | last post by:
I am creating a class with operator overloads. It makes references to another class I've created, but do not wish to modify. I try to overload an operator having arguments having the other class...
1
by: Piotre Ugrumov | last post by:
I'm following your help. I have written the overload of the operator <<. This overload work! :-) But I have some problem with the overload of the operator >>. I have written the overload of this...
1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
7
by: Sean | last post by:
Can someone help me see why the following "operator=" overloading doesn't work under g++? and the error message is copied here. I see no reason the compiler complain this. Thanks, $ g++...
17
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
18
by: skishorev | last post by:
Hi, Here I am taking two functions. void f(int,int) and another one is float f(int,int). Is it possible to overload with return values. Thx, kishore
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
3
by: i3x171um | last post by:
To start off, I'm using GCC4. Specifically, the MingW (setjmp/longjmp) build of GCC 4.2.1 on Windows XP x64. I'm writing a class that abstracts a message, which can be either an integer (stored as...
5
by: jknupp | last post by:
In the following program, if the call to bar does not specify the type as <int>, gcc gives the error "no matching function for call to ‘bar(A&, <unresolved overloaded function type>)’". Since bar...
1
by: fabian.lim | last post by:
Hi all, Im having a problem with my code. Im programming a vector class, and am trying to overload the () operator in 2 different situations. The first situation is to assign values, e.g. Y =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.