copy-constructor

extraneous calls to copy-constructor and destructor

我怕爱的太早我们不能终老 提交于 2019-12-03 21:12:18
[a follow up to this question ] class A { public: A() {cout<<"A Construction" <<endl;} A(A const& a){cout<<"A Copy Construction"<<endl;} ~A() {cout<<"A Destruction" <<endl;} }; int main() { { vector<A> t; t.push_back(A()); t.push_back(A()); // once more } } The output is: A Construction // 1 A Copy Construction // 1 A Destruction // 1 A Construction // 2 A Copy Construction // 2 A Copy Construction // WHY THIS? A Destruction // 2 A Destruction // deleting element from t A Destruction // deleting element from t A Destruction // WHY THIS? To clearly see what's going on, I recommend include the

copy constructor with default arguments

偶尔善良 提交于 2019-12-03 19:51:02
问题 As far as I know, the copy constructor must be of the form T(const T&) or T(T&) . What if I wanted to add default arguments to the signature? T(const T&, double f = 1.0); Would that be standards compliant? 回答1: Yes. §[class.copy]/2: A non-template constructor for class X is a copy constructor if its first parameter is of type X& , const X& , volatile X& or const volatile X& , and either there are no other parameters or else all other parameters have default arguments [ Example: X::X(const X&)

Constructor or Assignment Operator

梦想与她 提交于 2019-12-03 15:14:07
Can you help me is there definition in C++ standard that describes which one will be called constructor or assignment operator in this case: #include <iostream> using namespace std; class CTest { public: CTest() : m_nTest(0) { cout << "Default constructor" << endl; } CTest(int a) : m_nTest(a) { cout << "Int constructor" << endl; } CTest(const CTest& obj) { m_nTest = obj.m_nTest; cout << "Copy constructor" << endl; } CTest& operator=(int rhs) { m_nTest = rhs; cout << "Assignment" << endl; return *this; } protected: int m_nTest; }; int _tmain(int argc, _TCHAR* argv[]) { CTest b = 5; return 0; }

“Almost default” copy constructor (& assignment operator) in C++

岁酱吖の 提交于 2019-12-03 12:37:14
A common thing I find myself doing is making "almost default" copy constructors and assignment operators. That is, I find myself in situations where the compiler supplied copy and assignment operators would work for most of the data members, but there's a particular data member which needs to be handled differently. This means that I have to explicitly create a copy constructor/assignment operator, including explicitly listing all the data members which have simple copy semantics. This can get annoying for classes where there are a fair number of data members, or later on when member variables

Why is template constructor preferred to copy constructor?

余生长醉 提交于 2019-12-03 10:34:18
问题 #include <iostream> struct uct { uct() { std::cerr << "default" << std::endl; } uct(const uct &) { std::cerr << "copy" << std::endl; } uct( uct&&) { std::cerr << "move" << std::endl; } uct(const int &) { std::cerr << "int" << std::endl; } uct( int &&) { std::cerr << "int" << std::endl; } template <typename T> uct(T &&) { std::cerr << "template" << std::endl; } }; int main() { uct u1 ; // default uct u2( 5); // int uct u3(u1); // template, why? } coliru Template overload of the constructor

Self destruction: this->MyClass::~MyClass() vs. this->~MyClass()

一笑奈何 提交于 2019-12-03 09:27:35
In my quest to learn C++ I stumbled across the article Writing Copy Constructors and Assignment Operators which proposes a mechanism to avoid code duplication across copy constructors and assignment operators. To summarise/duplicate the content of that link, the proposed mechanism is: struct UtilityClass { ... UtilityClass(UtilityClass const &rhs) : data_(new int(*rhs_.data_)) { // nothing left to do here } UtilityClass &operator=(UtilityClass const &rhs) { // // Leaves all the work to the copy constructor. // if(this != &rhs) { // deconstruct myself this->UtilityClass::~UtilityClass(); //

Copy constructor is called many times when constructing a thread by function object

不羁岁月 提交于 2019-12-03 08:28:27
I try to pass a function object to a thread. I am confused when I found the copy constructor is called two times in the 'main' thread. Why not simply copy once instead of twice? The second copy is useless. Code Snippet: #include <iostream> #include <thread> using namespace std; struct A { A() { cout << "constructor this=" << this << " thread_id=" << this_thread::get_id() << endl; } A(const A &other) { cout << "Copy constructor this=" << this << " thread_id=" << this_thread::get_id() << endl; } void operator()() { cout << "operator() this=" << this << " thread_id=" << this_thread::get_id() <<

MATLAB parfor and C++ class mex wrappers (copy constructor required?)

匆匆过客 提交于 2019-12-03 08:02:38
I'm trying to wrap a C++ class in a matlab mex wrapper using the approach outlined here . Basically, I have an initialization mex file which returns a C++ object handle: handle = myclass_init() I can then pass this to another mex file (e.g. myclass_amethod ) which use the handle to call class methods, then ultimately to myclass_delete to free the C++ object: retval = myclass_amethod(handle, parameter) myclass_delete(handle) I've wrapped this up in a MATLAB class for ease of use: classdef myclass < handle properties(SetAccess=protected) cpp_handle_ end methods % class constructor function obj =

Matlab copy constructor

六月ゝ 毕业季﹏ 提交于 2019-12-03 07:38:08
Is there a better way to implement copy construcor for matlab for a handle derived class other than adding a constructor with one input and explicitly copying its properties? obj.property1 = from.property1; obj.property2 = from.property2; etc. Thanks, Dani If you want a quick-and-dirty solution that assumes all properties can be copied, take a look at the PROPERTIES function. Here's an example of a class that automatically copies all properties: classdef Foo < handle properties a = 1; end methods function F=Foo(rhs) if nargin==0 % default constructor F.a = rand(1); else % copy constructor fns

What is the distinction between implicitly-declared and implicitly-defined copy constructors?

你。 提交于 2019-12-03 07:18:50
问题 I am reviewing the cppreference page on copy constructors here: http://en.cppreference.com/w/cpp/language/copy_constructor I've read the 2 sections regarding implicitly-declared copy constructors and implicitly-defined copy constructors quite a few times but I still don't understand the distinction. Wouldn't an implicitly declared but NOT defined constructor result in linker problems? The rules are very complex. I don't remember there being a distinction in C++03: Either you had a compiler