copy-constructor

extraneous calls to copy-constructor and destructor

[a follow up to this question] class A { public: A() {cout<<"A Construction" <<endl;} A(A const& a){cout<<"A Copy Construction"<<endl;} ~A() {cout<<"A Destruction" <<endl;} }; int main() { { vector<A> t; t.push_back(A()); t.push_back(A()); // once mo...

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 (commented out below) and when I switched to the more explicit method my code stopped crashing! Notice that I am getting the string that is being s...

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

Why copy constructor must be passed its parameter by reference? ...

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); } ~CopyCon() { if( name != NULL ) { delete name; name = NULL; } } }; int main() { CopyCon objCopyCon1; objCop...

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 compile error in the last line. But if I add 'const' before 'A&', its ok. I want to know why. Where it's exactly the problem? I dont get why it shoul...

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::Class1(const Class1 &class1) { // Perform copy } Class1& Class1::operator=(const Class1 *class1) { // perform copy return *this; } Now l...

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 standards compliant? ...

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; } Test(const Test &){ cout << "copy" << endl; } void operator=(const Test &){ cout << "=" << endl; } Test operator+(Test& p){ Test res(a+p.a); return res; } }; int main (int argc, ch...

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 CTest { public: CTest() : m_nTest(0) { cout << "Default constructor" << endl; } CTest(int a) : m_nTest(a) { cout << "Int constructor" << ...

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 asignment are disabled as explained in qt documentation. But is it possible to create a new Qobject from an existing one using a clone method? Would this be a logic error ? If i say QObject b; QObject a; b.clo...

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) : number(other.number), target(&number) { } const CWeird& operator=(const CWeird &w) { number = w.number; return *this; } void output() ...

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> #include<vector> using namespace std; class A { public: A() { cout << "Default A is acting" << endl ; } A(A &a) { cout << "Copy Construc...

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, is there a guaranteed copy constructor call? edit: What about the destructor? Is that called "sometimes", "always", or "never"? ...

variadic constructors

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&&... x) { std::cout << "inside the variadic constructor\n"; } }; int main() { Foo a; Foo b(a); } Somehow I was expecting this to ...

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

Hello there, I have the following code #include <iostream> using namespace std; class Object { public: Object(int id){ cout << "Construct(" << id << ")" << endl; m_id = id; } Object(const Object& obj){ cout << "Copy-construct(" << obj.m_id << ")" << endl; m_id = obj.m_id; } Object& oper...

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 &objCopyCon) { name = new char[_tcslen(objCopyCon.name)+1]; _tcscpy(name,objCopyCon.name); } ~CopyCon() { if( name != NULL ) { ...

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 wouldn't work, but shows what happens when I attempt a shallow copy of the button: GtkButton *a = g_object_new(GTK_TYPE_BUTTON, "label", "o_0",...

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 such a copy? It occurred to me to make the function a template function and simply have the user pass the type, but I hold out hope that ...

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 (PropertyInfo p in t.GetProperties()) { this.property(p) = p.GetValue(); //example pseudocode } } } The example ...

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>::C(const C& other) because I want to if I copy the auto_ptr from other, I have changed other by removing ownership. Is it legal to define a copy constructor ...