assignment-operator

what is return type of assignment operator?

China☆狼群 提交于 2019-11-27 01:01:36
I am just starting C++. I am a bit confused about the return type of assignment and dereference operator. I am following the book C++ Primer. At various occasions, the author says that the return type of assignment operator is reference to the type of left hand operand but later on, he says that the return type is the type of the left hand operand. I have referred C++11 Standard Sec. 5.17, where the return type is described as "lvalue referring to left hand operand". Similarly, I can't figure out whether dereference returns the pointed-to object or the reference to the object. Are these

The assignment operator and initialization

一世执手 提交于 2019-11-26 23:40:14
问题 I'm studying the C++ programming language and I'm reading the chapter about assignment operator ( = ). In C++ initalization and assignment are operation so similar that we can use the same notation. But my question is : when i initialize a variable am I doing it with the assignment operator ? When i assign to a variable, am I doing it with the assignment operator ? I think that the only difference is between initialization and assignment because when we initalize a variable we are giving it a

Template assignment operator overloading mystery

烈酒焚心 提交于 2019-11-26 23:11:38
问题 I have a simple struct Wrapper , distinguished by two templated assignment operator overloads: template<typename T> struct Wrapper { Wrapper() {} template <typename U> Wrapper &operator=(const Wrapper<U> &rhs) { cout << "1" << endl; return *this; } template <typename U> Wrapper &operator=(Wrapper<U> &rhs) { cout << "2" << endl; return *this; } }; I then declare a and b: Wrapper<float> a, b; a = b; assigning b to a will use the non-const templated assignment operator overload from above, and

Why should the assignment operator return a reference to the object?

帅比萌擦擦* 提交于 2019-11-26 22:41:12
I'm doing some revision of my C++, and I'm dealing with operator overloading at the minute, specifically the "="(assignment) operator. I was looking online and came across multiple topics discussing it. In my own notes, I have all my examples taken down as something like class Foo { public: int x; int y; void operator=(const Foo&); }; void Foo::operator=(const Foo &rhs) { x = rhs.x; y = rhs.y; } In all the references I found online, I noticed that the operator returns a reference to the source object. Why is the correct way to return a reference to the object as opposed to the nothing at all?

Should the Copy-and-Swap Idiom become the Copy-and-Move Idiom in C++11?

情到浓时终转凉″ 提交于 2019-11-26 22:22:45
问题 As explained in this answer, the copy-and-swap idiom is implemented as follows: class MyClass { private: BigClass data; UnmovableClass *dataPtr; public: MyClass() : data(), dataPtr(new UnmovableClass) { } MyClass(const MyClass& other) : data(other.data), dataPtr(new UnmovableClass(*other.dataPtr)) { } MyClass(MyClass&& other) : data(std::move(other.data)), dataPtr(other.dataPtr) { other.dataPtr= nullptr; } ~MyClass() { delete dataPtr; } friend void swap(MyClass& first, MyClass& second) {

How to push_back without operator=() for const members?

只谈情不闲聊 提交于 2019-11-26 22:07:43
问题 How to push_back() to a C++ std::vector without using operator=() for which the default definition violates having const members? struct Item { Item(int value) : _value(value) { } const char _value; } vector<Item> items; items.push_back(Item(3)); I'd like to keep the _value const since it should not change after the object is constructed, so the question is how do I initialize my vector with elements without invoking operator=()? Here is the basic error the g++ v3.4.6 is giving me: .../3.4.6

Assigning a pointer to an integer

假如想象 提交于 2019-11-26 22:04:21
问题 Can I assign a pointer to an integer variable? Like the following. int *pointer; int array1[25]; int addressOfArray; pointer = &array1[0]; addressOfArray = pointer; Is it possible to do like this? 回答1: Not without an explicit cast, i.e. addressOfArray = (int) pointer; There's also this caveat: 6.3.2.3 Pointers ... 6 Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type

Should implicitly generated assignment operators be & ref-qualified?

笑着哭i 提交于 2019-11-26 20:58:23
问题 The following code compiles without problem on gcc 4.8.1: #include <utility> struct foo { }; int main() { foo bar; foo() = bar; foo() = std::move( bar ); } It seems the implicitly generated assignment operators for foo are not & ref-qualified and so can be invoked on rvalues. Is this correct according to the standard? If so, what reason is there for not requiring implicitly generated assignment operators to be & ref-qualified? Why doesn't the standard require the following to be generated?

Scalar vs List Assignment Operator

吃可爱长大的小学妹 提交于 2019-11-26 20:57:24
Please help me understand the following snippets: my $count = @array; my @copy = @array; my ($first) = @array; (my $copy = $str) =~ s/\\/\\\\/g; my ($x) = f() or die; my $count = () = f(); print($x = $y); print(@x = @y); The symbol = is compiled into one of two assignment operators: A list assignment operator ( aassign ) is used if the left-hand side (LHS) of a = is some kind of aggregate. A scalar assignment operator ( sassign ) is used otherwise. The following are considered to be aggregates: Any expression in parentheses (e.g. (...) ) An array (e.g. @array ) An array slice (e.g. @array[...]

The copy constructor and assignment operator

*爱你&永不变心* 提交于 2019-11-26 19:51:02
If I override operator= will the copy constructor automatically use the new operator? Similarly, if I define a copy constructor, will operator= automatically 'inherit' the behavior from the copy constructor? sgokhales No, they are different operators. The copy constructor is for creating a new object. It copies a existing object to a newly constructed object.The copy constructor is used to initialize a new instance from an old instance. It is not necessarily called when passing variables by value into functions or as return values out of functions. The assignment operator is to deal with an