Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Optimization method of C++ operator overloading and return value

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "C++ operator overloading and return value optimization". Many people will encounter this dilemma in the operation of actual cases. next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Problem background

   compilation environment is still for C6455 DSP, in order to do some simple image histogram processing, and as far as possible without the template class, I wrote a simple class to store the array with length information, and can do some simple operations. The subtraction operator is overloaded so that the difference between the two histograms can be found.

Specific problems

   remember to remind yourself to overload copy constructors and assignment operators when there are members of a class that need to allocate space dynamically. To give an example, I defined the following CData class.

Class CData {private: int * makeshift public: CData (); CData (unsigned int nLen); / * There is no copy constructor at the very begining * / CData (const CData & data); ~ CData (); int & at (unsigned int i); int & at (unsigned int i) const; CData operator- (const CData & data); CData & operator= (const CData & data); CData & operator-= (const CData & data);}

   has two members in CData, a pointer of type int and a variable of type unsigned int representing the number of data.

   I overloaded a subtraction operator to directly subtract the int type data at each corresponding location in two objects of type CData of the same length. When invoking subtraction, copy constructors and assignment operators are bound to be used, so they also need to be overloaded.

By the way,    mentioned that I also declared an overload of the "- =" operator above to highlight that "- =" would be more efficient in certain situations, but because I can't directly overwrite the data in "subtracted" in practical application, I can't use the "- =" operator, only through the subtraction operator.

CData CData::operator- (const CData & data) {cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report