Google Groups Home
Help | Sign in
Message from discussion <Samples> Member and non-member operators
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post will appear after it is approved by moderators
Alex Vinokur  
View profile
 More options Jun 10 2006, 1:39 pm
From: Alex Vinokur <ale...@users.sourceforge.net>
Date: Sat, 10 Jun 2006 20:39:14 +0300
Local: Sat, Jun 10 2006 1:39 pm
Subject: <Samples> Member and non-member operators

// ==========================================
// * 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 2K ]
// ==========================================
// * 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;

}

  mem-nomem-operators-1-2.log
< 1K Download

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google