operator-overloading

Ambiguous definition of operator() with multiple inheritance

孤者浪人 提交于 2020-01-11 08:23:05
问题 I compile this code with GCC (4.2.1 Apple build 5664) #include <cstddef> using std::size_t; template <char I> struct index { }; struct a { void operator()(size_t const &) { } }; struct b { template <char I> void operator()(index<I> const &) { } }; struct c: public a, public b { }; int main (int argc, char const *argv[]) { c vc; vc(1); return 0; } and give me the following error: main.cpp: In function ‘int main(int, const char**)’: main.cpp:22: error: request for member ‘operator()’ is

C++ friend function can't access private members

╄→尐↘猪︶ㄣ 提交于 2020-01-11 04:35:07
问题 This is supposed to be a string class with a bunch of operators and functions, including two friend functions. And those two cause some trouble for me, because the compiler says that they can not access the private members. Here is my string.h: #include <iostream> #ifndef STR_H #define STR_H namespace MyStr { class Str { private: unsigned int length; char *data; public: Str(); Str(const Str&); Str(const char*); Str(char c, unsigned int db); ~Str(); char* cStr() const; unsigned int getLength()

How to add indention to the stream operator

青春壹個敷衍的年華 提交于 2020-01-10 12:39:12
问题 In our project we use the c++ stream operator (<<) in our object model to print out an easy readible format of the data. A simplified example is this: std::ostream& operator<<(std::ostream & oStream, const OwnClass& iOwnClass) { oStream << "[SomeMember1: " << iOwnClass._ownMember1 << "]\n"; oStream << "[SomeMember2: " << iOwnClass._ownMember2 << "]\n"; } Resulting in this in the logging: [SomeMember1: foo] [SomeMember2: bar] What we want now is to be able to indent the result of that operator

Where should non-member operator overloads be placed?

偶尔善良 提交于 2020-01-09 03:49:45
问题 I want to overload operator<< for my class. Should I add this overloaded definition to the std namespace? (since the ostream operator<< is part of the std namespace) Or should I just leave it in the global namespace? In short: class MyClass { }; namespace std { ostream& operator<< ( ostream& Ostr, const MyClass& MyType ) {} } OR class MyClass { }; std::ostream& operator<< ( std::ostream& Ostr, const MyClass& MyType ) {} Which is more appropriate and why? Thanks in advance for your responses.

Is it possible to overload operators in PHP?

谁说我不能喝 提交于 2020-01-08 13:58:15
问题 Specifically, I would like to create an Array class and would like to overload the [] operator. 回答1: If you are using PHP5 (and you should be), take a look at the SPL ArrayObject classes. The documentation isn't too good, but I think if you extend ArrayObject, you'd have your "fake" array. EDIT: Here's my quick example; I'm afraid I don't have a valuable use case though: class a extends ArrayObject { public function offsetSet($i, $v) { echo 'appending ' . $v; parent::offsetSet($i, $v); } } $a

Is it possible to overload operators in PHP?

梦想的初衷 提交于 2020-01-08 13:55:01
问题 Specifically, I would like to create an Array class and would like to overload the [] operator. 回答1: If you are using PHP5 (and you should be), take a look at the SPL ArrayObject classes. The documentation isn't too good, but I think if you extend ArrayObject, you'd have your "fake" array. EDIT: Here's my quick example; I'm afraid I don't have a valuable use case though: class a extends ArrayObject { public function offsetSet($i, $v) { echo 'appending ' . $v; parent::offsetSet($i, $v); } } $a

Why override operator()?

瘦欲@ 提交于 2020-01-08 11:41:19
问题 In the Boost Signals library, they are overloading the () operator. Is this a convention in C++? For callbacks, etc.? I have seen this in code of a co-worker (who happens to be a big Boost fan). Of all the Boost goodness out there, this has only led to confusion for me. Any insight as to the reason for this overload? 回答1: One of the primary goal when overloading operator() is to create a functor. A functor acts just like a function, but it has the advantages that it is stateful, meaning it

Why override operator()?

不打扰是莪最后的温柔 提交于 2020-01-08 11:41:09
问题 In the Boost Signals library, they are overloading the () operator. Is this a convention in C++? For callbacks, etc.? I have seen this in code of a co-worker (who happens to be a big Boost fan). Of all the Boost goodness out there, this has only led to confusion for me. Any insight as to the reason for this overload? 回答1: One of the primary goal when overloading operator() is to create a functor. A functor acts just like a function, but it has the advantages that it is stateful, meaning it

is the operator '+' unary or binary?

筅森魡賤 提交于 2020-01-07 09:26:20
问题 While reading this article it seems that the operator + is unary. How is that. From my understanding a unary operator is an operator that does not depend on another variable for its operation like ++a or a-- . How is the variable '+' unary. I thought it was binary ? I would appreciate it if some one could clear this up. 回答1: + is both a unary and binary operator. The unary + form ( +a ) forces the operand to be evaluated as a number or a pointer, while the binary form + form ( a + b ) is

Operatrator [] as non-static function

ε祈祈猫儿з 提交于 2020-01-07 04:42:04
问题 Code: SchedulingItem operator[](Schedule obj,int el){ return obj.OfVector().at(el); } Error: academia::SchedulingItem academia::operator[](academia::Schedule, int)' must be a nonstatic member function SchedulingItem operator[](Schedule obj,int el) Where is the problem? 回答1: The problem is that, just as the message says, this function must be a non-static member function. That's simply a law of C++, for operator[]. You've instead made it a non-member, or "free" function. 回答2: operator[] must