copy-constructor

Why doesn't Java have a copy constructor?

时光总嘲笑我的痴心妄想 提交于 2019-12-17 06:26:48
问题 Why doesn't Java support a copy constructor like in C++? 回答1: Java does. They're just not called implicitly like they are in C++ and I suspect that's your real question. Firstly, a copy constructor is nothing more than: public class Blah { private int foo; public Blah() { } // public no-args constructor public Blah(Blah b) { foo = b.foo; } // copy constructor } Now C++ will implicitly call the copy constructor with a statement like this: Blah b2 = b1; Cloning/copying in that instance simply

What's the most reliable way to prohibit a copy constructor in C++?

半腔热情 提交于 2019-12-17 06:10:01
问题 Sometimes it's necessary to prohibit a copy constructor in a C++ class so that class becomes "non-copyable". Of course, operator= should be prohibited at the same time. So far I've seen two ways to do that. Way 1 is to declare the method private and give it no implementation: class Class { //useful stuff, then private: Class( const Class& ); //not implemented anywhere void operator=( const Class& ); //not implemented anywhere }; Way 2 is to declare the method private and give it "empty"

What is a converting constructor in C++ ? What is it for?

本小妞迷上赌 提交于 2019-12-16 22:23:53
问题 I have heard that C++ has something called "conversion constructors" or "converting constructors". What are these, and what are they for? I saw it mentioned with regards to this code: class MyClass { public: int a, b; MyClass( int i ) {} } int main() { MyClass M = 1 ; } 回答1: The definition for a converting constructor is different between C++03 and C++11. In both cases it must be a non- explicit constructor (otherwise it wouldn't be involved in implicit conversions), but for C++03 it must

QObject cloning

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-14 01:01:03
问题 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 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.cloneFrom(a); or QObject a = new QOBject(); QObject b = new QOBject(); b->cloneFrom(a); and the clone method copies stuff like members etc would this be wrong? And

Use of the Implicit Copy Constructor in User-Defined Copy Constructor

跟風遠走 提交于 2019-12-14 00:50:19
问题 I have a rather large and lengthy class where the implicitly generated copy-constructor would almost do exactly the right thing, except for one specific field. Is there a way to write a user-defined copy-constructor that calls the implicit version, and then adds one or two lines at the end? Or do I have to write a lengthy, (and boring, and typo-prone) user-defined copy-constructor that mostly duplicates the implicit one? class MySimpleObject { private: FieldA m_fieldA; FieldB m_fieldB; [...

Do we need an accessible copy constructor for value initialization in C++98/03?

时间秒杀一切 提交于 2019-12-13 16:16:43
问题 This question refers only to pre C++11 . Consider the following seemingly broken code: struct X { X(){} // default user-provided constructor private: X(const X&){} }; int main() { X x = X(); } Live on Coliru According to cppreference.com in pre C++11 the default ctor will be called: The effects of value initialization are: 1) if T is a class type with at least one user-provided constructor of any kind, the default constructor is called; ... This seem to imply that the copy ctor doesn't

How to make a deep copy of this constructor?

為{幸葍}努か 提交于 2019-12-13 14:49:50
问题 I made this constructor, and I need to make a deep copy of it. I don't really understand the meaning of a deep copy. I know it makes independent copy of an object that has its own dynamic memory, but I don't understand what the need is for this. I'm also not sure how to actually implement the deep copy. Any suggestions? Here is my constructor that I need to make deep copy of: Could anyone provide some syntax help, like a skeleton? template<class t_type> inline ALIST<t_type>::ALIST() { t_type

Add a deep copy ctor to std::unique_ptr<my_type>

偶尔善良 提交于 2019-12-13 13:11:20
问题 I would like to store some std::unique_ptr<my_type> into a std::vector . Since my_type provides a clone() method it's quite straightforward to make deep copies of my_type * . The point is how to extend std::unique_ptr preserving all its functionalities while adding the copy ctor and the assignment operator. Inheritance? Templace specialization? Could you please provide a code snippet? 回答1: The purpose of std::unique_ptr is for it to be unique i.e. it shouldn't be copyable. That's why they

Derived and base class, can I set the base explicitly?

孤街浪徒 提交于 2019-12-13 12:24:50
问题 public class SuperCar: Car { public bool SuperWheels { get {return true; } } } public class Car { public bool HasSteeringWheel { get {return true;} } } How can I set the base class for the derived Supercar? For example, I want to simply set SuperCars base class like this: public void SetCar( Car car ) { SuperCar scar = new SuperCar(); car.Base = car; } Basically, if I have Car objects, I do not want to manually iterate through every property of the car in order to setup the SuperCar oject,

Copy Constructor Issue C++: “0xC0000005: Access violation writing location 0x00000000.”

末鹿安然 提交于 2019-12-13 09:41:05
问题 I'm having some trouble getting my copy constructor to work in my BigInt class I'm working on. In BigIntVector.cpp in the copy constructor, the lines: for (int i = 0; i < vectorSize; i++) { vectorArray[i] = orig.vectorArray[i]; } causes the exception 0xC0000005: Access violation writing location 0x00000000. Any help figuring out why would be appreciated. Thank you. In BigInt.cpp : // copy constructor BigInt::BigInt(BigInt const& orig) : isPositive(orig.isPositive) , base(orig.base) , skip