copy-constructor

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

青春壹個敷衍的年華 提交于 2019-12-30 00:51:09
问题 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

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

家住魔仙堡 提交于 2019-12-30 00:51:09
问题 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

Why is the copy constructor not called?

牧云@^-^@ 提交于 2019-12-29 08:40:09
问题 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

Copy Constructor Needed with temp object

故事扮演 提交于 2019-12-29 05:24:27
问题 The following code only works when the copy constructor is available. When I add print statements (via std::cout ) and make the copy constructor available it is not used (I assume there is so compiler trick happening to remove the unnecessary copy). But in both the output operator << and the function plop() below (where I create a temporary object) I don't see the need for the copy constructor. Can somebody explain why the language needs it when I am passing everything by const reference (or

How to copy (or swap) objects of a type that contains members that are references or const?

瘦欲@ 提交于 2019-12-29 04:45:09
问题 The problem I am trying to address arises with making containers such as an std::vector of objects that contain reference and const data members: struct Foo; struct Bar { Bar (Foo & foo, int num) : foo_reference(foo), number(num) {} private: Foo & foo_reference; const int number; // Mutable member data elided }; struct Baz { std::vector<Bar> bar_vector; }; This won't work as-is because the default assignment operator for class Foo can't be built due to the reference member foo_reference and

why copy constructor is call when we pass an object as an argument by value to a method

爱⌒轻易说出口 提交于 2019-12-28 06:30:31
问题 i am new to C++ programming, when i am doing some C++ programs i have got a doubt that is why copy constructor is called when i pass an object as argument by value to a function. please see my below code in that i am passing a object of class as an argument by value to a function display() but it calling copy constructor and then control is hitting the display() function but i am understanding why it so please help. #include "stdafx.h" #include <iostream> using namespace std; class ClassA {

C++ copy constructor vs overloaded assignment vs constructor best practices?

我只是一个虾纸丫 提交于 2019-12-25 16:26:12
问题 this is more of an opinion/best practices question. I'm new to C++ and I'm currently working on a program that uses dynamically allocated strings. I finally get the difference between the constructors, the copy constructors and overloaded assignment operator. I also get the need for a destructor for these objects. (I'm building an exam object that holds question objects that hold an array of T/F answer objects, each point to dynamic storage for the strings). Here's my question: What is

Dynamic list of integers in C++

放肆的年华 提交于 2019-12-25 07:23:52
问题 I'm trying to create a dynamic array in my List class that will start off with a size of 2 and when you insert values with the Insert method, it will check to see if there is enough space if not it will resize the array with a size + 2... The problem is it is crashing VS is complaining about corruption of the heap. Also I think my copy constructor isn't being called because the cout's arent displaying: list.h File: class List { public: // DEFAULT Constructor List(); // Deconstructor to free

C++ Rule of 5 copy and move (constructor and assignment) caveat: to copy or move

喜你入骨 提交于 2019-12-25 05:26:10
问题 I am writing a c++11+ standards compliant class, and it is time for me to implement the rule of 5. Destructor Copy Constructor Move Constructor Copy Assignment Operator Move Assignment Operator I had a question about copy/move constructors/assignment. It is my understanding that a copy constructor/assignment should make a copy (shallow, deep?) of your class. In the case that your class has unique members, such as a unique_ptr, there are two scenarios I foresee. Make a deep copy of the object

copy constructor,destructor and temporaries

痞子三分冷 提交于 2019-12-25 04:29:10
问题 I wrote this class to test the behaviour of the default constructor,the copy constructor, the assignment operator and the destructor: #include <iostream> class Test { public: Test(); Test(const Test&); ~Test(); Test &operator=(const Test&); private: static int count; int label; }; Test::Test() : label(count++) { std::cout<<"constructor of "<<label<<std::endl; } Test::Test(const Test &other) : label(count++) { std::cout<<"copy-constructor of "<<label<<std::endl; } Test::~Test() { std::cout<<