assignment-operator

difference between copy constructor and assignment operator

Deadly 提交于 2021-02-11 14:21:47
问题 I have gone through [question] (What's the difference between assignment operator and copy constructor?) and understood difference between a copy constructor and assignment operator. Now my question is although copy constructor initializes the previously uninitialized object where as assignment operator replaces data from previously initialized object what's the difference in terms of final outcome. I am thinking that final outcome in both case comes out to be same right? In the end after

c++ : move assignement operator and inheritance

夙愿已清 提交于 2021-02-11 14:17:12
问题 This code compiles and runs fine: #include <iostream> class Base { public: Base(int value) : clean_(true) { value_ = new int; *value_ = value; } ~Base() { if(clean_) delete value_; } Base(Base&& other) noexcept : value_{std::move(other.value_)}, clean_(true) { other.clean_=false; } Base& operator=(Base&& other) noexcept { value_ = std::move(other.value_); other.clean_=false; clean_=true; } void print() { std::cout << value_ << " : " << *value_ << std::endl; } int* value_; bool clean_; };

ref-qualifiers for the assignment operator of standard library types

本秂侑毒 提交于 2021-02-09 10:55:43
问题 I was wondering, is there a reason the assignment operator of standard types is not lvalue ref-qualified? None of them are. Because of that, we can write things such as this: std::string{} = "42"; std::string s = "hello " + std::string{"world"} = "oops!"; std::vector<int> v = { 1,2,3 }; std::move(v) = { 4,5,6 }; If the assignment operator was lvalue ref-qualified all of these examples would not compile. Is it because there's a lot of things to modify (but then so it was for noexcept) and

ref-qualifiers for the assignment operator of standard library types

牧云@^-^@ 提交于 2021-02-09 10:54:09
问题 I was wondering, is there a reason the assignment operator of standard types is not lvalue ref-qualified? None of them are. Because of that, we can write things such as this: std::string{} = "42"; std::string s = "hello " + std::string{"world"} = "oops!"; std::vector<int> v = { 1,2,3 }; std::move(v) = { 4,5,6 }; If the assignment operator was lvalue ref-qualified all of these examples would not compile. Is it because there's a lot of things to modify (but then so it was for noexcept) and

In x = 1, are both x and 1 objects?

半城伤御伤魂 提交于 2021-02-09 01:12:00
问题 In x = 1 , are both x and 1 objects? Because print(1) and x = 1; print(x) will result in the same output. Even the syntax of print function is: print( *objects , sep=' ', end='\n', file=sys.stdout, flush=False) 回答1: Names in Python are not objects. Using a name in an expression automatically evaluates to the object referred to by the name. It is not possible to interact with the name itself in any way, such as passing it around or calling a method on it. >>> x = 1 >>> type(1) # pass number to

In x = 1, are both x and 1 objects?

爱⌒轻易说出口 提交于 2021-02-09 01:01:12
问题 In x = 1 , are both x and 1 objects? Because print(1) and x = 1; print(x) will result in the same output. Even the syntax of print function is: print( *objects , sep=' ', end='\n', file=sys.stdout, flush=False) 回答1: Names in Python are not objects. Using a name in an expression automatically evaluates to the object referred to by the name. It is not possible to interact with the name itself in any way, such as passing it around or calling a method on it. >>> x = 1 >>> type(1) # pass number to

checking for self-assignment in fortran overloaded assignment

倾然丶 夕夏残阳落幕 提交于 2021-02-07 21:11:20
问题 I am trying to implement a polynomial class with fortran 2003, with overloaded arithmetic operations and assignments. The derived type maintains allocatable list of term definitions and coefficients, like this type polynomial private type(monomial),dimension(:),allocatable :: term double precision,dimension(:),allocatable :: coef integer :: nterms=0 contains ... end type polynomial interface assignment(=) module procedure :: polynomial_assignment end interface ... contains elemental

checking for self-assignment in fortran overloaded assignment

喜夏-厌秋 提交于 2021-02-07 21:04:10
问题 I am trying to implement a polynomial class with fortran 2003, with overloaded arithmetic operations and assignments. The derived type maintains allocatable list of term definitions and coefficients, like this type polynomial private type(monomial),dimension(:),allocatable :: term double precision,dimension(:),allocatable :: coef integer :: nterms=0 contains ... end type polynomial interface assignment(=) module procedure :: polynomial_assignment end interface ... contains elemental

What constructor or operator is used in a return (C++)

北城余情 提交于 2021-02-07 19:18:36
问题 I run this code for experimenting copy constructor and assignment operator class AClass { private: int a; public: AClass (int a_) : a(a_) { cout << " constructor AClass(int) " << a << endl; } AClass(const AClass & x) : a(x.a) { cout << " copy constructor AClass(const AClass &) " << a << endl; } AClass & operator=(const AClass & x) { a = x.a; cout << " AClass& operator=(const AClass &) " << a - endl; return *this; } }; AClass g () { AClass x(8); return x; } int main () { cout << " before

What constructor or operator is used in a return (C++)

烈酒焚心 提交于 2021-02-07 19:17:06
问题 I run this code for experimenting copy constructor and assignment operator class AClass { private: int a; public: AClass (int a_) : a(a_) { cout << " constructor AClass(int) " << a << endl; } AClass(const AClass & x) : a(x.a) { cout << " copy constructor AClass(const AClass &) " << a << endl; } AClass & operator=(const AClass & x) { a = x.a; cout << " AClass& operator=(const AClass &) " << a - endl; return *this; } }; AClass g () { AClass x(8); return x; } int main () { cout << " before