copy-constructor

When does gcc compile unused template code?

北战南征 提交于 2019-12-07 19:25:15
问题 I have the following (admittedly contrived) code that compiles just fine in gcc 6, but doesn't compile in gcc 7. Notice the use of an undeclared constructor in the definition of bar . This should print an error if the function is ever referenced elsewhere in the code (uncommenting foo.bar() causes gcc 6 to print an error). However, gcc 7 prints an error even if the function is not used. Some changes cause the code to also compile with gcc 7 (e.g. if B is replaced with T in the definition of A

C++ bitwise vs memberwise copying? [closed]

假如想象 提交于 2019-12-07 13:27:08
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . What is the difference between bitwise and memberwise copying? Surely if you copy the members then you will end up copying the bits

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

微笑、不失礼 提交于 2019-12-07 09:49:06
问题 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

Error: templated copy constructor calls itself

拜拜、爱过 提交于 2019-12-07 08:41:18
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); } } From the Qt docs : Qt automatically takes a copy of the container when it enters a foreach loop. 来源

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

痴心易碎 提交于 2019-12-07 06:12:07
问题 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

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

狂风中的少年 提交于 2019-12-07 03:54:08
问题 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

c++ copy constructor signature : does it matter

混江龙づ霸主 提交于 2019-12-07 03:35:53
问题 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!!! 回答1: 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

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

梦想与她 提交于 2019-12-07 02:09:06
问题 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

copy constructor parameters

喜你入骨 提交于 2019-12-07 01:40:50
问题 In a copy constructor why do arguments need to have default values associated with them? What happens if there are no default values associated with them and more than one argument is provided in the constructor? For example: X(const X& copy_from_me, int = 10); has a default value for the int, but this: X(const X& copy_from_me, int); does not. What happens in this second case? http://en.wikipedia.org/wiki/Copy_constructor 回答1: A copy constructor always takes one parameter, reference to the

initialize std::array without copying/moving elements

…衆ロ難τιáo~ 提交于 2019-12-07 00:33:22
问题 #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"