473,425 Members | 1,872 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,425 software developers and data experts.

Does the following construct qualify as overloading on return type ?

Hi,
Does the following construct qualify as overloading on return type ?
If not, in what way ? I overloaded the type conversion operator and
used pointers to member functions. Its pretty straightforward but I
am sure I have missed as this is not supposed to be possible. Am I
misunderstanding overloading ?
Thanks,
Nikhil

// foo() overloads on both input parameters and return value
//-------------------------------------------------------------
// int int_output = foo(int &int_input);
// double double_output = foo(int &int_input);
// int int_output = foo(double &double_input);
// double double_output = foo(double &double_input);
//-------------------------------------------------------------

#include <iostream>

class foo
{
public:
foo(int *given) {given_int = given; int_ = &foo::int_int; double_ =
&foo::double_int;}
foo(double *given) {given_double = given; int_ = &foo::int_double;
double_ = &foo::double_double;}
operator double () { (this->*double_)(); }
operator int () { (this->*int_)(); }
private:
double double_double() { std::cout << "--foo called with input
double and output double\n";}
double double_int() { std::cout << "-->foo called with output
double and output int was called\n";}
int int_int() { std::cout << "-->foo called with input int and
output int was called\n";}
int int_double() { std::cout << "-->foo called with input int and
output double was called\n";}
int *given_int;
double *given_double;
int (foo::*int_)();
double (foo::*double_)();
};

int main(int argc, char *argv)
{
double double_input, double_output;
int int_input, int_output;

std::cout << "Trying foo with int input and int output\n";
int_output = foo(&int_input);

std::cout << "Trying foo with int input and double output\n";
double_output = foo(&int_input);

std::cout << "Trying foo with double input and int output\n";
int_output = foo(&double_input);

std::cout << "Trying foo with double input and double output\n";
double_output = foo(&double_input);

}

// Output

// Trying foo with int input and int output
// -->foo called with input int and output int was called
// Trying foo with int input and double output
// -->foo called with output double and output int was called
// Trying foo with double input and int output
// -->foo called with input int and output double was called
// Trying foo with double input and double output
// --foo called with input double and output double

Aug 15 '08 #1
1 1472
Ni*************@gmail.com kirjutas:
Hi,
Does the following construct qualify as overloading on return type ?
If not, in what way ? I overloaded the type conversion operator and
used pointers to member functions. Its pretty straightforward but I
am sure I have missed as this is not supposed to be possible. Am I
misunderstanding overloading ?
By definition, only functions can be overloaded. Your foo is a class
instead, so it cannot be overloaded. Neither can it be called like you
seem to think according to the text output messages. I'm pretty sure the
different conversion operators are not called overloads either (correct
me if I'm wrong!)

But yes, by helper classes like this one can accomplish more than by a
simple function. Functors come to mind first. Here the helper class
imitates overloading by the return value, but I guess other solutions to
this problem might be simpler in most cases.

BTW, I simplified your code by removing unneeded obfuscations, and fixed
some UB errors, here it is:

#include <iostream>

class foo
{
public:
foo(int given): double_(given) {
std::cout << "-->foo called with input int";
}
foo(double given): double_(given) {
std::cout << "-->foo called with input double";
}
operator int () {
std::cout << " and output int\n"; return int(double_);
}
operator double () {
std::cout << " and output double\n"; return double_;
}
private:
double double_;
};

int main()
{
double double_input=0, double_output;
int int_input=0, int_output;

std::cout << "Trying foo with int input and int output\n";
int_output = foo(int_input);

std::cout << "Trying foo with int input and double output\n";
double_output = foo(int_input);

std::cout << "Trying foo with double input and int output\n";
int_output = foo(double_input);

std::cout << "Trying foo with double input and double output\n";
double_output = foo(double_input);

}
Regards
Paavo

>
Thanks,
Nikhil

// foo() overloads on both input parameters and return value
//-------------------------------------------------------------
// int int_output = foo(int &int_input);
// double double_output = foo(int &int_input);
// int int_output = foo(double &double_input);
// double double_output = foo(double &double_input);
//-------------------------------------------------------------

#include <iostream>

class foo
{
public:
foo(int *given) {given_int = given; int_ = &foo::int_int; double_ =
&foo::double_int;}
foo(double *given) {given_double = given; int_ = &foo::int_double;
double_ = &foo::double_double;}
operator double () { (this->*double_)(); }
operator int () { (this->*int_)(); }
private:
double double_double() { std::cout << "--foo called with input
double and output double\n";}
double double_int() { std::cout << "-->foo called with output
double and output int was called\n";}
int int_int() { std::cout << "-->foo called with input int and
output int was called\n";}
int int_double() { std::cout << "-->foo called with input int and
output double was called\n";}
int *given_int;
double *given_double;
int (foo::*int_)();
double (foo::*double_)();
};

int main(int argc, char *argv)
{
double double_input, double_output;
int int_input, int_output;

std::cout << "Trying foo with int input and int output\n";
int_output = foo(&int_input);

std::cout << "Trying foo with int input and double output\n";
double_output = foo(&int_input);

std::cout << "Trying foo with double input and int output\n";
int_output = foo(&double_input);

std::cout << "Trying foo with double input and double output\n";
double_output = foo(&double_input);

}

// Output

// Trying foo with int input and int output
// -->foo called with input int and output int was called
// Trying foo with int input and double output
// -->foo called with output double and output int was called
// Trying foo with double input and int output
// -->foo called with input int and output double was called
// Trying foo with double input and double output
// --foo called with input double and output double

Aug 15 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
20
by: Randy Yates | last post by:
Why is this necessary? The identifiers provide by the programmer are unique, and that is what is important - why map what the user has specified to something else? -- Randy Yates Sony Ericsson...
4
by: PKH | last post by:
This is a part of an autopointer template i'm using. I originally had a Get() member that returned the pointer, but thought it would nice to be able to use the -> operator. The question is why...
31
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
4
by: Tony Johansson | last post by:
Hello!! I have done some operator overloading but my main testprogram doesn't work well. Have you any idea which of my methods are wrong? #include <iostream> #include <string> using...
6
by: flopbucket | last post by:
Could someone explain to me what the difference is between function template specialization and function overloading? I guess overloading can change the number of parameters, but otherwise they...
14
by: Pramod | last post by:
I have one question. Can I catch exception in function overloading.In my programm i want to create two function with same signature and same return type. I know its not possible...can i use...
1
by: Madhav | last post by:
Hi All, Very recently I had one iteresting question about overloading. The guy was arguing why we dont have overloading based on return type. I told him about the reason mentioned in "Thinking in...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.