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.

Filter by
Sorted by
Tagged with
-2 votes
0 answers
101 views

C++ Copy constructor in template is never called and the default one is used instead, why? [closed]

For a bit of context: I'm making a game on Nintendo DS in C++ and in this game I made a template object named CircularDeque. It's basically a Deque but with a circular structure. I am trying to create ...
Zeyzer's user avatar
  • 1
0 votes
1 answer
77 views

Deleting copy/move constructor for singleton class in cpp

The below code snip from a singleton class(claiming to be!!) is used for returning the singleton instance of the class using reference. The copy/move constructors were not marked as deleted in this ...
meetanandkr's user avatar
2 votes
1 answer
95 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
0 votes
0 answers
89 views

segmentation fault (can solve with initialization list, can't with copy constructor)

I'm having segmentation fault error in my code: #include <SFML/Graphics.hpp> #include "game.hpp" #include "map.hpp" #include "button.hpp" class Game { private: ...
DR4NKR1D3R's user avatar
0 votes
1 answer
86 views

How to write copy/move constructors with delegated constructors and conditional initialiser lists

I'm close to finishing my container but my last problem to solve is how to handle the copy/move constructor and appropriately construct the correct member variable inside the private union member ...
DWil's user avatar
  • 33
2 votes
0 answers
64 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
1 vote
1 answer
123 views

c++ when do vector push_back deep copy objects?

I created a vector and used push_back to put several node objects into it. However, I can't predict when its going to use the move constructor or copy constructor. Is there any pattern to when ...
qwert789812's user avatar
0 votes
0 answers
59 views

Boost Bimap with unique_ptr

I am trying to insert unique_ptr into the boost::bimap, but i am getting the error "call to implicitly deleted copy constructor". I am moving the unique_ptr through std::move and i have move ...
Tharani B's user avatar
-5 votes
2 answers
112 views

Does bitwise copy constructor really share same memory location for objects? [closed]

Hey I am confused regarding memory allocation during bitwise copy of objects in C++. Here's what the book by Herbert Schildt C++ fourth edition Pg:366 says: Let's begin by restating the problem that ...
CREATIVITY Unleashed's user avatar
-1 votes
1 answer
77 views

Trying to figure out a Copy Constructor for the course class

This is the course class in question. class Course { //** You may not change the declarations in this private: area. CourseName name; ///< Name of the course int ...
TobyFromHR's user avatar
0 votes
1 answer
76 views

Understanding Copy Constructor and Temporary Object Creation in C++ When Passing Objects by Value

Given the next code: #include <iostream> #include <vector> #include <cstring> class Person { public: Person(const char *i_Name = "Unknown", int i_Age = 0) : ...
Yarin0600's user avatar
-1 votes
1 answer
29 views

copy static array in a dynamic array in c++

The main goal of this code is to copy part of the strings from myArray to neuArray and then output the copied strings from neuArray to the console. the code is working but not 100% because i get the ...
El ayoub's user avatar
3 votes
1 answer
119 views

Why compiler always choose non explicit constructor for copy-list-initialization?

For below code, is there any reason compiler will choose non-explicit constructor.. struct S { S() = default; explicit S(S & cp) { std::cout << "explicit" << ...
Kishan Parmar's user avatar
0 votes
0 answers
49 views

In a chaining call, is copy assignment called? [duplicate]

I would like to understand when a copy constructor or assignment is called. Assume the following: class Foo { public: Foo() { cout << "foo constructor " << this <&...
Sam's user avatar
  • 1
6 votes
3 answers
246 views

Copy semantics and vectors

I am dealing with objects that allocate memory for internal use. Currently, they are not copyable. E.g. class MyClass { public: MyClass() { Store = new int; } ~MyClass() { delete Store; } ...
user avatar
2 votes
0 answers
79 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
  • 602
1 vote
1 answer
84 views

Will copy constructor be called when calling insert of std::unordered_map

I have a std::unordered_map and I found that the object I insert into it is different from that I got from it by using for range traversing. I doubt that there might be some object copying happening ...
Chansy's user avatar
  • 29
0 votes
1 answer
59 views

Excessive Invocation of Copy Constructor for Custom Comparator Class in C++ Priority Queue

I had declared a priority_queue with custom comparator having a vector property in it. Below is the full code for it: CODE #include <bits/stdc++.h> using namespace std; class Compare { private:...
Neeraj-Kumar-Coder's user avatar
6 votes
2 answers
164 views

mutable data member, template constructor and trivially copy constructible

Example code could be found below or on godbolt. Say we have 4 classes: S<T>: holding a data member. SCtor<T>: holding a data member and has a template constructor. SCtorMutable<T>...
wanghan02's user avatar
  • 1,257
2 votes
2 answers
231 views

Constructor implementation preference for move and copy semantics C++

Whenever I want to implement a class that needs a move constructor and some form of a copy constructor I find myself wondering about the following: For an example class C that stores a std::vector<...
huzzm's user avatar
  • 557
1 vote
1 answer
124 views

Why does the copy constructor is not called?

#include <iostream> #include <memory> using namespace std; class Init { private: int x; public: Init(int y) { x = y; cout << "default constructor ...
chandu's user avatar
  • 75
1 vote
1 answer
57 views

An error occurred while constructing a 3D vector class

I'm debugging a 3D vector code, but I get the following error /usr/bin/g++ -fdiagnostics-color=always -g /home/fangrui/vectorFEM/vectorFEM/main.cpp -o /home/fangrui/vectorFEM/vectorFEM/main In file ...
ojipadeson's user avatar
3 votes
0 answers
88 views

When does an rvalue reference result in a move vs copy constructor and why? [duplicate]

I am confused about explicit usage of the rvalue reference. Suppose we have a class named ClassX and it has all copy and move methods. When I execute the code below, different copy methods have been ...
Suat Mutlu's user avatar
2 votes
2 answers
161 views

Clang error: calling a private constructor while none of them were actually called

I made a static function of some class, and made constructor private. Now I want to use the function. Clang++ says "The constructor is private", while g++ compiles normally. I don't know if ...
Ya Ihniy's user avatar
0 votes
3 answers
111 views

C++ call to function by reference and copy

void ref(std::string& str) { (void)str; } void copy(std::string str) { (void)str; } int main() { std::string str = "Hello World"; for(size_t i = 0; i < 10000; i++) ...
Maxou's user avatar
  • 37
0 votes
1 answer
55 views

How to set two similar msg's attributes with less duplicate codes in C++?

How to set variable properties use less duplicate code in C++? A.proto syntax = "proto3"; package Test1; message A{ double x = 1; double y = 2; double z = 3; double theta = ...
aliveforu's user avatar
-1 votes
1 answer
180 views

Query about Copy Constructor and Assignment operator in C++

I am trying to overloading a string class.In the code shown below I am expecting only overloaded Assignment operator to be invoked but overloaded copy constructor is also invoked. Can anybody please ...
user3395789's user avatar
0 votes
1 answer
123 views

error: no matching function for call to copy constructor, c++

Sorry in advance for what may be a bad post. I've scoured stackoverflow for pre existing posts that answer my question, but although many posts on here are similar, none of them seem to apply to my ...
JohnZ's user avatar
  • 93
0 votes
2 answers
102 views

Unit test that the container uses memcpy optimization

Let's say there is some custom std::vector-like container that uses memcpy optimization in it's copy constructor to memcpy trivially constructible objects instead of calling multiple copy constructors....
Voivoid's user avatar
  • 461
4 votes
2 answers
134 views

Class constructor compiles on clang but rejected on gcc

I am learning about constructors in C++. In particular, that a C++ class can have more than one constructor. But then I made the following program that worked with clang but not with gcc and msvc. ...
Baki's user avatar
  • 101
-1 votes
1 answer
130 views

How to perfectly forward a universal reference that is either a const ref or a movable rvalue?

I have coded a lock-free and thread-safe ring queue with C++20, and it works so far. The only thing is not perfect that it has to have two enque() methods, one accepts a const reference to a lvalue as ...
Leon's user avatar
  • 1,661
0 votes
1 answer
44 views

Why the copy assignment push_back element to the existing object?

I created a class named Sentence: class Sentence { public: vector<Clause> Clause_arr; Sentence() {}; Sentence(const Sentence& s) { for(int i = 0; i < s.Clause_arr.size(...
Jacob's user avatar
  • 79
1 vote
1 answer
71 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
0 votes
1 answer
113 views

How to use copy assignment operator with linked list c++

I have to use copy constructor and copy assignment operator to test a linked list. list1 has 3 doubles appended to it list2 = list1 where the copy constructor comes in. List4 = list3 = list1 is where ...
Mindset's user avatar
0 votes
3 answers
190 views

How to make copy constructor copy also virtual table?

Here is simple code snippet. I want to have I-A-B class inheritance so that I's copy constructor can also initialize in-place virtual table of derivate class which inherits from I. For simplicity sake ...
TarmoPikaro's user avatar
  • 4,853
3 votes
1 answer
74 views

How to leverage compiler-generated copy/move ctors when using unique_ptr as a data member?

Say I have a class with a unique_ptr data member, and I want the class to support copying, by deep-cloning the object the smart pointer is pointing at: #include <iostream> #include <memory>...
davidA's user avatar
  • 12.8k
-2 votes
1 answer
187 views

alternative copy constructor in c++

Edit: I am going to introduce some badly designed class, this was an old question and I have grown as a programmer since then... Let's say I have a class with some state and with a method that only ...
ugo_capeto's user avatar
0 votes
1 answer
74 views

How to save additional copy construct in structured return?

If I want to return some struct in the way of structured return. Like pair<bool, vector<int>> get(const string &id) { vector<int> v; return {true, v}; } or struct st{}; ...
f1msch's user avatar
  • 585
1 vote
2 answers
72 views

How to disable overly generic forwarding constructor in c++17 and defer to copy constructor [duplicate]

If I have the simple class written using c++20 concepts #include <array> #include <type_traits> template <class T, int N> struct MyVec { std::array<T, N> m_vec; ...
bradgonesurfing's user avatar
3 votes
1 answer
227 views

C++ unordered map requiring copy constructor when adding my custom hash function

I am trying to use a custom hash struct for class A which is a key type in an std::unordered_map which is one of std::variant alternatives in class B. Here is a simplified version of my code where the ...
TheMemeMachine's user avatar
1 vote
2 answers
71 views

Issues with copy constructor in custom Matrix class

I have a custom matrix class, that works fine in and of itself. The issue I'm running into is that when I try to create a new matrix defined by another matrix, the whole matrix isn't copied. The ...
Herman Neple's user avatar
0 votes
1 answer
128 views

Which way is preferred to call copy constructor for a class?

Below is sample class written with copy constructor in c++. class Foo { public: Foo() { std::cout << "Foo::Foo\n";} ~Foo() { std::cout << "Foo::Foo\n";} ...
Mantosh Kumar's user avatar
1 vote
1 answer
66 views

assigning a class variable outsite constructor body [duplicate]

I am very new to C++, and am following along with this tutorial and have come across this class constructor: class Player: public Entity { private: std::string m_Name; public: Player(const std:...
dg141's user avatar
  • 57
0 votes
3 answers
97 views

Writing the Rule of Three for a 2D map in C++

I am making a program that determines if a tweet is happy or sad, and I was thinking I tokenize the tweets, then create a map that stores the word as the key, how many times it was used in total, and ...
user83975's user avatar
2 votes
1 answer
192 views

Cannot use std::map::emplace to insert an object of a class with no copy constructor

I'm trying to compile the following code: #include <map> #include <condition_variable> class MyClass { public: MyClass(): m_cv() {} std::condition_variable m_cv; //just to ...
Silverspur's user avatar
1 vote
1 answer
55 views

Can class with const members be assigned or copied

According to abseil.io/tips/177, it said Specifically, if your class has const members, it cannot be assigned to (whether by copy-assignment or move-assignment). The language understands this: if ...
Biear's user avatar
  • 53
0 votes
0 answers
199 views

Why does assigning a capturing lambda to std::function result in more copies [duplicate]

In the following code, assigning to an std function results in more copy constructor calls than assigning to an auto variable. ( actually initializing, not assigning ;) ) I get that a value captured ...
Gonen I's user avatar
  • 5,596
1 vote
1 answer
119 views

How to use a copy constructor to create a deep-copy of an Object array's contents without implementing cloneable() or arraycopy()?

I need to create a copy of an array's contents, which are made up of various Objects of different types. However, I cannot use explicit casting to check the type of the Object, nor use the cloneable ...
Gray's user avatar
  • 21
0 votes
0 answers
31 views

Forcing copy constructors of child classes to lock mutex declared in base [duplicate]

I have a base class with a mutex since all inheriting classes need the ability to lock state. I want to force all children copy and assignment constructors to lock the mutex, ideally without having to ...
user3641187's user avatar
2 votes
1 answer
142 views

What is the purpose of copy constructor during return from a function? [duplicate]

#include <iostream> using namespace std; class samp { int *p; int len; int idx; // idx denotes the number of elements currently in the array (allocated by p) public: samp()...
Preetom Saha Arko's user avatar

1
2 3 4 5
49