copy-constructor

Checklist for writing copy constuctor and assignment operator in C++

Please write a list of tasks that a copy constructor and assignment operator need to do in C++ to keep exception safety, avoid memory leaks etc. ...

Matlab copy constructor

Hi, Is there a better way to implement copy construcor for matlab for a handle derived class other than adding a constructor with one input and explicitly copying its properties? obj.property1 = from.property1; obj.property2 = from.property2; etc. Thanks, Dani ...

C++: dynamically allocating an array of objects?

This is kind of a beginners question, but I haven't done C++ in a long time, so here goes... I have a class that contains a dynamically allocated array, say class A { int* myArray; A() { myArray = 0; } A(int size) { myArray = new int[size]; } ~A() { // Note that as per MikeB's...

c++ push_back, non const copy constructor

I have a class that i want to push_back into a deque. The problem is when i push back i need the original object to be changed thus i need a non const copy ctor. Now if i implement that my const copy ctor gets called. If i removed the const ctor i get an compile error about no available ctors. How do i implement this in a way that i can ...

Copy Constructor in C++ is called when object is returned from a function?

I understand copy constructor is called on three instances When instantiating one object and initializing it with values from another object (as in the example above). When passing an object by value. 3. When an object is returned from a function by value. I have question with no.3 if copy constructor is called when an object value...

Copy constructor vs. return value optimization

In a previous question, it appeared that a plain return-by-value function always copies its return argument into the variable being assigned from it. Is this required by the standard, or can the function be optimized by constructing the 'assigned to' variable even within the function body? struct C { int i; double d; }; C f( int i, in...

Auto Pointer constructor in VC2008

I have an auto pointer implementation: template <typename T, bool Arr = false> class GAutoPtr { T *Ptr; public: typedef GAutoPtr<T, Arr> &AutoPtrRef; GAutoPtr(T *ptr = 0) { Ptr = ptr; } GAutoPtr(AutoPtrRef p) { Ptr = p.Release(); } ~GAutoPtr() { Empty(); } operator T*() { retur...

Copy constructor initialization lists

I know that if you leave a member out of an initialization list in a no-arg constructor, the default constructor of that member will be called. Do copy constructors likewise call the copy constructor of the members, or do they also call the default constructor? class myClass { private: someClass a; someOtherClass b; public:...

How to define a Copy constructor with pointers in it?

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 error when performing the copy constructor. I am trying to copy just the value from the pointer of the copied object, but avoiding assigning t...

Why doesn't Java have a copy constructor?

Why doesn't Java support a copy constructor like in C++? ...

C++: Why is the copy ctor used in this code?

class A { public: A(const int n_); A(const A& that_); A& operator=(const A& that_); }; A::A(const int n_) { cout << "A::A(int), n_=" << n_ << endl; } A::A(const A& that_) // This is line 21 { cout << "A::A(const A&)" << endl; } A& A::operator=(const A& that_) { cout << "A::operator=(const A&)" << endl; } int foo(const A& a_...

C++: Could Polymorphic Copy Constructors work?

Consider: class A { public: A( int val ) : m_ValA( val ) {} A( const A& rhs ) {} int m_ValA; }; class B : public A { public: B( int val4A, int val4B ) : A( val4A ), m_ValB( val4B ) {} B( const B& rhs ) : A( rhs ), m_ValB( rhs.m_ValB ) {} int m_ValB; }; int main() { A* b1 = new B( 1, 2 ); A* b2 = new A( ...

Can I create an object from a derived class by constructing the base object with a parameter?

In other words, given a base class shape and a derived class rectangle: class shape { public: enum shapeType {LINE, RECTANGLE}; shape(shapeType type); shape(const shape &shp); } class rectangle : public shape { public: rectangle(); rectangle(const rectangle &rec); } I'd like to know if I could create an instance of rectangl...

Problem with AutoPtr in C++/CLI mixed mode

I have a C++/CLI wrapper around native .lib and .h files. I use the AutoPtr class pretty extensively in the wrapper class to manage the unmanaged objects I create for wrapping. I have hit a roadblock with the copy constructor/assignment operator. Using the AutoPtr class from Mr. Kerr: http://weblogs.asp.net/kennykerr/archive/2007/03/2...

Why won't C++ allow non-const to const conversion in copy ctor?

I have two getter members: Node* prev() { return prev_; } int value() { return value_ } Please note the lack of const identifiers (I forgot them, but now I want to know why this won't work). I am trying to get this to compile: Node(Node const& other) : prev_(other.prev()), value_(other.value()) { } The compiler rejects this. I tho...

Templated copy-constructor fails with specific templated type

As some of my code required implicit conversion between matrices of different types (e.g. Matrix<int> to Matrix<double>), I defined a templated copy constructor Matrix<T>::Matrix(Matrix<U> const&) instead of the standard Matrix<T>::Matrix(Matrix<T> const&): template <typename T> class Matrix { public: // ... template <typename U...

stl vector.push_back() abstract class doesn't compile

Hi, Let's say I have an stl vector containing class type "xx". xx is abstract. I have run into the issue where the compiler won't let me "instantiate" when i do something like the following: std::vector<xx> victor; void pusher(xx& thing) { victor.push_back(thing); } void main() { ; } I assume this is because the copy constru...

Parameter choice for copy constructor

I was recently asked in an interview about the parameter for a copy constructor. [Edited] As a designer of C++ language implementing copy constructor feature, why would you choose constant reference parameter over a const pointer to a const object. I had a few ideas like since a pointer can be assigned to NULL which probably doesn't m...

Is this good code? (copy ctor + operator=)

For one reason or another, I'm forced to provide both a copy constructor and an operator= for my class. I thought I didn't need operator= if I defined a copy ctor, but QList wants one. Putting that aside, I hate code duplication, so is there anything wrong with doing it this way? Fixture::Fixture(const Fixture& f) { *this = f; } Fi...

Reducing code duplication between operator= and the copy constructor

I have a class that requires a non-default copy constructor and assignment operator (it contains lists of pointers). Is there any general way to reduce the code duplication between the copy constructor and the assignment operator? ...