copy-constructor

Why is the copy ctor used in this code?

大兔子大兔子 提交于 2019-12-06 21:55:15
问题 class A { public: A(const int n_); A(const A& that_); A& operator=(const A& that_); }; A::A(const int n_) { cout << "A::A(int), n_=" << n_ << endl; } A::A(const A& that_) // This is line 21 { cout << "A::A(const A&)" << endl; } A& A::operator=(const A& that_) { cout << "A::operator=(const A&)" << endl; } int foo(const A& a_) { return 20; } int main() { A a(foo(A(10))); // This is line 38 return 0; } Executing this code gives o/p: A::A(int), n_=10 A::A(int), n_=20 Apparently the copy

SFINAE away a copy constructor

蹲街弑〆低调 提交于 2019-12-06 19:22:57
问题 Under certain conditions, I'd like to SFINAE away the copy constructor and copy assignment operator of a class template. But if I do so, a default copy constructor and a default assignment operator are generated. The SFINAE is done based on tags I pass as class template parameters. The problem is, that SFINAE only works on templates and a copy constructor/assignment operator can't be a template. Does there exist a workaround? 回答1: This solution uses a base class that is conditionally not

Explicit copy constructor and uniform initialization

…衆ロ難τιáo~ 提交于 2019-12-06 16:43:21
问题 Explicit copy constructors disallow something like Foo foo = bar; , and enforce the copy usage as Foo foo(bar); . In addition, explicit copy constructors also disallow returning objects by value from a function. However, I tried replacing the copy initialization with braces, like so struct Foo { Foo() = default; explicit Foo(const Foo&) = default; }; int main() { Foo bar; Foo foo{bar}; // error here } and I am getting the error (g++5.2) error: no matching function for call to 'Foo::Foo(Foo&)'

Disallow the use of auto for a given class, C++14 vs. C++17 update

谁说我不能喝 提交于 2019-12-06 15:31:25
问题 What is the feature that allows me use auto for non-copyable (and non-movable) types in C++17 and didn't for C++14? Consider the following code: struct A{ A(A const&)=delete; A(A&&)=delete; }; int main(){ auto a1 = A{}; // ok in C++17, not ok in C++14 auto&& a2 = A{}; // ok in C++17, ok in C++14 } It turns out that this was invalid code in C++14 but it is valid in C++17. The behavior is consistent in clang and gcc: https://godbolt.org/z/af8mEc The reason I ask is because until recently I was

Copying S4 base class instance into a derived object

五迷三道 提交于 2019-12-06 14:13:46
问题 I have two simple classes: .A1 <- setClass("A1", representation=representation( x1 ="numeric"), prototype = prototype(x1 = 10)) .A2 <- setClass("A2", contains="A1", representation=representation(x2="numeric"), prototype = prototype(x2 = 10)) setMethod("initialize", "A2", function(.Object, ..., x2 = .Object@x2) { callNextMethod(.Object, ..., x2 = x2) }) Using this code everything works: a1 <- .A1(x1 = 3) initialize(a1) a2 <- .A2(x1 = 2, x2 = 4) initialize(a2, x2 = 3) .A2(a1, x2 = 2) An object

Implementation supplied copy constructor and assignment operator

∥☆過路亽.° 提交于 2019-12-06 08:42:55
I have a small confusion regarding the situations where the implementation (compiler) will not supply the copy constructor and the copy assignment operator. When we declare the copy ctor and/or copy assignment operator in our class. Some says when we derive from a class which has a private copy ctor and/or copy assignment operator. I am a little confused about the second situation, is the second situation is precisely. a) The implementation will not declare them for you, so you will get a compile time error. OR b) The implementation will declare and define them, but when the compiler defined

When does gcc compile unused template code?

爷,独闯天下 提交于 2019-12-06 07:34:59
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 ), while some changes cause it to fail with gcc 6 (e.g. if this-> is not used). What's going on here?

Abstract class, copy constructor

梦想与她 提交于 2019-12-06 04:22:40
问题 Does it make sense to define a copy constructor / operator = in class having pure virtual method or only in derived classes? 回答1: like normal classes: yes, if you have a specific implementation need. 回答2: If it has only pure virtual methods (and no data members) then, no, the synthesised one is fine (and won't do anything). If it does have data members then you should define your own copy constructor if/when it makes sense to do so, just like for any other class. Derived classes don't really

base class 'QAbstractListModel' has private copy constructor

。_饼干妹妹 提交于 2019-12-06 03:32:17
问题 I have a QT QML project. (still very small) I started by binding a listview on my UScenario model, by subclassing QAbstractListModel and it worked fined. Now, each UScenario has a list of UTask , which also have a list of UCondition (so, Utask also subclasses QAbstractListModel ). But then, QT Creator gives me an error: Core/Tasks/utask.h:6: erreur : base class 'QAbstractListModel' has private copy constructor class UTask: public QAbstractListModel ^ So I'm not sure where is my problem. I

understanding virtual copy constructors

折月煮酒 提交于 2019-12-05 18:20:49
I'm having trouble understanding what's really happening with the code in a book I'm using to learn C++. Here's the code: class Base { public: Base() {}; virtual ~Base() {}; virtual Base* Clone() {return new Base(*this);} }; class Derived { public: Derived() {}; virtual ~Derived() {}; virtual Base* Clone() {return new Derived(*this);} }; So in this Clone() function I understand that the function returns a pointer to a Base class object. What I don't understand is what's happening within that function. When I've previously used new as in int *pInt = new int , I was under the impression that new