copy-constructor Questions

3

Solved

I have a class which is publicly inherited from QWidget: class MyWidget : public QWidget { Q_OBJECT public: MyWidget(const MyWidget& other) : obj1(other.obj1), obj2(other.obj2) private: ...
Bacteroid asked 30/9, 2013 at 10:58

4

Solved

I want to refresh my memory on the conditions under which a compiler typically auto generates a default constructor, copy constructor and assignment operator. I recollect there were some rules, bu...

3

Recently we had a bug in our system that is caused by forgetting to assign a newly added class property in the copy constructor. For example: public class MyClass { private Long companyId; pri...
Seng asked 19/12, 2018 at 17:54

3

Solved

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; } privat...
Breakwater asked 1/8, 2023 at 7:40

2

Solved

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...

10

Solved

Why must a copy constructor's parameter be passed by reference?
Maletta asked 21/4, 2010 at 19:12

11

Solved

Today in university I was recommended by a professor that I'd check for (this != &copy) in the copy constructor, similarly to how you should do it when overloading operator=. However I question...
Tangram asked 1/4, 2011 at 19:3

5

I know the general syntax of copy constructor in c++ would take reference. But, my doubt is what happens if we use pointer type instead of reference? Why don't we use pass by pointer mechanism in...
Flowerpot asked 12/1, 2015 at 8:59

6

Solved

After reading this recent question by @Mehrdad on which classes should be made non-movable and therefore non-copyable, I starting wondering if there are use cases for a class which can be copied bu...

8

Solved

In C++, the concept of returning reference from the copy assignment operator is unclear to me. Why can't the copy assignment operator return a copy of the new object? In addition, if I have c...

2

Solved

Consider the following program: #include <vector> #include <iostream> class A { int x; public: A(int n) noexcept : x(n) { std::cout << "ctor with value\n"; } A(const...
Bangweulu asked 10/10, 2022 at 8:32

2

Solved

Does TypeScript support copy-constructor (like for example C++ does)? When the answer is no (or not yet), then what is the best practice to initialize our base-class (which we extend), and copy fro...
Erotic asked 10/2, 2019 at 5:34

1

Solved

I have this example struct B { B(); }; struct D : B { }; D d{ B() }; // what happens here? or why it's well-formed This is an aggregate initialization, but I can't understand how d is constructed?...
Seadon asked 3/5, 2022 at 16:25

2

Solved

Consider the following example: struct Parent { Parent (); Parent (const Parent &); }; struct Child : public Parent { using Parent::Parent; }; Parent p; Child c (p); This was taken fr...
Forester asked 13/9, 2019 at 15:57

3

Solved

I have extended std::string to fulfil my needs of having to write custom function build into string class called CustomString I have defined constructors: class CustomString : public std::strin...

3

After some searching I didn't found any good answer to my question regarding copy constructor and inheritance. I have two classes: User and Trainee. Trainee inherits from User and two String parame...
Lasonde asked 29/11, 2012 at 21:29

1

Solved

I stumbled over the following piece of code. The "DerivedFoo" case produces different results on MSVC than on clang or gcc. Namely, clang 13 and gcc 11.2 call the copy constructor of Foo ...
Jacobsen asked 6/2, 2022 at 18:25

2

Solved

I have some pretty complicated objects. They contain member variables of other objects. I understand the beauty of copy constructors cascading such that the default copy constructor can often work....
Yazzie asked 30/1, 2022 at 1:53

1

Solved

In the following program struct A has a constructor template A(T) requiring that the type T be copy-constructible. At the same time A itself must have implicitly defined copy-constructor: #include ...
Valet asked 27/1, 2022 at 7:19

6

Solved

I have the following field and constructor: private final Properties properties; public PropertiesExpander(Properties properties) { this.properties = properties; } The good practice is to make...
Gladi asked 6/8, 2015 at 20:0

4

Solved

What are pros and cons of duplication an object instance with constructor or instance function? Example A: type TMyObject = class strict private FField: integer; public constructor Create(s...
Makings asked 28/10, 2010 at 10:5

6

Solved

I haven't ever heard it's possible, but asking with the hope that it might. For a class with many more member variables than this: class A { public: SomeOtherClass* s; int i; int j; A() {} A...
Skiascope asked 17/6, 2011 at 13:25

6

Solved

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...
Riha asked 28/9, 2011 at 8:52

4

Solved

In More Effective C++, Scott Meyers says C++ specifies that an object thrown as an exception is copied. I suppose then, that if the copy constructor throws an exception in turn, std::terminate...
Pilloff asked 8/2, 2014 at 10:16

2

Solved

After reading about copy elision of cppreference I wanted to play with exceptions and copy elision. So I wrote the following code with gcc 7.2 on coliru #include <iostream> class Exceptio...
Dennie asked 15/1, 2018 at 8:20

© 2022 - 2024 — McMap. All rights reserved.