Delphi Clinic C++Builder Gate Training & Consultancy Delphi Notes Weblog Dr.Bob's Webshop
Dr.Bob's Kylix Kicks
 Borland C++Builder lost+found #6
See Also: C++Builder Papers and Columns

Friends near and far
One of the gotcha's in the new C++ standard is the lookup of friend functions. The 'Standard' stipulates that friends must be in the same namespace as the class that wants to befriend either a clas or a function. This may lead to some trouble. Consider:

namespace MyNameSpace
{
  class Foo
  {
    public:
      Foo();
    virtual ~Foo();
      friend ostream& operator<<(ostream& ostr, const Foo& object);
    };
  };
Havoc! Actually, it is quite difficult to accomplish this! Look at the following:
namespace MyFoo
{
  class Foo
  {
  public:
    Foo();
    virtual ~Foo();
Nothing new. However, in the next section is the goody: specify the namespace!!!
  friend std::ostream& operator<<(std::ostream& ostr, const Foo& object);
  protected:
    virtual void PrintOn(std::ostream& ostr) const;
  };
};

MyFoo::Foo::Foo()
{}

MyFoo::Foo::~Foo()
{}
Quite elaorate, just to let the compiler search in the right place!
std::ostream& MyFoo::operator<<(std::ostream& ostr, const MyFoo::Foo& object)
{
  ostr << object << std::endl;
  return ostr;
}

void MyFoo::Foo::PrintOn(std::ostream& ostr) const
{
  ostr << "Foo";
}
Get it? Got it? Great!


This webpage © 2000-2017 by Bob Swart (aka Dr.Bob - www.drbob42.com). All Rights Reserved.