assignment-operator

Overloading assignment operator in C++

As I've understand, when overloading operator=, the return value should should be a non-const reference. A } It is non-const to allow non-const member functions to be called in cases like: ( a = b ).f(); But why should it return a reference? In what instance will it give a problem if the return value is not declared a reference, ...

C++ copy-construct construct-and-assign question

Here is an extract from item 56 of the book "C++ Gotchas": It's not uncommon to see a simple initialization of a Y object written any of three different ways, as if they were equivalent. Y a( 1066 ); Y b = Y(1066); Y c = 1066; In point of fact, all three of these initializations will probably result in the same obje...

Shortcut "or-assignment" (|=) operator in Java

I have a long set of comparisons to do in Java, and I'd like to know if one or more of them come out as true. The string of comparisons was long and difficult to read, so I broke it up for readability, and automatically went to use a shortcut operator |= rather than negativeValue = negativeValue || boolean. boolean negativeValue = false...

How to write a cctor and op= for a factory class with ptr to abstract member field?

I'm extracting files from zip and rar archives into raw buffers. I created the following to wrap minizip and unrarlib: Archive.hpp - Used to access everything. If I could make all the functions in the other classes inaccessible from the outside, I would. (Actually, I suppose I could friend all the other classes in Archive and use privat...

C++ type-checking at compile-time

Hi, all. I'm pretty new to C++, and I'm writing a small library (mostly for my own projects) in C++. In the process of designing a type hierarchy, I've run into the problem of defining the assignment operator. I've taken the basic approach that was eventually reached in this article, which is that for every class MyClass in a hierarchy ...

Assigning a vector of one type to a vector of another type

Hi, I have an "Event" class. Due to the way dates are handled, we need to wrap this class in a "UIEvent" class, which holds the Event, and the date of the Event in another format. What is the best way of allowing conversion from Event to UIEvent and back? I thought overloading the assignment or copy constructor of UIEvent to accept Eve...

Has anyone found the need to declare the return parameter of a copy assignment operator const?

The copy assignment operator has the usual signature: my_class & operator = (my_class const & rhs); Does the following signature have any practical use? my_class const & operator = (my_class const & rhs); You can only define one or the other, but not both. ...

behaviour of the implicit copy constructor / assignment operator

Hello, I have a question regarding the C++ Standard. Suppose you have a base class with user defined copy constructor and assignment operator. The derived class uses the implicit one generated by the compiler. Does copying / assignment of the derived class call the user defined copy constructor / assignment operator? Or do you need to...

Why would the assignment operator ever do something different than its matching constructor?

I was reading some boost code, and came across this: inline sparse_vector &assign_temporary(sparse_vector &v) { swap(v); return *this; } template<class AE> inline sparse_vector &operator=(const sparse_vector<AE> &ae) { self_type temporary(ae); return assign_temporary(temporary); } It seems to be mapping...

Constructor or Assignment Operator

Can you help me is there definition in C++ standard that describes which one will be called constructor or assignment operator in this case: #include <iostream> using namespace std; class CTest { public: CTest() : m_nTest(0) { cout << "Default constructor" << endl; } CTest(int a) : m_nTest(a) { cout << "Int constructor" << ...

Must parameter of assignment operator be reference?

When overloading assignment operator of a class in C++, must its parameter be reference? For example, class MyClass { public: ... MyClass & operator=(const MyClass &rhs); ... } Can it be class MyClass { public: ... MyClass & operator=(const MyClass rhs); ... } ? Thanks! ...

In Objective C, is there a difference between if (object == nil) and if (nil == object)?

I would lean towards if (object == nil) but I've noticed in some tutorials the use of if (nil == object) Is this just a style thing, or is there some justified rationale for using either format? ...

assignment operator overloading problem

This issue has me confused. The first piece of code works fine without crashing, it assigns s1 to s2 perfectly fine. But the second group of code causes the program to crash. Anyone have any idea on why this is happening or what the problem could be? Code 1:(works) s1.add(10, 30, 25, "Test Screen", false); s1.add(13, 10, 5, "Nam...

Why must the copy assignment operator return a reference/const reference?

In C++, the concept of returning reference from the copy assignment operator is unclear to me. Why can't the copy assignment operator return a copy of the new object? In addition, if I have class A, and the following: A a1(param); A a2 = a1; A a3; a3 = a2; //<--- this is the problematic line The operator= is defined as follows: A A:...

Preventing copy construction and assignment of a return value reference

If I have a function that returns a reference to an instance of a class that I don't have control over its source, say list<int>: list<int>& f(); I want to ensure that its value is only assigned to another reference, e.g.: list<int> &a_list = f(); If the user were instead to do: list<int> a_list = f(); // note: no '&', so the list...

Passing temporaries as LValues

Hello I'd like to use the following idiom, that I think is non-standard. I have functions which return vectors taking advantage of Return Value Optimization: vector<T> some_func() { ... return vector<T>( /* something */ ); } Then, I would like to use vector<T>& some_reference; std::swap(some_reference, some_func()); but so...

Can I define an assignment operator in vb.net Structure?

I am having a structure that is actually a simple byte with more functionality. I defined it this way: Structure DeltaTime Private m_DeltaTime As Byte Public ReadOnly DeltaTime As Byte Get Return m_DeltaTime End Get End Property End Structure I want to have these two functionalities: Public ...

RAII and assignment

I created the following class for an sqlite3 connection: class SqliteConnection { public: sqlite3* native; SqliteConnection (std::string path){ sqlite3_open_v2 (path.c_str(), &native, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); } ~SqliteConnection (){ sqlite3_close(native); } } and then one...

What is the copy-and-swap idiom?

What is this idiom and when should it be used? Which problems does it solve? Will the idiom change when C++0x is used? Although it's been mentioned in many places, we didn't have any singular "what is it" question and answer, so here it is. Here is a partial list of places where it was previously mentioned: What are your favorite C++ ...

Special member functions in C++0x

The Wikipedia article about special member functions doesn't contain any reference to move constructors and move assignment operators. I would like to update the entry but I'm not sure what the 0x standard says. What are the rules regarding these two functions? Are they automatically generated by the compiler and if so when? Edit: I...