Operator inheritance

How can I inherit an overloaded operator?
This is what I have
class Base
{
protected:
  Base& operator<<(char* str);
}
class Derived:public Base
{
 ...
}
Derived d;

I do not want to redefine << in Derived; however, when I call
d<<str;
VC6 compiler complains about << not available for d.
LVL 1
olegspAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

akalmaniCommented:
Hi !!
    My dear oleqsp operators are not inheritable.
0
inprasCommented:
#include "iostream.h"
Hi
try this
class Base
{
  public:
    Base& operator<<(char* str)
      {
            cout << str;
            return *this;
      }
} ;

class Derived:public Base
{
} ;
void main()
{
      char str[10];
  Derived d;
  cin >> str;
  d<<str;
}

remove protected and put public
regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KangaRooCommented:
Tough luck, the compiler is correct, Base::operator<<(char*) is protected and that access qualification can not be changed (you cannot make it public) from a derived class.
You could overload it:

class Derived : public Base
{
   public:
     Base& operator<<(char* s){Base::operator<<(s);}
};
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

KangaRooCommented:
Off course, if you have access to the class' source.... making it public \em{could} be ok.
0
sureshkumarCommented:
>>,<< these are already members of another class (i.e., members f iostream class) when you are overloading these operators you have to declare as friend functions to the base class then there is no inheritance concept at that point.

ok.....

further queries welcome
0
sureshkumarCommented:
hi kangaroo

is it right i.e., overloading friend functions.
0
KangaRooCommented:
sureshkamer, I don't understand your last question regarding the friend stuff.

It doesn't matter if operator << and >> are  already declared in another class. They can be declared for any class and their use is not restricted to iostreams.

And yes, you can use overloading, inheritance and polymorphism with these operators, just as with any other member functions and operators.

NB. When declared for use with iostream they are declared as friends because the first argument should be an iostream object:
   ostream& operator<<(ostream& out, MyClass& object);
   // to use as in
   out << anObject;
and when declared as a member of MyClass the first argument would be the MyClass obejct this
   ostream& operator<<(ostream& out);
   // to use as in
   anObject << out; // Bit different
0
sureshkumarCommented:
my doubt is

istream& ostream& are the members of iostream class so we can't define as members of another class.so we have to declare as friend function.
ok....


now if we are declaring friend functions to the base class then there is no inheritance concept at that point.
that is what i am saying.



please give me feedback.

0
KangaRooCommented:
The iostream classes do not come into the picture here.
Inheritance is not a compelling reason to declare operators as friends. Usually operators are declared as friend because this allow the compiler to swap arguments:

class Vector
{
 public:
    Vector operator*(double scalar);
};

Vector v1, v2;
v2 = v1 * 2.0; // ok
v2 = 2.0 * v1; // Not ok, no match for
      // operator*(double, Vector)

Now if we use friends:
class Vector
{
 public:
    friend Vector operator*(Vector v, double scalar);
};

the second operation would be legal.
0
sureshkumarCommented:
sorry i am not getting you clearly.

my doubts are :

1.can we declare member functions  of one class to the members of other class.

2.can we inherit friend functions.
0
KangaRooCommented:
>> 1.can we declare member functions  of one class
>> to the members of other class.
Not sure what you mean by that?

>> 2.can we inherit friend functions.
Classes can only inherit members (data and functions). Friendship is not inherited, if class A declares a function f as a friend, then this function is not a friend of other classes, even if these inherit from A. Thus f can access private members of A but not of its derived classes. It may however access the private members that those derived classes inherited from A. Example:

class A;
void function(A& a);

class A
{
   friend void function(A&);
   void f();
};

class B : public A {};


void function(A& a)
{
   a.f();
}

void g()
{
    A a;
    B b;
    function(a);
    function(b);
}
0
KangaRooCommented:
However, if B overloads functions from A the situation may change.

class A;
class B;
void function(A&, B&);

class A
{
   friend void function(A&, B&);
   void f();
   virtual void v(){}
};

class B : public A
{
   virtual void v(){}
   void f() {}
   
};


void function(A& a, B& b)
{
   a.f();
   a.v();

   b.f(); // illegal
   b.v(); // illegal
}

void g()
{
    A a;
    B b;
    function(a, b);
}
0
olegspAuthor Commented:
amikami>>Hi !!
amikami>>My dear oleqsp operators are
amikami>>not inheritable.


To amikami: Please do not confuse when you are not sure:

"All overloaded operators except assignment (operator=) are inherited by derived classes" (see MFC documentation on overloading operators)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.