Questions tagged [copy-constructor]

A copy constructor is a constructor that creates a new object that is a clone of an existing object. The term is mostly used in the C++ programming language, where copy constructors have a special status.

copy-constructor
249 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
6 votes
2 answers
4k views

Is the copy constructor of Java's ArrayList shallow copy or deep copy?

I have long being confused with whether Java's container's copy constructor is shallow copy or deep copy? Below is my understanding: ints1, ints2, ints3 are references so they lie in stack. inst1 ...
蔡纪轮's user avatar
5 votes
0 answers
172 views

Does C++11 standard require implementers to prioritize noexcept move constructor over const copy constructor for std::vector?

Reading this and this and 23.3.6.5/1 of the standard, where in the latest C++ standard draft is it specified that implementers should prioritize the use of non-throwing move-constructor T(T &&...
Tadeus Prastowo's user avatar
5 votes
1 answer
446 views

Copy constructor conflicts with const member function in pch? A gcc bug?

I'm new to gcc, and I'm encountering a really strange problem while compiling precompiled header in gcc-4.7.2/4.7.3. The codes below: FooClass.h #include <cstddef> #include <X11/X.h> #...
Lendy Zhang's user avatar
4 votes
1 answer
157 views

Understanding error when using partial function application or curry in conjunction with overload and std::visit

tl;dr I'd like to understand what's wrong with the first code below, i.e. what the error is telling me. MRE I've been able to shorten the example to the following, which generates the same error as ...
Enlico's user avatar
  • 25.6k
3 votes
0 answers
196 views

Can not general copy constructor in class template be defaulted?

In the following program struct template A has a constructor from const A<int> &. For A<int> it should be copy-constructor, which can be defaulted: template<typename T> struct A {...
Fedor's user avatar
  • 18.5k
3 votes
0 answers
114 views

Copy/move elision in case of delegating to a copy/move constructor

In the following program, the constructor A::A(int) delegates the call to copy/move constructor, which both are explicitly deleted: struct A; A f(); struct A { A() {} A(int) : A(f()) {} A(...
Fedor's user avatar
  • 18.5k
3 votes
0 answers
80 views

Why is "subclass copy constructor calling virtual inherited parent trivial constructor" not recommended

I have a class that virtually inherits from a base class that only has a trivial constructor, but the copy constructor is explicitly deleted. Now I need to provide a copy constructor for this subclass ...
依奈ちゃん's user avatar
3 votes
0 answers
247 views

In C++, placement new and copy constructor implementing copy assignment operator, is it a good practice?

Given the code struct Foo{ Foo(const Foo &other){ i = other.i; }; Foo &operator=(const Foo &other){ if(this == &other){ return (*this); } new (this) Foo(...
Theo Mars's user avatar
3 votes
2 answers
607 views

Is it possible to avoid copy-constructor call in C++

I am writing a template function which accepts a custom class (that can be any class or primitive type) as a template argument, then reads some data (of that type) from an input stream, and then ...
TheMemeMachine's user avatar
3 votes
1 answer
3k views

Does = make_pair Invoke the Copy Constructor?

Normally the = in a variable declaration is treated by the compiler as move construction. For example: vector<int> foo = vector<int>(13, 0); Will call the vector<int>::vector<...
Jonathan Mee's user avatar
  • 38.4k
3 votes
1 answer
127 views

Automation between containers of different objects with copy assign

I have a sparse vector of type std::vector<SparseElement<T,I>> where SparseElement is: template<typename T, typename I = unsigned int> struct SparseElement { I index; T ...
Chameleon's user avatar
  • 1,936
3 votes
2 answers
2k views

how to create copy constructor when the class contains a pointer

I have a problem with copy constructor concept. I wrote an example like this: struct f1 { string x; string y; f1(); ~f1(); }; struct f2 { int a; string b; f1 *ff; ...
F.M's user avatar
  • 495
2 votes
1 answer
100 views

Copying objects between pointers and references C++

I have done some reading starting on the Rule of three and this has given me some exposure to memory management when writing C++ and it is challenging coming from a Java background. I'm just writing ...
Xhyub's user avatar
  • 31
2 votes
0 answers
65 views

Problem in defining of Vector class Copy Constructor (C++)

I tried to define a Vector class and some functions of it manually. Everything seemed like okay, until I wrote the copy constructor, where I get a warning in this part of the loop: this->arr[i] = ...
Samvel Muqelyan's user avatar
2 votes
0 answers
87 views

Why - in that example with an extended lambda - is an ambigious copy constructor and some deleted function

I do not understand the behaviour of the following code: template< bool b > struct Foo { Foo() = default; __host__ Foo( const Foo & ) requires( b ) {} __device__ Foo( const Foo &...
tommsch's user avatar
  • 658
2 votes
0 answers
128 views

Copy-Assignment Operator of class uses (copy and swap) and ordinary implement give different result

#include <iostream> #include <string> #include <vector> #include <algorithm> #include <iterator> #include <memory> using std::cout; using std::vector; class ...
David Zhou's user avatar
2 votes
0 answers
839 views

Cannot assign a reference that has a deleted copy constructor?

I'm in a situation where I need to implement Move Constructor and Move-Assignment Operator for a class which holds a reference to an object with a deleted Copy Ctor and Copy-Assignment Operator, ...
yano's user avatar
  • 4,224
2 votes
2 answers
420 views

In C++, what is the proper way to do copy/move/assignment for objects with a unique id?

/* Example.h file */ class Example { public: Example(const std::string& unique_id_, int attribute1_, int attribute2_,): unique_id(unique_id_), attribute1(attribute1_), attribute2(...
Alex's user avatar
  • 21
2 votes
1 answer
2k views

Pybind11 - class without copy constructor/operator

I have a library that I am trying to interface from C++ to python using pybind11. I am finding myself in the following configuration. I have a C++ class that doesn't allow the copy (copy constructor ...
Emile D.'s user avatar
  • 612
2 votes
0 answers
166 views

How to adapt deep copy patern so it returns by value

I am apologize if it is a stupid problem. I want to do deep copy of a derived class. I have do a search and found there already exist the topic. C++: Deep copying a Base class pointer Copying ...
Xu Hui's user avatar
  • 1,235
2 votes
1 answer
204 views

In c++, Does it make sense to prohibit copy construction if the default construction is prohibited in the first place?

I was going through a code implementation where the intention was to not let anyone make objects of a particular class. Here is the code snippet: class CantInstantiate { CantInstantiate(); ...
arnie7's user avatar
  • 21
2 votes
0 answers
107 views

Vector of instances of a Class

I am trying to create a vector of objects taken from a certain class i created but none of the constructors of that instances of the class are available I have been trying several different ...
JacquesLeen's user avatar
2 votes
0 answers
135 views

Ambiguity resolution and rvalue/lvalue conversion

I wrote this short test code # include <iostream> using std::cout; struct P { P() {cout << "I'm P::P()\n";}; P(const P& p) {cout << "I'm P::P(const P&)\n";} ...
GSi's user avatar
  • 649
2 votes
0 answers
441 views

std::vector seems to prefer copy over move construction when auto-resizing

It seems that when std::vector resizes itself (e.g. when inserting a new element and there not being enough room), it uses its elements' copy constructor rather than their move constructor, even if ...
einpoklum's user avatar
  • 124k
2 votes
1 answer
2k views

C++ writing a copy constructor for simple linked list

Trying to write a simple copy constructor for a linked list class. My class is simple, I have a variable First that points to the first node, and a variable Last that points to the last node. It is ...
Joe Caraccio's user avatar
  • 1,959
2 votes
1 answer
136 views

copy/move/conversion semantic when derived object was bound to base class reference?

I want to understand C++ as much as I can. If there is a base class B and a derived class D. D d; instantiate a derived object. B & rb = d;, rb refers to derived object d. This does NOT ...
Zachary's user avatar
  • 1,673
1 vote
1 answer
79 views

Prototype Design Pattern Example in C++

I am learning prototype design pattern and implementing a demo,I add deep copy constructors and clone functions to these classes,but I'm not sure if I implemented them correctly. I did the ...
bbbg's user avatar
  • 11
1 vote
0 answers
91 views

emplace lambda task that used packaged_task into a queue

So I have this enqueue implementation for a threadpool template <typename F, typename... Args> auto enqueue(F&& f, Args&&... args) -> std::future<decltype(f(...
lior's user avatar
  • 61
1 vote
1 answer
124 views

Problem with assignment operator when assigning partially only

Consider this code. I have defined a copy constructor for Person that will copy all data except for one, namely its DontCopy data member. But I can't seem to get the same thing done with the ...
prestokeys's user avatar
  • 4,825
1 vote
0 answers
190 views

Explicit deletion of copy constructor and assignment in Pybind11

If you have a class with explicit deletion of the copy constructor and assignment operation as in the following example Foo(const Foo& other) = delete; Foo& operator=(const Foo&...
A. Fenzry's user avatar
  • 444
1 vote
1 answer
89 views

Why are these objects returned by copy and not by move?

Why are the value instances returned by getA() and getA2() returned using the copy constructor and not the move constructor? I'm using visual studio community 2017, but godbolt gcc/20 and onlinegdb ...
Gonen I's user avatar
  • 5,727
1 vote
0 answers
122 views

Lambda factor copyable or moveable

A lambda is just an instance of a nameless class. If it doesn't capture anything, it won't even have any member variables. I think whether a specific lambda is copyable or moveable lies on whether the ...
John's user avatar
  • 3,271
1 vote
0 answers
460 views

C++: Copy constructor call using curly braces

Suppose I have the following piece of code where the only things about Type I know are: Type does not implement constructor with signature Type(std::initializer_list<Type>) i.e. constructor ...
cerveka2's user avatar
  • 465
1 vote
0 answers
174 views

Cpp/C++ redefine operator=

I have a class foo with an unique int ID and a string name. The ID is automatically assigned at construction and the name is an argument of the constructor. The copy constructor is =deleted. Now I ...
Finn's user avatar
  • 47
1 vote
2 answers
434 views

Program uses Copy Constructor instead of Move Constructor

I am trying to understand the concept of copy and move constructors in C++. And so trying out different examples. One such example whose output i am unable to understand is given below: #include <...
user avatar
1 vote
0 answers
101 views

Why is copy constructor called twice? C++

So, I'm creating a class BigInteger for better understanding of how objects in class are constructed. So consider this code: #include <iostream> #include <vector> #include <algorithm>...
Learpcs's user avatar
  • 282
1 vote
0 answers
1k views

Copy Constructor for Circular Singly Linked List

So I have been coding in C++ for not so long and am struggling with a copy constructor and my program keeps on crashing when I use it. Node: template <class T> class Node { public: Node(T ...
Morgan Else's user avatar
1 vote
0 answers
49 views

how should I write this copy constructor?

I have class model: class model { private: link_list list; float parameter_B1; float parameter_B0; public: model(); float getparameter_B1() const; float getparameter_B0() ...
hanie's user avatar
  • 1,863
1 vote
1 answer
369 views

Initialising member aggregate type without copy-constructor

I need to initilise an member array of a class with a non-default constructor and without using the copy constructor. I have the following two classes: class MemberClass { public: MemberClass(...
Owen Binchy's user avatar
1 vote
0 answers
78 views

C2280: boost::container::map of uncopyable value type can't be std::move()'d?

I have an uncopyable class Uncopyable stored in boost::container::map<K, Uncopyable>. Another class, Owner, has an object of such map. The implementation of Owner::Owner(Owner&&) ...
passing_through's user avatar
1 vote
2 answers
96 views

How do I invoke copy constructor appropriately while using rvalue for an object?

Here I am trying to push an object into a vector using push_back function and rvalue of that object. Look at the addMovie() function of the Movies class below #include "Movies.h" void Movies::...
Parag 's user avatar
  • 65
1 vote
0 answers
72 views

Copy constructor called on object instantiation

I may just be miss reading this code but in my attempt to call the default constructor, the code instead calls the copy constructor on my proposed creation. Could someone give me a knowledge bomb on ...
Theko Lekena's user avatar
1 vote
1 answer
265 views

How to make the operator= in a Vector class?

I don't know, which is not correct, the copy constructor or the operator=. I tested with two "tombs", and the printer is working, but at the end of the program the compiler said "debug assertion ...
Zsembes's user avatar
  • 11
1 vote
0 answers
230 views

Memory leak,copy constructor,assign operator

Everthing is in the code Here is code. Constructor's. Polynomial::Polynomial() { base=0; Coefficients=new int [base+1]; Coefficients[0]=0; } Basically creates Poly equals to 0; ...
user avatar
1 vote
1 answer
317 views

Copy Constructor Assertion Error

My copy constructor is failing due to an assertion error. It is saying that the size of the queue is incorrect and I'm not sure why. Here is the code for my Queue class: public class Queue<T> ...
Jonny Gjeltema's user avatar
1 vote
0 answers
29 views

Assignment operator for a structure

I'm trying to assign some default values to a structure elements. My structure is as follows: struct SourceProperties { SoundTypes m_SoundTypes; ...
GGame's user avatar
  • 13
1 vote
1 answer
3k views

Copy constructor of a doubly linked list (c++)

I have been having trouble implementing a copy constructor for a doubly linked list. My professor provided a skeleton for a DLL class and i am implementing the functions as part of a project. ...
Geedubs123's user avatar
1 vote
0 answers
50 views

How to properly design and implement CopyConstructable interface in C++?

This question was a precursor to the current one. Need to come up with better OOP design. Problem desription: there is a class called FileWriter which uses object of class Formatter to write a ...
peetonn's user avatar
  • 2,972
1 vote
1 answer
163 views

C++ algorithm using reference or new value

I have a question about using functor return value and parameter. Lets have this code: int op_increase (int i) { return ++i; } int main () { std::vector<int> foo; std::vector<int> ...
Bobul Mentol's user avatar
1 vote
0 answers
669 views

How to make a deep copy, when shared pointers are involved?

I am having a structure of three classes A, B and C, which can have ownership of each other. There can be two objects having ownership of the same object. There can not be a circular dependency. You ...
Luca Fülbier's user avatar

1
2 3 4 5