copy-constructor

Construct object with itself as reference?

China☆狼群 提交于 2019-12-02 19:44:13
I just realised that this program compiles and runs (gcc version 4.4.5 / Ubuntu): #include <iostream> using namespace std; class Test { public: // copyconstructor Test(const Test& other); }; Test::Test(const Test& other) { if (this == &other) cout << "copying myself" << endl; else cout << "copying something else" << endl; } int main(int argv, char** argc) { Test a(a); // compiles, runs and prints "copying myself" Test *b = new Test(*b); // compiles, runs and prints "copying something else" } I wonder why on earth this even compiles. I assume that (just as in Java) arguments are evaluated

assignment operator vs. copy constructor C++

浪子不回头ぞ 提交于 2019-12-02 18:14:54
I have the following code to test out my understanding of basic pointers in C++: // Integer.cpp #include "Integer.h" Integer::Integer() { value = new int; *value = 0; } Integer::Integer( int intVal ) { value = new int; *value = intVal; } Integer::~Integer() { delete value; } Integer::Integer(const Integer &rhInt) { value = new int; *value = *rhInt.value; } int Integer::getInteger() const { return *value; } void Integer::setInteger( int newInteger ) { *value = newInteger; } Integer& Integer::operator=( const Integer& rhInt ) { *value = *rhInt.value; return *this; } // IntegerTest.cpp #include

how to copy SubClass object in BaseClass copy constructor

烂漫一生 提交于 2019-12-02 17:38:36
问题 I would like to make copy of SubClass object in BaseClass constructor. I need that the following code execute correctly. class BaseClass{ BaseClass() {} BaseClass(BaseClass base) { //TODO: how to implement? } } class SubClass extends BaseClass { SubClass() {} } public class Test { public static void main(String[] args) { BaseClass sub = new SubClass(); BaseClass subCopy = new BaseClass(sub); if (subCopy instanceof SubClass) { // need to be true } } } Is it even possible? If yes how can I do

Copy constructor initialization lists

拟墨画扇 提交于 2019-12-02 15:58:26
I know that if you leave a member out of an initialization list in a no-arg constructor, the default constructor of that member will be called. Do copy constructors likewise call the copy constructor of the members, or do they also call the default constructor? class myClass { private: someClass a; someOtherClass b; public: myClass() : a(DEFAULT_A) {} //implied is b() myClass(const myClass& mc) : a(mc.a) {} //implied is b(mc.b)??? or is it b()? } Explicitly-defined copy constructors do not call copy constructors for the members. When you enter the body of a constructor, every member of that

Problem with ostringstream and copy constructor [duplicate]

佐手、 提交于 2019-12-02 13:15:02
问题 This question already has answers here : Closed 8 years ago . 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

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

我怕爱的太早我们不能终老 提交于 2019-12-02 12:57:45
问题 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

how to copy SubClass object in BaseClass copy constructor

巧了我就是萌 提交于 2019-12-02 12:53:37
I would like to make copy of SubClass object in BaseClass constructor. I need that the following code execute correctly. class BaseClass{ BaseClass() {} BaseClass(BaseClass base) { //TODO: how to implement? } } class SubClass extends BaseClass { SubClass() {} } public class Test { public static void main(String[] args) { BaseClass sub = new SubClass(); BaseClass subCopy = new BaseClass(sub); if (subCopy instanceof SubClass) { // need to be true } } } Is it even possible? If yes how can I do it? Else how can I get similar effect? qqilihq It's not possible. A constructor of class A gives you an

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

Deadly 提交于 2019-12-02 12:07:49
问题 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

Does copy of an object through copy constructor get the same instance variable as the original object?

风流意气都作罢 提交于 2019-12-02 08:21:29
Does a copy of an object with object instance variables get the same instance variable as the original object? if so, I was wondering if the original and copy objects are referencing to the same instance variables. Unlike C++, Java does not provide copy constructors automatically. There is therefore no general answer to any question about the behavior of copy constructors, as Java places no restrictions on their behavior. Nevertheless, every object, however initialized, has its own instance variables. These are not shared with any other object; they can be "the same" as another object's only

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

懵懂的女人 提交于 2019-12-02 06:09:29
问题 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&) =