copy-constructor

Why is class with non-const copy constructor not treated as copy constructible?

非 Y 不嫁゛ 提交于 2019-12-05 13:39:39
Given struct Foo { Foo(Foo&) {} }; std::is_copy_constructible<Foo>::value is false Foo has valid copy-constructor: From draft n4659: 15.8.1 Copy/move constructors [class.copy.ctor] 1 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 (11.3.6). [Example: X::X(const X&) and X::X(X&,int=1) are copy constructors. but is_copy_constructible tests is_constructible_v<T, const T&> ( const ) according to the standard.

warning: base class ‘A’ should be explicitly initialized in the copy constructor

南楼画角 提交于 2019-12-05 13:22:04
I've got the following class-structure: class A{ A(){} A(const A& src){} }; class B : virtual A { B():A(){} B(const B& src):A(src){} }; class C : virtual A { C():A(){} C(const C& src):A(src){} }; class D : virtual B, virtual C { D():B(),C(){} D(const D& src):B(src),C(src){} }; This gives me the warning: In copy constructor ‘D’: warning: base class ‘A’ should be explicitly initialized in the copy constructor Which I dont unterstand. The Copy -Constructor of D calls the copy-ctor of B which calls the copy-ctor of A. Why does it want me to call the copy-ctor of A in D? If I would do so, wouldnt

how can I find where a C++ copy constructor is being USED via a compile error?

北城以北 提交于 2019-12-05 10:47:12
In short: is there some way I can modify a class definition such that it fails to compile at the point of use of a copy constructor no matter where it's used? I have a very large project and was cleaning up some class definitions. There's a class that I explicitly don't want to use copy constructors on (let's ignore why that is for the sake of this discussion), and in the interest of safety, I figured I'd just define the copy constructor as private and not actually implement it... that way it would throw a compile error if I tried to use it anywhere. Lo and behold, it compiles fine, but I have

Can reflection be used to instantiate an objects base class properties?

烂漫一生 提交于 2019-12-05 10:09:39
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 is a bit simple (it's from the Jira API - RemoteStatus has 4 properties), but imagine the base class had 30 properties. I don't want to manually set all those values, especially if my inherited class only has a couple of extra properties. Reflection seems to hint at an answer. I saw at Using inheritance in constructor (publix X

c++ copy constructor signature : does it matter

送分小仙女□ 提交于 2019-12-05 08:04:20
My current implementation uses lots of copy constructors with this syntax MyClass::Myclass(Myclass* my_class) Is it really (functionnaly) different from MyClass::MyClass(const MyClass& my_class) and why? I was adviced that first solution was not a true copy constructor. However, making the change implies quite a lot of refactoring. Thanks!!! It's different in the sense that the first isn't a copy constructor, but a conversion constructor. It converts from a MyClass* to a MyClass . By definition, a copy-constructor has one of the following signatures: MyClass(MyClass& my_class) MyClass(const

How to call copy constructor of all base classes for copying most derived class object in diamond inheritance in C++?

我的梦境 提交于 2019-12-05 07:49:00
Consider the below code: #include<iostream> using namespace std; class A { public: A() {cout << "1";} A(const A &obj) {cout << "2";} }; class B: virtual A { public: B() {cout << "3";} B(const B & obj) {cout<< "4";} }; class C: virtual A { public: C() {cout << "5";} C(const C & obj) {cout << "6";} }; class D:B,C { public: D() {cout << "7";} D(const D & obj) {cout << "8";} }; int main() { D d1; cout << "\n"; D d(d1); } The output of the program is below: 1357 1358 So, for line D d(d1) the copy constructor of D class is bein called. During inheritance we need to explicitly call copy constructor

c++ copy initialization & direct initialization, the weird case

感情迁移 提交于 2019-12-05 06:12:20
Before continue reading this, please read Is there a difference in C++ between copy initialization and direct initialization? first, make sure you understand what it is talking about. I'll summarize the rule here first (read standard n3225 8.5/16, 13.3.1.3, 13.3.1.4, and 13.3.1.5), 1) For direct initialization, all constructors will be considered as the overloading set, the overloading resolution will select the best one according to the overloading resolution rules. 2) For copy initialization and the source type is the same as destination type or derived from destination type, the rule is as

extraneous calls to copy-constructor and destructor

蹲街弑〆低调 提交于 2019-12-05 05:28:41
问题 [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 more } } The output is: A Construction // 1 A Copy Construction // 1 A Destruction // 1 A Construction // 2 A Copy Construction // 2 A Copy Construction // WHY THIS? A Destruction // 2 A Destruction // deleting element from t A Destruction //

initialize std::array without copying/moving elements

守給你的承諾、 提交于 2019-12-05 05:28:30
#include <iostream> #include <string> #include <array> class C { private: std::string a; std::string b; std::string c; public: C(std::string a_, std::string b_, std::string c_) : a{a_},b{b_},c{c_} {} ~C(){}; C(const C&) =delete; C(const C&&) =delete; const C& operator=(const C&) =delete; const C& operator=(const C&&) =delete; }; std::array<C,2> array = {C("","",""),C("","","")}; int main() {} this won't compile (Android Studio with NDK and clang) with a "call to deleted constructor of c" error. I know that I can e.g. use a std::vector and emplace_back() to construct an element directly inside

Can a derived class be made uncopyable by declaring copy constructor/operator private in base class?

橙三吉。 提交于 2019-12-05 04:48:04
I thought in theory the answer to this question was yes. However, in practice, my compiler (VS2010) does not seem to complain in the following situation: I have an abstract base class providing some common interface (yet having no data members) and various sub and subsubclasses derived from it. class Base { public: Base() {} virtual ~Base() {} virtual void interfaceFunction1() = 0; virtual void interfaceFunction2() = 0; private: Base(const Base&); // all derived classes should be uncopyable Base& operator=(const Base&); // no data members }; My compiler found it unproblematic to even implement