copy-constructor Questions
3
Solved
Ok so I ran into a problem when implementing a c# property like system in c++ (see: https://mcmap.net/q/16573/-c-like-properties-in-native-c).
Consider the following example:
struct TransformCmp
{
...
Phagy asked 28/7, 2021 at 11:46
10
Solved
I have recently discovered that when I have pointers within a class, I need to specify a Copy constructor.
To learn that, I have made the following simple code. It compiles, but gives me runtime e...
Emblazonment asked 23/4, 2009 at 13:39
1
Solved
I'm using VS Code to compile and debug with g++ in Linux.
Needed includes and usings:
#include <string>
#include <iostream>
using namespace std;
Here is my class which is moveab...
Hurtado asked 25/6, 2021 at 7:33
2
Solved
Qt defines Q_DISABLE_COPY as follows:
#define Q_DISABLE_COPY(Class) \
Class(const Class &) = delete;\
Class &operator=(const Class &) = delete;
Q_DISABLE_COPY is used in the QObject ...
Dorweiler asked 22/6, 2021 at 5:29
3
Solved
For properly handling object copying, the rule of thumb is the Rule of Three. With C++11, move semantics are a thing, so instead it's the Rule of Five. However, in discussions around here and on th...
Dermatoglyphics asked 18/8, 2017 at 10:17
2
Solved
For the following C++14 code, why does g++'s generated code for new A[1]{x} seem to invoke the copy constructor twice?
#include <iostream>
using namespace std;
class A {
public:
A() { cout ...
Internode asked 15/5, 2021 at 3:47
2
Solved
I'm using this two classes
// This is generic data structure containing some binary data
class A {
public:
A();
A(const A&);
~A();
}
// Main data container
class B {
public:
B();
B( const...
Facesaving asked 14/1, 2012 at 21:50
7
Solved
I need to write a copy constructor that deep copies the contents of a std::shared_ptr. However, there are a bunch of variable int a, b, c, d, e; also defined in the class. Is there a way to generat...
Daryldaryle asked 4/9, 2011 at 2:17
6
Solved
If I override operator= will the copy constructor automatically use the new operator? Similarly, if I define a copy constructor, will operator= automatically 'inherit' the behavior from the copy co...
Decarbonate asked 20/3, 2011 at 11:41
4
Solved
What is the use of copy constructor while the same can be done with assignment operator '='?
Why C++ provide a copy constructor? The assignment operator can do the same task. Is there any advantage of copy constructor over assignment operator?
Rathskeller asked 23/2, 2021 at 20:7
2
Solved
I wasn't able to find a concrete answer for the following question:
Consider the following code:
Obj f() {
Obj o2;
return o2;
}
int main() {
Obj o1 = f();
return 0;
}
How many times is the co...
Aga asked 30/1, 2021 at 20:39
5
Solved
Given a base class that is inherited by plethora of derived classes, and a program structure that requires you manage these via base class pointers to each entity. Is there a simple way to copy the...
Drachma asked 17/2, 2011 at 10:3
2
I'm trying to use the ExprTk mathematical expression parser library within a class whose objects are to be stored in a vector of objects, which is a member variable of another class; however, when ...
Pogge asked 15/10, 2020 at 17:50
7
Solved
This snippet is compiled without errors in Visual Studio 2013 (Version 12.0.31101.00 Update 4)
class A
{
public:
A(){}
A(A &&){}
};
int main(int, char*)
{
A a;
new A(a);
return 0;
}
...
Goodden asked 7/7, 2015 at 9:37
3
Solved
Please take a look on the following example:
class Base
{
protected:
int m_nValue;
public:
Base(int nValue)
: m_nValue(nValue)
{
}
const char* GetName() { return "Base"; }
int GetValue() ...
Noctiluca asked 16/2, 2012 at 10:26
1
I'm writing some custom library like stl, but without allocations in ctors and with disabled copy ctors in resource owning classes (because the environment doesn't support exceptions and all allocs...
Prejudge asked 22/4, 2020 at 12:37
4
Solved
My aim is to keep an std::thread object as data member, and initialize it when needed.
I'm not able to do this (as in my code below) because the copy constructor of the std::thread class is deleted...
Brahms asked 22/8, 2013 at 9:6
3
Solved
Why use references to the parameters of the copy constructor?
I found a lot of information saying that it is to avoid unlimited calls, but I still can't understand it.
Simmon asked 2/4, 2020 at 9:1
3
I'm having a problem with a certain task, it's an excercise, not a real program. The task is to define a copy constructor of structure D that behaves in the exactly same way as a copy constructor g...
Agulhas asked 9/11, 2014 at 19:16
2
In the past, I've said to safely copy a collection do something like:
public static void doThing(List<String> strs) {
List<String> newStrs = new ArrayList<>(strs);
or
public...
Tortuga asked 9/3, 2020 at 13:25
1
Solved
This question is motivated by this one.
Consider the following code:
struct B {};
struct S {
B b; // #1
S() = default;
template <typename ...dummy> // #2
constexpr S(const S&) {}...
Ostend asked 1/3, 2020 at 5:24
4
Solved
Is it valid to copy a struct some of whose members are not initialized?
I suspect it is undefined behavior, but if so, it makes leaving any uninitialized members in a struct (even if those members...
Deckhand asked 7/2, 2020 at 11:45
3
Assume I have an object with both copy and move assignment operators defined. When I write this:
Object a(/*parameters 1*/);
/* some code */
a = Object(/*parameters 2*/);
the third line will mos...
Annmarie asked 30/1, 2020 at 15:27
2
Solved
Why it is useful to explicitly 'disable' or delete the = operator and copy constructor of a class:
SomeClass& operator=(SomeClass&) = delete;
SomeClass(SomeClass&) = delete;
I guess th...
Millimeter asked 15/12, 2015 at 14:36
3
Suppose I create a shared pointer with a custom deleter. In the following code I would like to check what happens with the deleter object itself:
struct A {
A() { std::cout << "A\n"; }
~A(...
Barbital asked 18/12, 2019 at 3:39
© 2022 - 2024 — McMap. All rights reserved.