// ==========================================
// * Samples
// * Member and non-member operators
// * Usage Sample Code
// ------------------------------------------
// * Language : C++
// * Compiler: GNU g++ 4.0.1
// * Content : Program files, Log files
// * Version : MOP-1.2
// ------------------------------------------
// * Attachments
// - mem-nomem-operators-1-2.cpp
// - mem-nomem-operators-1-2.log
// ==========================================
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
[
mem-nomem-operators-1-2.cpp ]
// ==========================================
// * Samples
// * Member and non-member operators
// * Usage Sample Code
// ------------------------------------------
// * Language : C++
// * Compiler: GNU g++ 4.0.1
// * Content : Program files, Log files
// * Version : MOP-1.2
// ------------------------------------------
// * Attachments
// - mem-nomem-operators-1-2.cpp
// - mem-nomem-operators-1-2.log
// ==========================================
#include <iostream>
using namespace std;
// ------
struct Blah1 {};
struct Blah2 {};
// ------
class Foo
{
friend Foo operator+ (const Foo& arg1, const Foo& arg2);
friend Foo operator+ (int arg1, const Foo& arg2);
friend Foo operator+ (const Foo& arg1, int arg2);
private:
int m_value;
public :
Foo () {}
Foo (const Blah1&)
{
cout << "Constructor: " << __PRETTY_FUNCTION__ << endl;
}
explicit Foo (const Blah2&)
{
cout << "Constructor: " << __PRETTY_FUNCTION__ << endl;
}
Foo operator- (const Foo& arg)
{
Foo foo;
foo.m_value = m_value + arg.m_value;
cout << "Member: " << __PRETTY_FUNCTION__ << endl;
return foo;
}
Foo operator- (int arg)
{
Foo foo;
foo.m_value = m_value - arg;
cout << "Member: " << __PRETTY_FUNCTION__ << endl;
return foo;
}
};
// ------
Foo operator+ (const Foo& arg1, const Foo& arg2)
{
Foo foo;
foo.m_value = arg1.m_value + arg2.m_value;
cout << "Non-Member: " << __PRETTY_FUNCTION__ << endl;
return foo;
}
// ------
Foo operator+ (int arg1, const Foo& arg2)
{
Foo foo;
foo.m_value = arg1 + arg2.m_value;
cout << "Non-Member: " << __PRETTY_FUNCTION__ << endl;
return foo;
}
// ------
Foo operator+ (const Foo& arg1, int arg2)
{
Foo foo;
foo.m_value = arg1.m_value + arg2;
cout << "Non-Member: " << __PRETTY_FUNCTION__ << endl;
return foo;
}
// ------
struct Bar
{
operator Foo () { cout << "operator: " << __PRETTY_FUNCTION__ << endl;}
};
// ------
int main ()
{
Foo f0, f1, f2;
f0 = f1 + f2;
f0 = f1 - f2;
cout << endl;
int i1 = 127;
f0 = f1 + i1;
f0 = i1 + f1;
f0 = f1 - i1;
// f0 = i1 - f1; // no match for 'operator-' in 'i1 - f1'
cout << endl;
Blah1 b1;
f0 = f1 + b1;
f0 = b1 + f1;
f0 = f1 - b1;
// f0 = b1 - f1; // no match for 'operator-' in 'b1 - f1'
cout << endl;
Blah2 b2;
// f0 = f1 + b2; // no match for 'operator+' in 'f1 + b2'
// f0 = b2 + f1; // no match for 'operator+' in 'b2 + f1'
// f0 = f1 - b2; // no match for 'operator+' in 'b2 + f1'
// f0 = b1 - f1; // no match for 'operator-' in 'b1 - f1'
cout << endl;
Foo foo;
Bar bar;
f0 = foo + bar;
f0 = foo - bar;
f0 = bar + foo;
// f0 = bar - foo; // no match for 'operator-' in 'bar - foo'
return 0;
}