copy-constructor

MATLAB parfor and C++ class mex wrappers (copy constructor required?)

徘徊边缘 提交于 2019-12-04 11:10:58
问题 I'm trying to wrap a C++ class in a matlab mex wrapper using the approach outlined here. Basically, I have an initialization mex file which returns a C++ object handle: handle = myclass_init() I can then pass this to another mex file (e.g. myclass_amethod ) which use the handle to call class methods, then ultimately to myclass_delete to free the C++ object: retval = myclass_amethod(handle, parameter) myclass_delete(handle) I've wrapped this up in a MATLAB class for ease of use: classdef

Abstract class, copy constructor

浪子不回头ぞ 提交于 2019-12-04 10:18:28
Does it make sense to define a copy constructor / operator = in class having pure virtual method or only in derived classes? like normal classes: yes, if you have a specific implementation need. 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 have much to do with it. If it is an object you plan on copying, yes, it's a good idea. See my comment below for

implementing a copy constructor

霸气de小男生 提交于 2019-12-04 09:22:14
I have the following class definition and it needs a copy constructor so deep copies are made to copy the raw pointers. Can anybody advice on how to best do this? Using xerces-c++ for XML class XMLDocument { private: typedef std::vector<XML::XMLNode> v_nodes; public: XMLDocument::XMLDocument(); XMLDocument::XMLDocument(const XMLDocument& copy); XMLDocument::~XMLDocument(); XMLDocument& operator=(XMLDocument const& rhs); void CreateDocument(const std::string& docname); void AddChildNode(XMLNode& node); void RemoveNode(XMLNode& node); void AddNodeValue(XMLNode& node, const std::string& value);

Why would a copy constructor have more than one parameter?

和自甴很熟 提交于 2019-12-04 09:12:33
问题 $12.8/2 - '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 (8.3.6).106)' So far, I have not come across any example of a situation where there is a need to declare a copy constructor with additional default parameters. Would like to know any real time use of such a copy constructor which take more than one

Can I call a copy constructor explicitly?

廉价感情. 提交于 2019-12-04 09:03:32
问题 I'm a little confused as to the mechanics of the copy constructor. Correct me if I'm wrong: If a method takes a reference to an object as a parameter, and the class defines a copy construtor, then the class uses the constructor to create a copy of itself and that gets passed to the function instead of a reference to the original object? Furthermore, one can call Object * obj = new Object(&anotherObject); to create a copy of anotherObject? 回答1: No, if a function take a reference: void f1(

base class 'QAbstractListModel' has private copy constructor

老子叫甜甜 提交于 2019-12-04 08:38:57
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 tried reading the doc about QAbstractListModel vs QAbstractItemModel , but I have no clue. I also tried to

How to write Template class copy constructor

喜你入骨 提交于 2019-12-04 06:41:19
How to write copy constructor for a template class. So that if the template parameter is another user defined class it's copy constructor is also get called. Following is my class template <typename _TyV> class Vertex { public: Vertex(_TyV in) : m_Label(in){ } ~Vertex() { } bool operator < ( const Vertex & right) const { return m_Label < right.m_Label; } bool operator == ( const Vertex & right ) const { return m_Label == right.m_Label; } friend std::ostream& operator << (std::ostream& os, const Vertex& vertex) { return os << vertex.m_Label; } _TyV getLabel() { return m_Label;} private: _TyV m

Why does resize() cause a copy, rather than a move, of a vector's content when capacity is exceeded? [duplicate]

故事扮演 提交于 2019-12-04 03:43:09
This question already has an answer here: How to enforce move semantics when a vector grows? 3 answers Given class X below (special member functions other than the one explicitly defined are not relevant for this experiment): struct X { X() { } X(int) { } X(X const&) { std::cout << "X(X const&)" << std::endl; } X(X&&) { std::cout << "X(X&&)" << std::endl; } }; The following program creates a vector of objects of type X and resizes it so that its capacity is exceeded and reallocation is forced: #include <iostream> #include <vector> int main() { std::vector<X> v(5); v.resize(v.capacity() + 1); }

Find the list of member variables of a class and their types?

Deadly 提交于 2019-12-04 03:11:59
问题 I haven't ever heard it's possible, but asking with the hope that it might. For a class with many more member variables than this: class A { public: SomeOtherClass* s; int i; int j; A() {} A(const A& soc): s(soc.s->Clone()), i(soc.i), j(soc.j) {} }; I always have to remember that if I add another variable int k to the class, I'll also have to add it in the initialization list k(soc.k) and sometimes in the destructor too. I've had to add/remove member variables so many times and it's really

Deleting copy constructor breaks inherited constructors

寵の児 提交于 2019-12-04 00:26:44
I am trying to use the constructor inheritance feature of C++11. The following snippet (copied from somewhere, I don't remember whence) works completely fine: #include <iostream> struct Base { Base() : Base(0) {} Base(int a) : Base(a, 0) {} Base(int a, double b) { std::cout << "Base(" << a << "," << b << ")" << std::endl; } }; struct Derived : Base { using Base::Base; Derived(const Derived& that) = delete; // This line is the culprit }; int main(int argc, char* argv[]) { Derived d1; Derived d2(42); Derived d3(42, 3.14); } That is, until the line marked by the comment is added; because then all