copy-constructor

C++: Copy contructor: Use Getters or access member vars directly?

Have a simple container class: public Container { public: Container() {} Container(const Container& cont) //option 1 { SetMyString(cont.GetMyString()); } //OR Container(const Container& cont) //option 2 { m_str1 = cont.m_str1; } public string GetMyString() { return m_str...

Linked list and copy constructor

I'm trying to write a basic, singly-linked list class in C++. I did it in my data structures class years back, but I can't remember the details. Should my Node class have a copy constructor? It has a Node* as a member variable, and as far as I know you're always supposed to write a copy constructor, destructor, and assignment operator f...

Is there any advantage in using a reference argument in this function?

I have defined the following class: class Action { public: Action(){ _bAllDone = false; } void AddMove( Move & m ); private: std::deque<Move> _todo; bool _bAllDone; }; The member AddMove is defined as follows: void Action::AddMove( Move & m ) { _todo.push_back( m ); } I noted that without the...

Is it bad form to call the default assignment operator from the copy constructor?

Consider a class of which copies need to be made. The vast majority of the data elements in the copy must strictly reflect the original, however there are select few elements whose state is not to be preserved and need to be reinitialized. Is it bad form to call a default assignment operator from the copy constructor? The default assi...

Initializing an array in C++

I am trying to initialize an array of objects: SinglyLinkedList offeredClasses[22] = {SinglyLinkedList("CSCE101"),SinglyLinkedList("CSCE101L"),SinglyLinkedList("CSCE150E"),SinglyLinkedList("CSCE150EL"),SinglyLinkedList("CSCE150EM"),SinglyLinkedList("CSCE150EML"),SinglyLinkedList("CSCE155"),SinglyLinkedList("CSCE155H"),SinglyLinkedList("...

Is it correct to use declaration only for empty private constructors in C++?

For example is this correct: class C { private: C(); C(const & C other); } or you should rather provide definition(s): class C { private: C() {}; C(const & C other) {}; } ? Thanks for the current answers. Let's extend this question - does compiler generate better code in one of this examples? I can im...

Copy constructor and = operator overload in C++: is a common function possible?

Since a copy constructor MyClass(const MyClass&); and an = operator overload MyClass& operator = (const MyClass&); have pretty much the same code, the same parameter, and only differ on the return, is it possible to have a common function for them both to use? ...

C++ Class Inheritance problem

Hi I have two classes, one called Instruction, one called LDI which inherits from instruction class. class Instruction{ protected: string name; int value; public: Instruction(string _name, int _value){ //constructor name = _name; value = _value; } ~Instruction(){} Instruction (const Instruction &r...

Copy constructor for a binary tree C++

I have a Tree class with the following definition: class Tree { Tree(); private: TreeNode *rootPtr; } TreeNode represents a node and has data, leftPtr and rightPtr. How do I create a copy of a tree object using a copy constructor? I want to do something like: Tree obj1; //insert nodes Tree obj2(obj1); //without modifying obj1. ...

Why copy constructor is not called in this case?

Hello everybody. Here is the little code snippet: class A { public: A(int value) : value_(value) { cout <<"Regular constructor" <<endl; } A(const A& other) : value_(other.value_) { cout <<"Copy constructor" <<endl; } private: int value_; }; int main() { A a = A(5); } I assumed that output ...

weird C++ constructor/copy constructor issues in g++

#include <iostream> using namespace std; class X { public: X() { cout<<"Cons"<<endl; } X(const X& x){ cout<<"Copy"<<endl; } void operator=(const X& x){ cout<<"Assignment called";...

Copy Constructor Needed with temp object.

The following code only works when the copy constructor is available. When I add print statements (via std::cout) and make the copy constructor available it is not used (I assume there is so compiler trick happening to remove the unnecessary copy). But in both the output operator << and the function plop() below (where I create a t...

What is the copy constructor bug causing parsing errors?

I'm writing a compiler for a small language, and my Parser class is currently in charge of building an AST for use later. However, recursive expressions are not working correctly because the vector in each AST node that holds child nodes are not working correctly. Currently my AST's header file looks like this: class AST { public: e...

Why does copy constructor call other class' default constructor?

I was wondering why an error like this would occur. no matching function for call to 'Foo::Foo()' in code for a copy constructor? Assume Foo is just an object with normal fields ( no dynamically allocated memory, etc. ), and the only constructor it has defined is a constructor that takes one argument. I didn't even know the construct...

Copy constructors and Assignment Operators

I wrote the following program to test when the copy constructor is called and when the assignment operator is called: #include class Test { public: Test() : iItem (0) { std::cout << "This is the default ctor" << std::endl; } Test (const Test< "This is the copy ctor" << std::endl; } ~Test() ...

Can you call a copy constructor from another method?

/** @file ListP.cpp * ADT list - Pointer-based implementation. */ #include <iostream> #include <cstddef> // for NULL #include <new> // for bad_alloc #include "ListP.h" // header file using namespace std; List::List() : size(0), head(NULL) { } // end default constructor List::List(const List& aList) : size(aList.size) { if (aLi...

Copy constructor invokes an infinite loop

Hi, I am passing a value to copy constructor as a reference, but an infinite loop is being invoked. Here's my class: class Vector2f{ private: GLfloat x; GLfloat y; public: Vector2f(); Vector2f(const GLfloat _x, const GLfloat _y); Vector2f(const Vector2f &_vector); ~Vector2f(); }; Here's implementation of me...

Is there a way to clone a class object in C#?

Possible Duplicate: Cloning objects in C# I have this class Object that has about 20 properties (they're all basic or struct types). Is there a simple way to create a clone of an instance of this object. A bit like the copy ctor in C++? This is for a Silverlight application. ...

How to copy by value into a container class?

I am writing a sparse matrix class. I need to have a node class, which will be a template for its contents. My issue in writing this class is: How do I store the contents? I want to store the contents by value. If I stored it by pointer and it should be destroyed, then I'd have trouble. How can I safely perform a copy in the setConte...

Any gotchas in copy ctor and assignment operator having slightly different semantics?

Please look at the following code and tell me if it's going to cause problems in the future, and if yes, how to avoid them. class Note { int id; std::string text; public: // ... some ctors here... Note(const Note& other) : id(other.id), text(other.text) {} void operator=(const Note& other) // returns void: no chaining ...