copy-constructor

Is memcpy of a trivially-copyable type construction or assignment?

守給你的承諾、 提交于 2019-11-30 04:34:57
Let's say you have an object of type T and a suitably-aligned memory buffer alignas(T) unsigned char[sizeof(T)] . If you use std::memcpy to copy from the object of type T to the unsigned char array, is that considered copy construction or copy-assignment? If a type is trivially-copyable but not standard-layout, it is conceivable that a class such as this: struct Meow { int x; protected: // different access-specifier means not standard-layout int y; }; could be implemented like this, because the compiler isn't forced into using standard-layout: struct Meow_internal { private: ptrdiff_t x_offset

C++ : Implementing copy constructor and copy assignment operator

自闭症网瘾萝莉.ら 提交于 2019-11-30 00:50:25
问题 After reading about copy constructors and copy assignment operators in C++, I tried to create a simple example. Though the below snippet apparently works, I am not sure whether I am implementing the copy constructor and copy assignment operator the right way. Could you please point out if there are any mistakes/improvements or a better example to understand the relevant concepts. class Foobase { int bInt; public: Foobase() {} Foobase(int b) { bInt = b;} int GetValue() { return bInt;} int

Which is the difference between declaring a constructor private and =delete?

笑着哭i 提交于 2019-11-30 00:46:09
问题 For example, I want to declare a class but I want the client to not be able to use the copy constructor (or copy assignment operator) Both of the following two does not allow the use of the copy constructor: 1. class Track { public: Track(){}; ~Track(){}; private: Track(const Track&){}; }; 2. class Track { public: Track(){}; ~Track(){}; Track(const Track&)=delete; }; Is one of these ways "more correct" than the other or are equal? Is there any side-effect? //Does not compile with both the

Checklist for writing copy constructor and assignment operator in C++

落爺英雄遲暮 提交于 2019-11-29 22:27:09
Please write a list of tasks that a copy constructor and assignment operator need to do in C++ to keep exception safety, avoid memory leaks etc. Luc Hermitte First be sure you really need to support copy. Most of the time it is not the case, and thus disabling both is the way to go. Sometimes, you'll still need to provide duplication on a class from a polymorphic hierarchy, in that case: disable the assignment operator, write a (protected?) copy constructor, and provide a virtual clone() function. Otherwise, in the case you are writing a value class, you're back into the land of the Orthogonal

In which situations is the C++ copy constructor called?

时光总嘲笑我的痴心妄想 提交于 2019-11-29 20:10:33
I know of the following situations in c++ where the copy constructor would be invoked: when an existing object is assigned an object of it own class MyClass A,B; A = new MyClass(); B=A; //copy constructor called if a functions receives as argument, passed by value, an object of a class void foo(MyClass a); foo(a); //copy constructor invoked when a function returns (by value) an object of the class MyClass foo () { MyClass temp; .... return temp; //copy constructor called } Please feel free to correct any mistakes I've made; but I am more curious if there are any other situations in which the

Is it bad form to call the default assignment operator from the copy constructor?

倾然丶 夕夏残阳落幕 提交于 2019-11-29 17:27:01
问题 Consider a class of which copies need to be made. The vast majority of the data elements in the copy must strictly reflect the original, however there are select few elements whose state is not to be preserved and need to be reinitialized . Is it bad form to call a default assignment operator from the copy constructor? The default assignment operator will behave well with Plain Old Data( int,double,char,short) as well user defined classes per their assignment operators. Pointers would need to

Inheriting copy and move constructors of base class using “using” keyword

ぐ巨炮叔叔 提交于 2019-11-29 16:33:32
I want to inherit copy constructor of the base class using using keyword: #include <iostream> struct A { A() = default; A(const A &) { std::cerr << __PRETTY_FUNCTION__ << std::endl; } A( A &&) { std::cerr << __PRETTY_FUNCTION__ << std::endl; } A& operator=(const A &) { std::cerr << __PRETTY_FUNCTION__ << std::endl; return *this; } A& operator=( A &&) { std::cerr << __PRETTY_FUNCTION__ << std::endl; return *this; } }; struct B : A { using A::A; using A::operator=; B& operator=(const B &) { std::cerr << __PRETTY_FUNCTION__ << std::endl; return *this; } B& operator=( B &&) { std::cerr << __PRETTY

Why is the copy constructor not called?

主宰稳场 提交于 2019-11-29 14:09:21
class MyClass { public: ~MyClass() {} MyClass():x(0), y(0){} //default constructor MyClass(int X, int Y):x(X), y(Y){} //user-defined constructor MyClass(const MyClass& tempObj):x(tempObj.x), y(tempObj.y){} //copy constructor private: int x; int y; }; int main() { MyClass MyObj(MyClass(1, 2)); //user-defined constructor was called. MyClass MyObj2(MyObj); //copy constructor was called. } In the first case, when MyClass(1, 2) calls the user-defined constructor and returns an object, I was expecting MyObj to call the copy constructor. Why it doesn't need to call the copy constructor for the second

std::string x(x);

早过忘川 提交于 2019-11-29 13:27:35
std::string x(x); This crashes very badly on my compiler. Does this mean I should test for this != &that in my own copy constructors, or can I assume that no client will ever be so stupid? Initializing something with itself is undefined behavior, which probably might even mean that once it is invoked you even can't detect it later. Suppose the compiler detects it and out of spite generates assembly for nasal demons, not a call to your copy constructor at all? In practice, you can assume that the client is not that stupid, and if they are it is their business to debug it and figure it out. You

What is the difference between overloading operator= and overloading the copy constructor?

喜夏-厌秋 提交于 2019-11-29 13:25:23
What is the difference between overloading the operator = in a class and the copy constructor ? In which context is each one called? I mean, if I have the following: Person *p1 = new Person("Oscar", "Mederos"); Person *p2 = p1; Which one is used? And then when the other one is used? Edit: Just to clarify a little bit: I already know that if we explicitly call the copy constructor Person p1(p2) , the copy constructor will be used. What I wanted to know is when each one is used, but using the = operator instead, as @Martin pointed. In your case neither is used as you are copying a pointer.