if I compile (under g ++) and run the following code, it prints "Foo :: Foo (int)". However, after private instance constructors and assignment operators are closed, it cannot compile with the following error: "error:" Foo :: Foo (const Foo &) is private. "How does it need a copy constructor if it calls the standard constructor at runtime?
#include <iostream> using namespace std; struct Foo { Foo(int x) { cout << __PRETTY_FUNCTION__ << endl; } Foo(const Foo& f) { cout << __PRETTY_FUNCTION__ << endl; } Foo& operator=(const Foo& f) { cout << __PRETTY_FUNCTION__ << endl; return *this; } }; int main() { Foo f = Foo(3); }
The copy constructor is used here:
Foo f = Foo(3);
This is equivalent to:
Foo f( Foo(3) );
where the first set of parens re calls the copy constructor call. You can avoid this by saying:
Foo f(3);
, , ( ). ++ (. 12.8/15), .
, , , . (, IO ).
, , , .
.
. , , .
12.8.15
, , / . , , .111). ( ):- return class return type, cv- ,- , (12.2) cv- ,
, , / . , , .111). ( ):
- return class return type, cv- ,
- , (12.2) cv- ,
. " ".
Source: https://habr.com/ru/post/1708639/More articles:Можно ли запустить клиентское приложение из Silverlight? - certificateDeploying biztalk on developer / build machines - nantstructured / combined log with Log4J - javaSql server function to display word frequency in a column - stringGenerics C # .net - genericsCheck MVC.net installed from the Web Setup project - asp.net-mvcPaste SVG into HTML (for Opera) - htmlList replacements with sed or awk - vimtd colspan does not work when using jquery show / hide () - jqueryAvoid code duplication when checking input - user-interfaceAll Articles