Design method of overloaded operator in C + +

Source: Internet
Author: User

User-defined types such as: strings, dates, complex numbers, federations, and files often overload the two + operator to implement object connections, attach or merge mechanisms. But to implement the + operator correctly will bring some challenges to design, implementation and performance. This article outlines how to select the correct policy to overload this operator for a user-defined type.

Consider the following expression: int x=4+2;

The built-in + operator has two operands of the same type, adds and returns a right value of 6, and is assigned to X. We can conclude that the built-in + is a two-dollar, symmetric, interchangeable operator. It produces the same type of result as its operand type. According to this test, when you define a type overload operator for a user, you should also follow the characteristics of the corresponding built-in operator.

Overloading the user-defined type + operator is a common programming task. Although C + + provides several implementations, they can easily lead to design misunderstandings that often affect code correctness, performance, and compatibility with standard library components.

Here we will analyze the characteristics of the built-in operator and try to emulate its corresponding overload mechanism.

First step: Select between Member and non-member functions

You can implement the two-dollar operator in the form of a class member function such as: +,-and = =, for example:

Class String

{

Public

BOOL operator== (const String & S);//Compare *this and S

};

There is a problem with this method. The overloaded operator is not symmetric here, as opposed to its built-in operator, and its two arguments have one type: const string * CONST (this argument is implied) and another type: const string &. Therefore, some STL algorithms and containers will not be able to properly handle such objects.

An alternative approach is to define the overloaded operator + as an external (extern) function with two parameters of the same type:

String operator + (const string & S1, const string s2);

This way, the class String must declare the overloaded operator as a friend:

Class String

{

Public

Friend String operator+ (const string& s1,const string&s2);

};

Step two: The dilemma of the return value

As mentioned earlier, the built-in operator + returns the right value, the same type as the operand. However, it is inefficient to return an object in the caller stack, especially when dealing with large objects. So can you return a pointer or a reference? The answer is no. Because the return pointer breaks the rule that the parameter type should be the same as the return value type. Worse, linking multiple expressions will become impossible:

String S1,s2,s3;

String Res;

RES=S1+S2+S3; It is not possible to use string* as a return value

Although there is a way to define additional + operator overloaded versions, this approach is not desirable because the returned pointer must point to the dynamically allocated object. In this case, if the caller releases (delete) The returned pointer fails, it will cause a memory leak. Obviously, returning to string* is not a good idea.

Well, how about returning to string&? The returned reference must necessarily be a valid String. It avoids the use of dynamic object assignment, which returns a reference to a local static object. The static object does solve the memory leak problem, but the feasibility of this method is still questionable. In a multithreaded application, two threads may invoke the + operator concurrently, resulting in confusion for String objects. Also, because a static object always retains its state before its invocation, it is necessary to clear the static String object for each invocation of the + operator. From this perspective, returning results on the stack is still the safest and simplest solution.

Related Article

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.