copy-constructor

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

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, and the following: A a1(param); A a2 = a1; A a3; a3 = a2; //<--- this is the problematic line The operator= is defined as follows: A A:...

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 only assigned to another reference, e.g.: list<int> &a_list = f(); If the user were instead to do: list<int> a_list = f(); // note: no '&', so the list...

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 someClass instance returned by return value optimization from method(), and then perform someClass's copy constructor on the reference? Avoid calling th...

When do we have to use copy constructors?

As I know that C++ compiler creates a copy constructor for each class. In which cases we have to write user defined copy constructors? Can you give some examples? ...

What is the copy-and-swap idiom?

What is this idiom and when should it be used? Which problems does it solve? Will the idiom change when C++0x is used? Although it's been mentioned in many places, we didn't have any singular "what is it" question and answer, so here it is. Here is a partial list of places where it was previously mentioned: What are your favorite C++ ...

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

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 seems you should always write a copy constructor for your classes if you use pointers, but why? Thanks, Boda Cydo. ...

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 this supposed to work or I am doing some very basic problem in the code? My code for the class is class A { public: int i; A(int ii) { ...

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; public: TreeNode() { ascii = 0; left = right = 0; } TreeNode* clone(); // ... }; TreeNode* TreeNode::clone() { ...

C++: Question about copy constructors

Hi, 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 thought to override the copy constructor to reassign the pointer with the new address of this. But I need variables to call the super-cl...

cloning object on linq grouping - copy constructor substitute

Hi all, 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 group, so I'm using little trick like this: from a in assets group a by a.AssetId into ga select new Asset() { ...

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 object of CVariable: MCXJS::CVariable s_var = (string)"good job"; I'm getting the following error: F:\Projekty\MCXJS\src\main.cpp|8|error:...

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 no other parameters or else all other parameters have default arguments (8.3.6).106)' So far, I have not come across any example of a situation w...

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

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/south/east/west node. It allows to store relationships between points, first of all their affiliation to the meridian/parallel... class...

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 code, and I'm curious and always looking to learn. One of the things the Java's Exception does, which I would like to also implement, is the...

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 (implicit) of 'Base' is suppressed. Indeed the standard says in $12.1 "If there is no user-declared constructor for class X, a default constructor ...

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 generates a warning I thought to use an assignment operator and copy constructor to do the same thing, but the behavior is different: St...

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&)"; } }; class C: public B { }; int main(int argc, char **argv) { B *b = new B(C()); } To my surprise B(const A& a) isn't called. Why is that? ...

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 should do the exact same thing as A=B\n"); } A& operator =(const B& b) { printf("A = B\n"); } }; union U { A a; B b; }; int main(int argc, c...

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 first two matrices are read from the file, the other two aren't because the program crashes with the above error. ...