copy-constructor

Can I write different copyCtor for const and non-const instances?

那年仲夏 提交于 2019-12-08 18:25:36
问题 I have the following problem: I have a class which should do this: Obj o; Obj o1(o), o1=o; // deep-copies const Obj c(o), c=o; // deep-copies const Obj c1(c), c1=c; // shallow-copies Obj o2(c), o2=c; // deep-copies How can I do this preferably without inheritance? (I mean I would do Const_obj inheriting from Obj otherwise.) EDIT: Using o.clone() directly is not an option because then I could easily introduce bugs by accidentally not cloning. EDIT: Finally, there is a proper, complete solution

Does Java have a default copy constructor (like in C++)? [duplicate]

你说的曾经没有我的故事 提交于 2019-12-08 17:01:52
问题 This question already has answers here : Why doesn't Java have a copy constructor? (9 answers) Closed 3 years ago . Does Java has a default copy constructor as C++? If it has one - does it remain usable if I declare another constructor (not a copy constructor) explicitly? 回答1: Java does not have bulit-in copy constructors. But you can write your own such constructors. See an example below: class C{ private String field; private int anotherField; private D d; public C(){} public C(C other){

Strange “Member function not viable” error in templated linear algebra vector class

↘锁芯ラ 提交于 2019-12-08 15:01:17
问题 I'm implementing a templated vector class (not the data container, but the vector in the linear algebra sense), and I'm getting quite a few errors whenever I refer to rhs in my operator overloading. Also, my copy constructor doesn't seem to be working. #ifndef __VecXd__VecXd__ #define __VecXd__VecXd__ #define ULL unsigned long long #include <iostream> using namespace std; template <class T> class VecXd { public: explicit VecXd(ULL newDimension = 1) { dimension = newDimension; vector = new T

C++ bitwise copy of object failing? Why?

删除回忆录丶 提交于 2019-12-08 13:16:45
问题 This question is regarding bitwise copying of class objects. Why is constructor not called, instead destructor is called in below code ? The output is as HowMany h2 = f(h); // No constructor get;s called here.. after construction of h: objectCount = 1 x argument inside f(): objectCount = 1 ~HowMany(): objectCount = 0 after call to f(): objectCount = 0 ~HowMany(): objectCount = -1 ~HowMany(): objectCount = -2 class HowMany { static int objectCount; public: HowMany() { objectCount++; } static

C++ Template constructor, why is copy constructor being called?

无人久伴 提交于 2019-12-08 07:54:31
问题 I have a class with a template constructor, and the code is actually calling the copy constructor, after the default-constructor, which does not make sense to me as the type is incorrect. For example: class A { public: A(void); // default constructor A(const A& other); // copy constructor template<class TYPE> A(const TYPE& object_to_ref); // template constructor }; This template constructor works (is properly called in other cases), but is not properly recognized as the "correct" constructor

Does basic_string class really have copy constructor that takes more than one parameter or is it just constructor?

被刻印的时光 ゝ 提交于 2019-12-08 05:42:09
问题 I was reading Why would a copy constructor have more than one parameter?. The accepted answer says that: The old std::basic_string does have one too: basic_string(const basic_string& s, size_type pos = 0, size_type n = npos) But http://www.cplusplus.com/reference/string/basic_string/basic_string/ says that: basic_string (const basic_string& str, size_type pos, size_type len = npos, const allocator_type& alloc = allocator_type()); The above isn't a copy constructor but substring constructor

Defining copy constructor in a class inherited from POD struct

本小妞迷上赌 提交于 2019-12-08 04:40:38
问题 As you know compiler defines default constructor, copy constructor, assignment operator and destructor for POD structures if it weren't defined manually. Usually (or maybe should I say always) it's a bit copy operation. So I've decided to inherit my class from Win structure BITMAP to provide memory allocation in constructor and freeing in destructor. I haven't used composition 'coz I want to allow using it with some WinAPI functions. Here is a some piece of code: class CPreviewFrame : public

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

大憨熊 提交于 2019-12-08 04:34:33
问题 Let the example be: class Base { Base (const Base & copyFrom) { globalRegister (* this); } } class Derived { Derived (const Derived & copyFrom) : Base (copyFrom) {} } I've read suggestions to include the Base's copy constructor on the initialisation list of Derived in order to copy over the Base's properties (as in the example). However, I have the Base's copy constructor passing itself (* this) to other object (to be registered with that object). Would that be a case where I actually must

Error: templated copy constructor calls itself

两盒软妹~` 提交于 2019-12-08 04:13:27
问题 when this copy constructor is called in my application, this copy constructor goes into an infinite loop i.e it keeps calling itself. When i debugged the pogram terminated at the first line itself. The foreach loop used is a Qt foreach. template<class TValue> Locus<TValue>::Locus (const Locus<TValue>& source ) : QVector<TValue>(source) // program terminates here { std::cout << "calling self from here " << std::endl; foreach (typename Locus<TValue>::value_type l, source) { this->push_back(l);

Copy Constructor Big 3 C++ issue

£可爱£侵袭症+ 提交于 2019-12-08 02:01:27
问题 I am struggling with implementing big 3 idea (assignment operator overload, copy constructor, destructor). my code below will crash the program. It compiles so I cannot even see any error hint. Please help. enter code here #include <iostream> using namespace std; #include <string> #include <fstream> #include <cmath> #include <cstdlib> #include <ctime> #include <vector> #include <windows.h> #include <cstring> #include <cctype> #include <iomanip> #include <algorithm> #include<sstream> class