If I wanted to return "this" from a member function of a class as a reference, would this piece of code be correct?
Record & operator = (const Record & that) { m_name = that.m_name; return * this; }
Should I just use "return this"?
Thanks for the help:)
Yup, that's right.
Return thiswill not work as it thisis a pointer. (The reason this is a pointer and not a link is because the links were not entered into the language until the classes were gone.)
this
, , ; . ( 3), .
, shouldn't I just use "return this" (, , "", , , , , . ")
shouldn't I just use "return this"
:
class T { void X() {} void Y() const {} };
T::X, this T *const, T::Y, this T const *const. , T, T. T ( ), *this.
T::X
T *const
T::Y
T const *const
*this
In this particular case, this may not be important, but there is a good practice in the copy-destination operator to have a protective self-start mechanism.
Record& operator=(const Record& that) { if( &that != this ) { m_name = that.m_name; } return *this; }
This protects against claims such as
Record r; // do something with r r = r; // this is protected
It would be important when a class performs resource management (for example, dynamically allocated memory).
Source: https://habr.com/ru/post/1765683/More articles:'from sqlite3 import dbapi2 as sqlite3' vs 'import sqlite3'? - pythonC ++ function parameter as a reference - c ++Interop Word - remove page from document - c #MySQL performance: nested key insertion / duplicate and several updates - performanceDynamic image resizing - javascriptIs it possible for the location authorization nodes in web.config to be external? - c #Ruby to Python syntax syntax - pythonHow to define relationships in Zend Framework - zend-frameworkSubversion svn: redefining externals files? - overrideDisplaying NUnit Run Results, CruiseControl.Net 1.5.7256.1 - nunitAll Articles