问题
I am trying to overload operator <<
, but it always need to be a const
function. However, I want to change values inside this overloaded function. How do I do this?
EDIT1: The code stub is something like below:
class Check
{
public:
void operator << (boost::any)
{
// checks weather the given is hresult,string(filename) or int(line no)
// and dump them into the exception object,
// There by hresult will initiate the object and int will throw the object.
// so the input order must be like below
}
private:
Exception exception;
};
Usage
Check check;
check << file->open << __FILE__ << __LINE__ ;
EDIT2: This is for who ever said that the syntax is not good to implement I am not a well exp. programmer. I just tried to make a quick solution for exception. My motive is it shouldn't consume more time, it should be easy to type. Because my colleagues have to use this exception class. I tried to find a solution for that, and the answer came as << operator overloading. For example consider the below sample
1) My Method
#define INFO __LINE__ << __FILE__
c++
RunAndCheck runAndCheck;
try
{
runAndCheck << checkVersion() << INFO;
runAndCheck << file->Open() << INFO;
runAndCheck << file->rename() << INFO;
}
catch(...)
{
}
2) Traditional method
#define INFO __FILE__,__LINE__
try
{
runAndCheck.check(checkVersion(),INFO);
runAndCheck.check(file->Open(),INFO);
runAndCheck.check(file->rename(),INFO);
}
catch(...)
{
}
May be in this stub it will be more or less same, but consider a situation where win32API used. There every call must be checked for exception. In that case, I found << overloading is easy to type. So that I made such a syntax
回答1:
You're likely doing something wrong then. Post the relevant bits of your code, along with a better summary of what your trying to do, and you'll get more help.
That being said, if you need to modify some data in a const object, there are a couple things you can do:
- Declare the member you want to modify
mutable
. - Use
const_cast<...>(...)
to remove the const modifier from the passed in object.
But most likely you're trying to do something the wrong way.
回答2:
operator<<
doesn't need to be a const function.
Your real problem, though, appears to be the void return type. In order to chain insertions together, you need to return *this
.
回答3:
The reason you're having trouble here is that this is an inappropriate use of operator overloading. Operator overloading is best used in these spots:
- Your type acts like a built-in type (for example, a mathematical type, array, or pointer), and you want to support the built-in operators on your type.
- Your type needs to support copying, in which case you should implement
operator =
. - Your type needs to interface with a standard library like the STL or streams, in which case you might need to implement
operator <<
for stream insertion oroperator <
to store your type in the standard container classes. - You want to implement a function object, in which case you should implement
operator ()
.
The code you have above does not fall into any of these categories, and consequently any use of it is likely to confuse people. For example, if someone not well-versed with your project sees something like this:
myCheck << myValue;
They are likely to be thinking "oh, that's some sort of stream insertion" or "oh, that's a mathematical type getting bit-shifted over." However, in your case this code really means "have the checker object myCheck
validate myValue
." If that's what you want to do, then write something more explicit like
myCheck.validate(myValue);
Now, someone looking over your code can get a much better sense for how it works and what it's trying to do.
In general, think about the Principle of Least Astonishment when writing code - code shouldn't surprise you about how it works. By using operator <<
in a nonstandard context, you are likely to cause programmers to misinterpret your code or have a hard time understanding what it does. Being more explicit about your intentions by using named functions rather than overloaded operators in this and related contexts makes the code more readable and decreases the chance that the code will trip up people reading it.
Now, as for an actual answer to your question. There is no requirement that operator <<
be a const
member function. Think about the standard streams classes like cout
or ofstream
; the operation
cout << "Hello, world!" << endl;
Certainly modifies cout
by pushing new data into it, just as the operation
cout << setfill('0') << left << hex;
Modifies cout
by changing the formatting flags. So, if you want your operator <<
function to mutate the object the data is pushed into, by all means go ahead and make it a non-const
member function. C++ doesn't have any expectations about the const
ness or non-const
ness of any overloaded operators, so you should be perfectly fine.
回答4:
You're implementing operator<<
as an inserter into our Check
object here. In this case, it looks like the right solution is to just have your insertion operator actually be non-const, since it would be nonsensical to use it on a const
object (because it's mutating logical object state).
来源:https://stackoverflow.com/questions/5062699/operator-overload-of-needs-const-produces-headache