copy-constructor

The copy constructor creates dependent copy

最后都变了- 提交于 2019-12-02 05:47:52
I implemented the copy constructor as it is described here . But still the problem is that when I update route_copy , then the same update is applied to route . So, I don't understand what is wrong in my code? public class Route implements Comparable<Route> { private List<Site> sites; public Route() { sites = new ArrayList<Site>(); } public Route(List<Site> sites) { this.sites = sites; } /** * Copy constructor */ public Route(Route r) { this(r.sites); } public void deleteSite(Site s) { this.sites.remove(s); } } public processData(Route route) { Route route_copy = new Route(route); Site s =

Find root cause of “cannot access private member declared in class 'QObject'”

空扰寡人 提交于 2019-12-02 04:30:29
I understand why I get a C2248: 'QObject::QObject' : cannot access private member declared in class 'QObject' . Qt objects are not copyable, as explained here: https://stackoverflow.com/a/3513395/356726 No copy constructor or assignment operator The problem is, that the compiler message always indicates the last line (closing } ) of the class: class MyQObject : public QObject { Q_OBJECT .... }; <-- error line Root cause is somewhere else, ie. where the class is copied (other file, some different line in code). This is sometimes hard to spot! Question : Is there a way to locate the line of the

Why can't I initialize an array of objects if they have private copy constructors?

别来无恙 提交于 2019-12-02 04:23:20
I just ran across some unexpected and frustrating behaviour while working on a C++ project. My actual code is a tad more complicated, but the following example captures it just as well: class Irritating { public: Irritating() {} private: Irritating(const Irritating& other) {} }; const Irritating singleton; // Works just fine. const Irritating array[] = {Irritating()}; // Compilation error. int main() { return 0; } Trying to compile this produces the following error (GCC version thrown in just in case): [holt@Michaela irritating]$ g++ --version g++ (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2)

Problem with ostringstream and copy constructor [duplicate]

你。 提交于 2019-12-02 04:20:32
Possible Duplicates: Why copying stringstream is not allowed? how copy from one stringstream object to another in C++? Compiling class T fails with Visual C++ and GCC producing iostreams template errors. Here is the code: #include <sstream> class T { static T copy; std::ostringstream log; T() {} T(const T& t) {log = t.log;} ~T() {copy = *this;} }; T T::copy; Changing log data member type to string makes it compile and run OK. Is this a legitimate behavior? Copy constructor and copy-assignment of any stream class in C++ has been made private . That means, you cannot make copy of std:

Copy an object and make both share a member variable (C++)

╄→гoц情女王★ 提交于 2019-12-02 03:50:21
I have been thinking and searching this but I can't solve this question. I would like an object that when copied into another object, both objects share certain member variable. So, when I change the value of the member variable of object1, it's also changes the variable in object2. Example: class ABC { public: int a = 5; //... } int main() { ABC object1; ABC object2 = object1; object2.a = 7; // now, object1.a is equal to 7 object1.a = 10; // now, object2.a is equal to 10 } I know about copy constructors, but I am not sure if it applies here or there is a better method. I have been thinking

Why vector hold a class type will call the copy constructor one more time when push_back()?

北战南征 提交于 2019-12-02 01:26:45
I have the following code: #include <iostream> using std::cin; using std::cout; using std::endl; #include <vector> using std::vector; class Quote { public: Quote() = default; Quote(const std::string &book, double sales_price): bookNo(book), price(sales_price) { } // Quote(const Quote&) = default; // memberwise copy Quote(const Quote &orig): bookNo(orig.bookNo), price(orig.price) { cout << orig.isbn() << endl; cout << "called Quote(const Quote &)" << endl; } Quote& operator=(const Quote&) = default; // copy assign std::string isbn() const { return bookNo; } virtual double net_price(std::size_t

TypeScript - pass to constructor entire object

不羁的心 提交于 2019-12-01 23:20:20
问题 I have a class at type script: export class Child { name:string; age:number; } I want to force class instances to have only properties that class declaration has. For example, if I get from firebase an object: myFirebaseService.getChild(id).then(function(child){ var currentChild = new Child(child); }) So when the object is: {name:"ben", color:"db"}, I want the result to be: currentChild = {"name":"ben"} Becouse "color" is not field of "child". I tried this: export class Child { name:string;

Calling copy constructor on yourself

帅比萌擦擦* 提交于 2019-12-01 22:20:37
I am curious about what is happening in this code I wrote almost by mistake: #include <iostream> class Test { public: Test() { std::cout << "Default constructor"; a= 10; } int a; }; int main() { Test obj(obj); std::cout << obj.a << std::endl; } It compiles in gcc without warnings of any kind (using -Wall -Werror). Executing it only prints garbage. If I am not mistaken, this is calling the implicit copy-constructor on itself, without ever initialising. I am curious about what the copy-constructor would do in such a situation, but gdb will not stop in that line (breakpoints set to that line jump

Why is copy constructor not called

强颜欢笑 提交于 2019-12-01 20:44:30
问题 Here is a simple class header file and a main program. In the main program, I thought that the copy constructor is called in exactly three situations: initialization(explicit copy), pass by value for function arguments, and return by value for functions. However it seems like it is not being called for one of them, I think either (3) or (4) as numbered in the comments. For which numbers (1) - (4) does it get called? Thanks. X.h: #include <iostream> class X { public: X() {std::cout << "default

Does a templated constructor override the implicit copy constructor in C++?

情到浓时终转凉″ 提交于 2019-12-01 17:38:55
Does a templated constructor (such as the following) override the implicit copy constructor? template <class T> struct Foo { T data; // ... template <class U> Foo(const Foo<U> &other) : data((T)doSomethingWith(other.data)) {} // ... }; If so, does it still override it if other is passed by value rather than constant reference? If so, is there any way around this without explicitly defining a copy constructor? No, that is not a copy constructor. Section 12.8 ( [class.copy] ) of the Standard requires that: A non-template constructor for class X is a copy constructor if its first parameter is of