copy-constructor

Copy constructor for object with vector fo object as member variable

删除回忆录丶 提交于 2019-12-10 11:49:10
问题 I have a class A with a vector<Object> object_list_ as a member variable. I know that if I had a vector of pointers I would have needed to write a specific copy constructor to achieve a deep copy. However, in this case, as I don't have pointers, when copying an object of type A , the default copy constructor will automatically also call the copy constructors of Object or each element in object_list_ or do I have to write my own copy constructor to do that? 回答1: The default copy constructor

CArray doesn't call copy constructors on memory reallocations, now what?

痞子三分冷 提交于 2019-12-10 11:32:32
问题 Suppose I have a class that requires copy constructor to be called to make a correct copy of: struct CWeird { CWeird() { number = 47; target = &number; } CWeird(const CWeird &other) : number(other.number), target(&number) { } const CWeird& operator=(const CWeird &w) { number = w.number; return *this; } void output() { printf("%d %d\n", *target, number); } int *target, number; }; Now the trouble is that CArray doesn't call copy constructors on its elements when reallocating memory (only memcpy

Why is class with non-const copy constructor not treated as copy constructible?

旧时模样 提交于 2019-12-10 09:29:16
问题 Given struct Foo { Foo(Foo&) {} }; std::is_copy_constructible<Foo>::value is false Foo has valid copy-constructor: From draft n4659: 15.8.1 Copy/move constructors [class.copy.ctor] 1 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 (11.3.6). [Example: X::X(const X&) and X::X(X&,int=1) are copy constructors

Can a derived class be made uncopyable by declaring copy constructor/operator private in base class?

僤鯓⒐⒋嵵緔 提交于 2019-12-10 04:10:28
问题 I thought in theory the answer to this question was yes. However, in practice, my compiler (VS2010) does not seem to complain in the following situation: I have an abstract base class providing some common interface (yet having no data members) and various sub and subsubclasses derived from it. class Base { public: Base() {} virtual ~Base() {} virtual void interfaceFunction1() = 0; virtual void interfaceFunction2() = 0; private: Base(const Base&); // all derived classes should be uncopyable

c++: cast operator vs. assign operator vs. conversion constructor priority

风格不统一 提交于 2019-12-10 03:50:42
问题 Let's have this code: Test1 t1; Test2 t2; t1 = t2; I believe there are three (or more?) ways how to implement t1 = t2 to overload assign operator in Test1 to overload type cast operator in Test2 to create Test1(const Test2&) conversion constructor According to my GCC testing, this is the priority of what is used: assign operator conversion constructor and type cast operator (ambiguous) const conversion constructor and const type cast operator (ambiguous) Please help me understand why this

Why the copy constructor is not called?

吃可爱长大的小学妹 提交于 2019-12-10 01:59:13
问题 In this code: #include <iostream> using std::cout; class Foo { public: Foo(): egg(0) {} Foo(const Foo& other): egg(1) {} int egg; }; Foo bar() { Foo baz; baz.egg = 3; return baz; } int main(void) { Foo spam(bar()); cout << spam.egg; return 0; } the output is 3 , while I expected it to be 1 . That means the copy constructor is not called in the line Foo spam(bar()) . I guess it's because the bar function doesn't return a reference. Could you please explain what's really going on at the

Deleting copy constructor breaks inherited constructors

点点圈 提交于 2019-12-09 14:40:56
问题 I am trying to use the constructor inheritance feature of C++11. The following snippet (copied from somewhere, I don't remember whence) works completely fine: #include <iostream> struct Base { Base() : Base(0) {} Base(int a) : Base(a, 0) {} Base(int a, double b) { std::cout << "Base(" << a << "," << b << ")" << std::endl; } }; struct Derived : Base { using Base::Base; Derived(const Derived& that) = delete; // This line is the culprit }; int main(int argc, char* argv[]) { Derived d1; Derived

std::string copy constructor NOT deep in GCC 4.1.2?

限于喜欢 提交于 2019-12-09 14:31:06
问题 I wonder if i misunderstood something: does a copy constructor from std::string not copy its content? string str1 = "Hello World"; string str2(str1); if(str1.c_str() == str2.c_str()) // Same pointers! printf ("You will get into the IPC hell very soon!!"); This will print "You will get into the IPC hell very soon!!" and it annoys me. Is this the normal behavior of std::string ? I read somewhere that it usually does a deep copy. However, this works as expected: string str3(str1.c_str()); if

Do the padding bytes of a POD type get copied?

杀马特。学长 韩版系。学妹 提交于 2019-12-09 14:00:21
问题 Suppose I have a POD type like this: struct A { char a; int b; }; On my system, sizeof(A) == 8 , even though sizeof(char) == 1 and sizeof(b) == 4 . This means that the data structure has 3 unused bytes. Now suppose we do A x = ...; A y =x; Question: Is it guaranteed that all 8 bytes of x and y will be identical, even those 3 unused ones? Equivalently, if I transfer the underlying bytes of some A objects to another program that does not understand their meaning or structure, and treats them as

Copy constructor not called, but compiler complains that there's no

落爺英雄遲暮 提交于 2019-12-08 22:03:26
问题 Given the following code: #include <boost/noncopyable.hpp> enum Error { ERR_OK=0 }; struct Filter : private boost::noncopyable { Filter() {} virtual ~Filter() {} virtual int filter(int* data) const = 0; }; struct SpecialFilter : public Filter, private boost::noncopyable { inline SpecialFilter(unsigned int min, unsigned int max) : min(min), max(max) {} virtual ~SpecialFilter() {} virtual int filter(int* data) const { // ... return ERR_OK; } unsigned int min; unsigned int max; }; struct AClass