I am wondering why C ++ (and possibly other languages, I'm not sure) does not allow such statements.
MyObject foo.DoBar();
You might think that a language can understand to build an object, and then call a function. The only reason I can think that this does not work is that if the construction of the object failed, the operator will still try to call the function.
What are the reasons why those who help develop and integrate new features in C ++ (and possibly other languages) do not allow this?
You can build an object and immediately call a function on it, you simply cannot assign the object to a variable if you do this:
MyObject().DoBar();
, , , , , , . , .
, , , .
, - ? :
MyObject foo; foo.DoBar();
. ( , ). , , " ". , , , .
, . , ?
:
" , , ".
, ? , , , . .
struct MyObject { MyObject(bool pCallDoBar = true) { if (pCallDoBar) DoBar(); } void DoBar(void) { } }; MyObject foo;
MyObject().DoBar(); // destructor called here, though
, , .
++. , , (, std::unordered_map ). . .
std::unordered_map
C , , ( ), , . , - . , , , .
" ?" " ?" , , ; - , .
, , ( ). ? void this? , , , ++, .
void
this
: , foo , .
foo
MyObject foo(arg1, arg2).DoBar();
,
MyObject foo(arg1, arg2); foo.DoBar();
: !
Note that it is possible to do what you want if DoBar returns a link to *this:
*this
MyObject foo = MyObject().DoBar();
With C ++ 0x move constructors, this will lead to a little overhead, but until then expects a false copy.
Source: https://habr.com/ru/post/1729172/More articles:Python List Indexed by Tuples - pythonASP.NET MVC ViewModel Console - c #point object reference - c ++is there a way to not stop the script if errors are detected? - phpJQuery backgroundPosition Animation Question - jqueryVirtual StringTree: how to determine if node text is fully displayed? - delphiSilverlight / WPF and Blend: DataBind text box, but determines the value of development time? - data-bindingIDE for CodeIgniter runs on Ubuntu Linux - editorSVN: ignore files / folders when updating - svnjquery - page reload - jqueryAll Articles