This question already has answers here:
What are the basic rules and idioms for operator overloading?
(8 answers)
Closed 7 years ago.
How can I perform this code with below details in C++:
mystring a;
a="A test text";
"mystring" is a class that is defined (by myself) for strings , and other operators like + , == , >> , << , etc are defined in this class.
How can i define a function (a friend function with class) that "=" perform something that I have mentioned.
if there were dictation mistakes, forgive me.
You need to define
mystring& operator=(const char*)
for this specific assignment to work.
Note that this overload returns a reference to self. This allows for compound assignments.
Not with a friend function. You need to overload the assignment operator :
mystring &operator = (mystring const &other) {
// ...
return *this;
}
Note that you'll also need a conversion constructor that takes in a C-string :
mystring(char const *str) {
// ...
}
Related
This question already has answers here:
C++ why the assignment operator should return a const ref in order to avoid (a=b)=c
(5 answers)
Why not return const-reference in operator= instead of a reference?
(1 answer)
why does builtin assignment return a non-const reference instead of a const reference in C++?
(3 answers)
Return type of assignment operator in C++
(2 answers)
When should the assignment operator return const T &, T & or T?
(1 answer)
Closed last month.
I have seen code that defines a copy assignment operator to return a const reference, e.g.
const MyClass & operator= (const MyClass &);
I'm confused about the intention, and how such operator can be used.
For example, let's say I use it to do something like a = b (a and b are objects of MyClass). Wouldn't that require a to have been declared as const, and if so, wouldn't that mean that I cannot modify a? (i.e. what's the point?)
This question already has answers here:
What are the basic rules and idioms for operator overloading?
(8 answers)
Closed 2 years ago.
The prototype of operator function to overload << operator is:
friend ostream& operator<<(ostream&, const className&);
I am a newbee so it would be appreciated if it could get explained with simple examples. Thanks
The act of writing to an object of type ostream can change its state. For instance, could set the fail bit or any other fail state. For that reason, the function parameter is a reference to non-const.
Typically, when you write
foo f;
std::cout << f;
you ignore the returned value. Remember that calling operators is similar to calling methods and the same line can be written as:
operator<<(std::cout,f);
For the argument type, consider that writing something to a stream does modify internal state of the stream. Hence, operator<< takes a non-const reference. You cannot pass a const object/reference, because a constant stream would not allow anything to be inserted.
Now chaining:
foo f,g;
std::cout << f << g;
same as
operator<<( operator<<( std::cout,f) , g);
------------------------
|
v
returns non-const ref
If operator<< would return a constant reference you could not chain it.
This question already has answers here:
std::string::assign vs std::string::operator=
(2 answers)
Closed 2 years ago.
For example, this code
std::string a("this is a string");
std::string b;
b = a;
std::string c;
c.assign(a);
Is there any difference between B and C in essence?
From cppreference
2) basic_string& assign( const basic_string& str );
...
2) Replaces the contents with a copy of str. Equivalent to *this = str;. In particular, allocator propagation may take place. (since C++11)
So this does the same.
This question already has answers here:
Converting an int to std::string
(11 answers)
Closed 5 years ago.
Is it possible to overload conversion operator from int to std::string? Is it possible to use this function:
void printToLog(std::string text);
Like this:
int a = 5;
printToLog(a);
Or this:
int a = 5;
printToLog(a + " bombs have been planted.");
?
There's a function for it in the standard library. The aptly named std::to_string:
int a = 5;
printToLog(std::to_string(a) + " bombs have been planted.");
You cannot add a conversion operator for this purpose. Such an operator can only be a member of a class type, and int is not a class type. Neither can you add a converting constructor to std::string. Use the mechanism the standard gives you.
This question already has answers here:
When to use addressof(x) instead of &x?
(5 answers)
Closed 7 years ago.
If addressof operator& works well then why C++ has introduced addressof() function? The & operator is part of C++ from the beginning - why this new function is introduced then? Does it offer any advantages over C's & operator?
The unary operator& might be overloaded for class types to give you something other than the object's address, while std::addressof() will always give you its actual address.
Contrived example:
#include <memory>
#include <iostream>
struct A {
A* operator &() {return nullptr;}
};
int main () {
A a;
std::cout << &a << '\n'; // Prints 0
std::cout << std::addressof(a); // Prints a's actual address
}
If you wonder when doing this is useful:
What legitimate reasons exist to overload the unary operator&?