Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C++

Function Overload by Return

Rate me:
Please Sign up or sign in to vote.
4.40/5 (30 votes)
5 Apr 2007CPOL2 min read 118.8K   441   29   44
Choosing the right function based on the return type

Introduction

This code shows one way to use the C++ overload rule using the function return type instead of its signature. Of course, nothing is changed in the language, and the result just looks like a return type overload.

Background

The function overload, in other words, reusing an existent function name but with different parameters, is something more or less known by the C++, Java and .NET community. As a sine qua non condition, we know that the overloaded function signature must be always different from its homonyms colleagues. The code below illustrates this:

C++
GUID guid;
wstring guidS;

CreateNewGUID(guidS); // calls void CreateNewGUID(wstring&)
CreateNewGUID(guid);  // calls void CreateNewGUID(GUID&) (the compiler knows that)

It's a pretty common way to accept different input formats. But what about the output? How to extract different information or the same information in different format using the overload feature (e.g. the above example) without passing the result variable as a output argument? Giving ourselves some imagination and thinking how a valid construction would be, we can think of something like this:

C++
GUID guid;
wstring guidS;

guidS = CreateNewGUID(); // calls wstring CreateNewGUID()
guid = CreateNewGUID();  // calls GUID CreateNewGUID() (the compiler knows that?)

Opening again our old and hurt C++ theories book, we can see the above code DOES NOT work. Or, at least, should not be. Just defining a couple of functions like that causes the following error:

error C2556: 'GUID CreateNewGUID(void)' : overloaded function differs 
	only by return type from 'std::wstring CreateNewGUID(void)'

That's right. Well, the error is right. The return type is not a function property that identifies it uniquely. Only the signature can do that (the parameters received by the function). AFAIK, this "limitation" is hack proof. Anyway, nothing disallows us to use another feature besides ordinary functions:

C++
struct CreateNewGUID
{
   // what is supposed to be here?
};

We got it! Now we can "call" our "function" creating a new instance of the struct and attributing the "return" to a wstring or to our GUID struct:

C++
guidS = CreateNewGUID(); 	// instantiates a CreateNewGUID
guid = CreateNewGUID(); 	// instantiates a CreateNewGUID. 
			// The difference is in the "return"

Using the Code

Since we create a new type, and knowing that this new type is different from the already known wstring and GUID types, we should simply convert our new type into every desirable return type:

C++
struct CreateNewGUID
{
   operator wstring () { ... } 	// the conversion is the "function call"

   operator GUID () { ... } 	// as there's a couple of conversions... 
				// over... underload!
};

This concludes our somewhat odd solution to overload a "function" by the return type:

C++
// instantiates a CreateNewGUID e calls CreateNewGUID::operator wstring()
guidS = CreateNewGUID();

// instantiates a CreateNewGUID e calls CreateNewGUID::operator GUID()
guid = CreateNewGUID();

There's the entire source.

C++
/** @file underload.cpp
@brief A way to make overload using just the return type.
@author Wanderley Caloni
*/
#include <windows.h>
#include <objbase.h>

#include <iostream />
#include <string>

using namespace std;

struct CreateNewGUID
{
   operator wstring () // the first function...
   {
      GUID guid = operator GUID();
      OLECHAR buf[40] = { };
      ::StringFromGUID2(guid, buf, sizeof(buf));
      return wstring(buf);
   }

   operator GUID () // ... and its "underload"
   {
      GUID guid = { };
      ::CoCreateGuid(&guid);
      return guid;
   }
};

Points of Interest

This code was developed a long time ago as an answer to a friend's doubt. I learnt while coding that C++ is even more powerful than we think it is. In one form or another, we can always get what we want.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Intelitrader
Brazil Brazil
Wanderley Caloni
Backend Specialist
Location: São Paulo, Brazil
Email: wanderley.caloni@gmail.com
Skills
C, C++, Assembly, Windows, x86, Reverse Engineering, Cryptography, Tech/Art Writing, Debugging, Git, SQL

Experience
Intelitrader, 2015 - current (Tech Lead)
Market Data System
DataHub System
Finantial Market Vendor Projects
BitForge, 2015 - current (Owner Developer)
Appliance ATM System Security
Information Security Product Maintenance
Minor projects and skills
Communication API with HID USB devices
Windows Phone and Android user interfaces programming
Boost.Asio Winsock bug
Hidapi issue with IO pending
UOL Diveo Broker, 2012 - 2015 (Backend Developer)
Financial Market Risk System (Stock Broker)
HUB Network API Development and Maintenance
Scua Information Security, 2008 - 2012 (Security Developer)
Information Security Product Maintenance
Disk Cryptography
Team Coordination
Minor projects and skills
Hardware inventory using WMI/SMBIOS
Software inventory using Windows Registry
PrintScreen protection using COM interfaces
Windows Event Log using device driver
User/Kernel communication using DeviceIoControl API
VNC like development
PsExec like development
Documents print control using regex (boost) and shell hook
User policies management using Windows Registry and API hooks
Database migration from CTree to SQL using OLE classes
Windows authentication using DCOM service, custom GINA and Credential Provider (Windows Vista)
Database synchronization using DCOM service
Linux maintenance bootable CD, bash scripts and cryptography tools
USB Cryptography (pen drives)
Crash dump analysis using Windbg
System account process execution using COM service
MBR (Master Boot Record) customization analysing BIOS features
Blowfish and SHA-1 development in 16 bits assembly (x86)
Audit driver using shared memory between user and kernel events
Open Communications Security, 2005 - 2008 (Security Specialist)
Reverse Engineering (Trojan Analysis)
API Hooking
Antidebugging Solution
Code Ofusc

Comments and Discussions

 
GeneralRe: Misuse of Conversion Operators [modified] Pin
*bilk*9-Apr-07 4:24
*bilk*9-Apr-07 4:24 
GeneralRe: Misuse of Conversion Operators Pin
Wanderley Caloni9-Apr-07 6:36
professionalWanderley Caloni9-Apr-07 6:36 
GeneralCool!!!! Pin
Nigel Atkinson2-Apr-07 12:46
Nigel Atkinson2-Apr-07 12:46 
GeneralCool Pin
Renato Tegon Forti2-Apr-07 7:57
Renato Tegon Forti2-Apr-07 7:57 
GeneralRe: Cool Pin
Wanderley Caloni2-Apr-07 10:07
professionalWanderley Caloni2-Apr-07 10:07 
GeneralNice job Pin
Anant wakode1-Apr-07 23:21
Anant wakode1-Apr-07 23:21 
GeneralRe: Nice job Pin
Wanderley Caloni2-Apr-07 2:34
professionalWanderley Caloni2-Apr-07 2:34 
GeneralGreat idea! Pin
kuhx198031-Mar-07 21:27
kuhx198031-Mar-07 21:27 
Great idea!Big Grin | :-D
GeneralRe: Great idea! Pin
Wanderley Caloni2-Apr-07 1:27
professionalWanderley Caloni2-Apr-07 1:27 
QuestionBest practice? Pin
Scott Dorman30-Mar-07 13:38
professionalScott Dorman30-Mar-07 13:38 
AnswerRe: Best practice? Pin
Wanderley Caloni30-Mar-07 16:37
professionalWanderley Caloni30-Mar-07 16:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.