Overload Function Based on Return Value Type

Source: Internet
Author: User

Overload Function Based on Return Value Type  Wandreley caloni Jieshao
This code shows a method to use C ++ to overload function rules by replacing the function parameter mark with the function return value. Of course, without changing the language itself, the return value looks like an overload of the return type.

Back scene
Function overloading. In other words, you can reuse an existing function name but use different parameter labels, which are more or less known in the C ++, Java, and. Net communities. As necessary, we all know that the feature mark of an overloaded function must be different from its same type function. The Code is as follows:
Guid;
Wstring guids;
Createnewguid (guids); // callvoid createnewguid (wstring &)
Createnewguid (guid); // The Compiler knows that accepts different input formats. But what if the output format is different? If no result variable is passed as the output parameter, how can we use the overload feature in a different way to extract different or identical information? Let's imagine what a reasonable structure may be like. We may think of some results like this: guid;
Wstring guids;
Guids = createnewguid (); // CILS wstring createnewguid ()
Guid = createnewguid (); // CILS guid createnewguid () (the compiler knows that ?) Once again, we opened our broken old c ++ theoretical textbook, and we saw that the above Code could not work. Or at least, it should not. Two Functions Defined like this will cause the following error: Error c2556: 'guid createnewguid (void )': overloaded function differs only by return type from 'std: wstring createnewguid (void) '. This is correct. The error is correct. The return type is not an attribute that identifies the uniqueness of a function. Only the feature mark can be expressed (the parameter received by the function ). Afaik: this restriction is used to prevent hackers. In any case, except for general functions, there is nothing to allow us to use another feature. :

Struct createnewguid
{
// What is supposed to be here?
};


We found it! Now we can "call" our "functions. This "function" creates a new struct instance and assigns the "return value" to a wstring or our guid struct:
Guids = createnewguid (); // instancates A createnewguid
Guid = createnewguid (); // instancates A createnewguid. The difference is in the "return"

Use Code
Since we have created a new type and know that the new type is different from the known wstring and guid, we should convert the new type to the required type:
Struct createnewguid
{
Operator wstring () {...} // The conversion is the "function call"

Operator GUID () {...} // as there's a couple of conversions... over... underload!
};
In the end, we get the method to reload the "function" through the return value:
// Instancates A createnewguid e CILS createnewguid: Operator wstring ()
Guids = createnewguid ();

// Instancates A createnewguid e CILS createnewguid: Operator GUID ()
Guid = createnewguid ();

The complete code is as follows:
/* @ File underload. cpp
@ Brief a way to make overload using just the return type.
@ Author wandreley caloni
*/
# Include <windows. h>
# Include <objbase. h>

# Include <iostream>
# Include <string>

Using namespace STD;

Struct createnewguid
{
Operator wstring () // The first function...
{
Guid = Operator GUID ();
Olechar Buf [40] = {};
: Stringfromguid2 (guid, Buf, sizeof (BUF ));
Return wstring (BUF );
}

Operator GUID () //... and its "underload"
{
Guid = {};
: Cocreateguid (& guid );
Return guid;
}
};
</String> </iostream> </objbase. h> </Windows. h>

Objective (POI)
This code was written for a long time to answer a friend's question. After learning it, C ++ is more powerful than we think. We can always get what we want in one way or another.

Original

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.