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.

copy-constructor
Filter by
Sorted by
Tagged with
1 vote
2 answers
1k views

Base class on the initialisation list of a derived class' copy constructor (C++)

Let the example be: class Base { Base (const Base & copyFrom) { globalRegister (* this); } } class Derived { Derived (const Derived & copyFrom) : Base (copyFrom) {} } I've read ...
Jānis Elmeris's user avatar
4 votes
4 answers
5k views

Vector of pointer to objects, need deep copy of vector, but the objects are the base of inherited objects

I want to have a deep copy of an vector with pointers to objects, but the object can either be C or B. I know confusing (the way I explain it), let me illustrate. class A { A(const A& copyme) {...
Stormenet's user avatar
  • 26.3k
7 votes
3 answers
17k views

Proper way to deep copy with copy constructor instead of Object.clone

I have some code that performs a deep copy using Object.clone, but I'm trying to rewrite it using the more "acceptable" copy constructor technique. Below are two simple examples of what I'm trying to ...
vocaro's user avatar
  • 2,779
2531 votes
8 answers
382k views

What is The Rule of Three?

What does copying an object mean? What are the copy constructor and the copy assignment operator? When do I need to declare them myself? How can I prevent my objects from being copied?
fredoverflow's user avatar
35 votes
2 answers
7k views

How are C++ array members handled in copy control functions?

This is something I have wondered for a long time. Take the following example: struct matrix { float data[16]; }; I know what the default constructor and destructor do in this specific example (...
fredoverflow's user avatar
4 votes
5 answers
2k views

implementing a copy constructor

I have the following class definition and it needs a copy constructor so deep copies are made to copy the raw pointers. Can anybody advice on how to best do this? Using xerces-c++ for XML class ...
Tony The Lion's user avatar
26 votes
4 answers
39k views

Correct way to duplicate Delphi object

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 ...
gabr's user avatar
  • 26.7k
8 votes
7 answers
2k views

In an STL Map of structs, why does the "[ ]" operator cause the struct's dtor to be invoked 2 extra times?

I've created a simple test case exhibiting a strange behavior I've noticed in a larger code base I'm working on. This test case is below. I'm relying on the STL Map's "[ ]" operator to create a ...
Monte Hurd's user avatar
  • 4,419
1 vote
1 answer
718 views

Problem with const qualifiers to get private atributes of an object

I'm a completely new to C++ and I'm having a very stupid problem. I have a Graph class and I need to create a copy constructor for it. This is my class: #include <igraph.h> #include <...
Rafael S. Calsaverini's user avatar
0 votes
2 answers
933 views

Bad memory allocation C++ for a vector

I get std_bad_alloc error in the following code. It seems the problems is when I add the matrix to the vector, the program crashes when I get to that line in the debugger. The problem is that only the ...
user69514's user avatar
  • 27.4k
0 votes
1 answer
2k views

C++ unrestricted union workaround

#include <stdio.h> struct B { int x,y; }; struct A : public B { // This whines about "copy assignment operator not allowed in union" //A& operator =(const A& a) { printf("A=A ...
Chris's user avatar
  • 6,682
2 votes
2 answers
594 views

C++ virtual inheritace and typecasting/copy constructor confusion

I have the code below: class A { }; class B: public virtual A { public: B() { cerr << "B()"; } B(const A& a) { cerr << "B(const A&)"; } }; ...
Panayiotis Karabassis's user avatar
0 votes
4 answers
2k views

C++ - call assignment operator at creation instead of copy constructor

I want to enforce explicit conversion between structs kind of like native types: int i1; i1 = some_float; // this generates a warning i1 = int(some_float): // this is OK int i3 = some_float; // this ...
Chris's user avatar
  • 6,682
2 votes
3 answers
695 views

Default constructor

struct Base{ Base(Base &){} // suppress default constructor }; struct Derived : Base{ }; int main(){ Derived d; } The code shown gives error because the default constructor (...
Chubsdad's user avatar
  • 25.1k
4 votes
4 answers
652 views

Implementing Exception class in C++

So, I am try to write a simple base Exception class for C++, based on the Java Exception class. I'm sure there are great libraries out there already, but I am doing this for practice, not production ...
Roadrunner-EX's user avatar
1 vote
2 answers
195 views

Data model, cyclic references

I have the following data structure for storing meridians and parallels. Each cartographic point stores: A] geographic and spatial coordinates, cartographic distortions, etc. B] pointer to north/...
Robo's user avatar
  • 21
20 votes
4 answers
8k views

Why is the copy constructor not called?

class MyClass { public: ~MyClass() {} MyClass():x(0), y(0){} //default constructor MyClass(int X, int Y):x(X), y(Y){} //user-defined constructor MyClass(const MyClass& tempObj):x(tempObj.x)...
cpx's user avatar
  • 17.3k
28 votes
8 answers
13k views

Implementing the copy constructor in terms of operator=

If the operator= is properly defined, is it OK to use the following as copy constructor? MyClass::MyClass(MyClass const &_copy) { *this = _copy; }
gregseth's user avatar
  • 13.2k
13 votes
4 answers
2k views

Why would a copy constructor have more than one parameter?

$12.8/2 - 'A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are ...
Chubsdad's user avatar
  • 25.1k
3 votes
3 answers
12k views

conversion from 'std::string' to non-scalar type requested

I have trouble implementing my class. It should be able to initialize from std::string. So I wrote a copy (?) constructor: CVariable (std::string&, const int p_flags = 0); I'm trying to make an ...
Xirdus's user avatar
  • 3,017
1 vote
1 answer
616 views

cloning object on linq grouping - copy constructor substitute

I have collection of custom objects(assets) which I want to group with LINQ. Custom object has standard properties like id, name and cost property. When grouping I want to calculate cost for each ...
Marko's user avatar
  • 1,924
2 votes
2 answers
410 views

C++: Question about copy constructors

I have a class where I use this to initialize a void* pointer. But the problem is, when I pass an instance of that class by value, the pointer will not change to the new address on the stack. So, I ...
Martijn Courteaux's user avatar
4 votes
4 answers
445 views

What's wrong with this copy constructor?

I've been trying to come up with a copy constructor for a tree. I've found quite a few suggestions. This one interested me. class TreeNode { int ascii; TreeNode* left; TreeNode* right;...
Peter Stewart's user avatar
2 votes
4 answers
2k views

Copy constructor problem

I tried to use copy constructor using statement: X y = X(); But copy constructor is not being called. I am using g++ 4.1.0. I set both X(const X&) and X(x&) constructor in the class. Is ...
Atul 's user avatar
  • 1,126
17 votes
5 answers
9k views

What's all the fuss about C++ copy constructors? [duplicate]

Possible Duplicate: When do we have to use copy constructors? Why exactly are C++ copy constructors so important? I just learned about them and I don't quite see what is the fuss about them. It ...
bodacydo's user avatar
  • 77.4k
2387 votes
5 answers
501k views

What is the copy-and-swap idiom?

What is the copy-and-swap idiom and when should it be used? What problems does it solve? Does it change for C++11? Related: What are your favorite C++ Coding Style idioms: Copy-swap Copy constructor ...
GManNickG's user avatar
  • 499k
95 votes
7 answers
82k views

When do we have to write a user-defined copy constructor?

I know that C++ compiler creates a copy constructor for a class. In which case do we have to write a user-defined copy constructor? Can you give some examples?
penguru's user avatar
  • 4,360
1 vote
4 answers
415 views

Does such an optimization exist in g++?

Consider the following code snippet: list<someClass>& method(); .... list<someClass> test = method(); What will the behavior of this be? Will this code: Return a reference to the ...
alexgolec's user avatar
  • 27.6k
4 votes
5 answers
3k views

Preventing copy construction and assignment of a return value reference

If I have a function that returns a reference to an instance of a class that I don't have control over its source, say list<int>: list<int>& f(); I want to ensure that its value is ...
Paul J. Lucas's user avatar
88 votes
8 answers
58k views

Why must the copy assignment operator return a reference/const reference? [duplicate]

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 class A, ...
bks's user avatar
  • 1,876
0 votes
3 answers
533 views

C++ auto_ptr and copy construction

If I have a class template <typename T> struct C { ... private: auto_ptr<T> ptr; }; How do I define the copy constructor for C: It cannot be template <typename T> C<T>...
user231536's user avatar
  • 2,681
5 votes
2 answers
757 views

Can reflection be used to instantiate an objects base class properties?

Like this: public class remoteStatusCounts : RemoteStatus { public int statusCount; public remoteStatusCounts(RemoteStatus r) { Type t = r.GetType(); foreach (...
scaryman's user avatar
  • 1,880
3 votes
2 answers
566 views

Can I pass a pointer to a superclass, but create a copy of the child?

I have a function that takes a pointer to a superclass and performs operations on it. However, at some point, the function must make a deep copy of the inputted object. Is there any way I can perform ...
alexgolec's user avatar
  • 27.6k
9 votes
2 answers
4k views

Is there a good way to copy a Gtk widget?

Is there a way, using the Gtk library in C, to clone a Gtk button (for instance), and pack it somewhere else in the app. I know you can't pack the same widget twice. And that this code obviously ...
Jake's user avatar
  • 225
2 votes
6 answers
335 views

Copy constructor demo (crashing... case 2)

Please have a glance at this program: class CopyCon { public: char *name; CopyCon() { name = new char[20]; name = "Hai";//_tcscpy(name,"Hai"); } CopyCon(const CopyCon &...
Akaanthan Ccoder's user avatar
2 votes
3 answers
9k views

destructor and copy-constructor calling..(why does it get called at these times)

I have the following code #include <iostream> using namespace std; class Object { public: Object(int id){ cout << "Construct(" << id << ")" << endl; m_id ...
silent's user avatar
  • 2,876
29 votes
1 answer
3k views

Are variadic constructors supposed to hide the implicitly generated ones?

Are variadic constructors supposed to hide the implicitly generated ones, i.e. the default constructor and the copy constructor? struct Foo { template<typename... Args> Foo(Args&&......
fredoverflow's user avatar
0 votes
2 answers
1k views

In C++, what happens when you return a variable?

What happens, step by step, when a variable is returned. I know that if it's a built-in and fits, it's thrown into rax/eax/ax. What happens when it doesn't fit, and/or isn't built-in? More importantly,...
Clark Gaebel's user avatar
  • 17.6k
5 votes
2 answers
3k views

c++ STL vector is not acccepting the copy constructor

I wrote a code ( c++,visual studio 2010) which is having a vector, even I though copy const is declared, but is still showing that copy const is not declared Here the code #include<iostream> #...
prabhakaran's user avatar
  • 5,208
2 votes
2 answers
1k views

CArray doesn't call copy constructors on memory reallocations, now what?

Suppose I have a class that requires copy constructor to be called to make a correct copy of: struct CWeird { CWeird() { number = 47; target = &number; } CWeird(const CWeird &other) :...
MMx's user avatar
  • 121
8 votes
2 answers
14k views

QObject cloning

I know that Qobjects are supposed to be identities not values eg you cannot copy them and by default the copy constructor and assignment are disabled as explained in qt documentation. But is it ...
Olorin's user avatar
  • 405
6 votes
3 answers
6k views

Constructor or Assignment Operator

Can you help me is there definition in C++ standard that describes which one will be called constructor or assignment operator in this case: #include <iostream> using namespace std; class ...
Julian Popov's user avatar
  • 17.4k
4 votes
3 answers
707 views

Copy Constructor in C++

I have this code #include <iostream> using namespace std; class Test{ public: int a; Test(int i=0):a(i){} ~Test(){ cout << a << endl; } ...
shreyasva's user avatar
  • 13.3k
15 votes
2 answers
3k views

copy constructor with default arguments

As far as I know, the copy constructor must be of the form T(const T&) or T(T&). What if I wanted to add default arguments to the signature? T(const T&, double f = 1.0); Would that be ...
fredoverflow's user avatar
3 votes
4 answers
186 views

Would this constructor be acceptable practice?

Let's assume I have a c++ class that have properly implemented a copy constructor and an overloaded = operator. By properly implemented I mean they are working and perform a deep copy: Class1::...
Robb's user avatar
  • 2,686
4 votes
5 answers
833 views

Copy constructor, why in return by value functions

Suppose i have: class A { A(A& foo){ ..... } A& operator=(const A& p) { } } ... A lol; ... A wow(...) { return lol; } ... ... A stick; stick = wow(...); Then I'll get a ...
fsdfa's user avatar
  • 353
0 votes
7 answers
870 views

Copy constructor demo (crashing...)

Here is the program... class CopyCon { public: char *name; CopyCon() { name = new char; } CopyCon(const CopyCon &objCopyCon) { name = new char; _tcscpy(name,objCopyCon.name); } ~...
Akaanthan Ccoder's user avatar
146 votes
10 answers
90k views

Why should the copy constructor accept its parameter by reference in C++?

Why must a copy constructor's parameter be passed by reference?
Jony's user avatar
  • 6,734
18 votes
3 answers
13k views

Why doesn't C# support the concept of Copy Constructor?

I was asked this question in one of my interviews, but I wasn't able to find out exactly why this concept is not there. Please let me know.
Amit's user avatar
  • 1,047
2 votes
1 answer
2k views

What is the difference among NSString alloc:initWithCString versus stringWithUTF8String?

I thought these two methods were (memory allocation-wise) equivalent, however, I was seeing "out of scope" and "NSCFString" in the debugger if I used what I thought was the convenient method (...
mobibob's user avatar
  • 8,745