Questions tagged [default-copy-constructor]

56 questions
60
votes
5 answers

C++ implicit copy constructor for a class that contains other objects

I know that the compiler sometimes provides a default copy constructor if you don't implement yourself. I am confused about what exactly this constructor does. If I have a class that contains other objects, none of which have a declared copy…
Jamison Dance
  • 19,896
  • 25
  • 97
  • 99
51
votes
1 answer

Call to implicitly deleted copy constructor in LLVM

As per C++11 rules 6 things (default constructor, copy constructor, move constructor, copy assignment, move assignment and destructor ) are generated by default. By second rule, when any custom copy, move or destructor is defined then those default…
cpp_hex
  • 603
  • 1
  • 5
  • 7
36
votes
7 answers

Is C++'s default copy-constructor inherently unsafe? Are iterators fundamentally unsafe too?

I used to think C++'s object model is very robust when best practices are followed. Just a few minutes ago, though, I had a realization that I hadn't had before. Consider this code: class Foo { std::set set; …
user541686
  • 205,094
  • 128
  • 528
  • 886
15
votes
7 answers

C# Automatic deep copy of struct

I have a struct, MyStruct, that has a private member private bool[] boolArray; and a method ChangeBoolValue(int index, bool Value). I have a class, MyClass, that has a field public MyStruct bools { get; private set; } When I create a new MyStruct…
3Pi
  • 1,814
  • 3
  • 19
  • 30
14
votes
1 answer

In C++0x, do non-static data member initializers override the implicit copy constructor?

According to N2628 related to c++0x, non-static data member initializers can be overridden by explicitly defined constructors, but it appears to be slightly nebulous about the implicitly defined copy constructor. In particular, I've noticed that…
Will Bradley
  • 1,733
  • 15
  • 27
10
votes
5 answers

Assignment operator and copy constructor in the presence of references

I am just experimenting with the references using this code: class A { }; class B { public: B(A& a): m_a(a){} A& m_a; }; int main() { A a; B b(a); B b1 = b; } I was expecting both B b1 = b; to produce a error. Instead when I…
Asha
  • 11,002
  • 6
  • 44
  • 66
10
votes
2 answers

Why copy constructor is not invoked?

Sorry for the overly ambiguous title.(due to the lack of my English skill). Please suggest a better title. Please consider the following code. struct A { typedef std::vector State; // template // A(Args...…
Sungmin
  • 2,499
  • 3
  • 26
  • 32
7
votes
2 answers

Do I still get default copy constructor and operator= if I define ones with non-const arguments?

In C++, if I define a copy constructor and operator= that take a non-const reference to the class, is the compiler supposed to still supply default versions for const reference? struct Test { Test(Test &rhs); Test &operator=(Test…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
5
votes
3 answers

Java object copy best option?

Possible Duplicate: How do I copy an object in Java? I need to copy an object in Java (i.e. copy the object "by value not by reference" so that the new object is not just a reference to the old). I'm weary of implementing clonable and would…
aeq
  • 10,377
  • 15
  • 45
  • 46
5
votes
2 answers

Intercept C++ implicit copy constructor, or invoke its functionality

Given: class Foo { private: static int cntFoos; //... stuff... public: Foo() { cntFoos++; } ~Foo() { cntFoos--; } }; ... where "stuff" may be any set of properties. (The idea is to have a counter of instances of that…
tru7
  • 6,348
  • 5
  • 35
  • 59
5
votes
4 answers

Uncopyable class with automatic default and move constructors

I want to make some classes use automatically generated constructors, but be non-copyable (but still movable). Currently I'm doing it like this: class A { public: A() = default; A(const A&) = delete; A(A&&) = default; A&…
Xirdus
  • 2,997
  • 6
  • 28
  • 36
4
votes
2 answers

C# Instance copy / Passing object reference is different then Java?

class Player { private Location location; public Location getLocation() { return location; } public void setLocation(Location location) { this.location = location; } } ... class Location { int x,y,z; …
SSpoke
  • 5,656
  • 10
  • 72
  • 124
4
votes
4 answers

Are undeclared copy-constructors automatically inline?

Are undeclared (auto-generated) copy constructors automatically marked as inline? If so, and if I don't want them to be marked as inline, does that mean I have to define one manually and copy every single member I need by hand (assuming I'm not…
user541686
  • 205,094
  • 128
  • 528
  • 886
3
votes
4 answers

Advice on Copy Constructor of a Class containing a non-copyable member reference

I have a class A which has an reference to an object of class B as a member. The copy constructor (and assignment operator) of class B is private. Do you think it is a valid and good idea to use the default copy constructor for A. (I actually want…
user796530
3
votes
1 answer

2 defaulted copy constructors: is it possible?

Is the following program ill-formed? struct Foo { Foo(Foo&) = default; Foo(const Foo&) = default; }; int main() { } It successfully compiles with clang++ 3.8.0 and g++ 6.3.0 (compiler flags are -std=c++11 -Wall -Wextra -Werror…
Constructor
  • 7,273
  • 2
  • 24
  • 66
1
2 3 4