Link to home
Start Free TrialLog in
Avatar of i_dont_have_a_name
i_dont_have_a_name

asked on

scope to operator<<

i have a base class of cRectangle2 and a class that inheritance it cBox

in cRectangle2 i have
friend ostream & operator<<(ostream &,const cRectangle2 &)

now in cBox i also have operator<< overloaded but i dont know how to call the cRectangle operator... i can get it to work with a static_cast but that makes an extra constructor that i should not have... how do i call the cRectangle2 operator inside the cBox operator?

Thanks
Avatar of Axter
Axter
Flag of United States of America image

Hi i_dont_have_a_name,

Please post your class header so we can have a better understanding of your requirements.


David Maisonave (Axter)
Cheers!
Taking a wild guess at your problem.... Try getting your std::ostream& operator<< to call a virtual function in your base class that takes responsibility for doing the display.

c.f.
--------8<--------
#include <iostream>
#include <string>

class base;
std::ostream& operator<<(std::ostream& os,const base&);

class base {
      virtual std::ostream& display(std::ostream& os) const;
      friend std::ostream& operator<<(std::ostream& os,const base&);
protected:
      const std::string name;
public:
      base(const std::string& name = "base") : name(name) {}
};

std::ostream& operator<<(std::ostream& os,const base& b)
{
      return b.display(os);
}

std::ostream& base::display(std::ostream& os) const
{
      return os << "base: " << name;
}

class derived : public base {
      std::ostream& display(std::ostream& os) const;
public:
      derived(const std::string& name = "derived") : base(name) {}
};

std::ostream& derived::display(std::ostream& os) const
{
      return os << "derived: " << name;
}

void blah(const base& b)
{
      std::cout << "This is a base: " << b << '\n';
}

int main()
{
      base b("benny");
      derived d("danny");
      blah(b);
      blah(d);
}
--------8<--------
Avatar of i_dont_have_a_name
i_dont_have_a_name

ASKER

i just wanted to know how to scope down to a friend function but i really dont care any more i just did it the long way


Thanks anyways
>>but i really dont care any more i just did it the long way

Can you please award points to rstaveley, who gave you a valid method, and close this question?

Thanks
rstaveley does not really have what i was looking for (have more time to go over it... was on a road trip)...

this is what i have

class cRectangle2 {
      friend ostream & operator<<(ostream & where, const cRectangle2 & t);
       {
             where <<  setw(10) << t.getLength() << setw(10) << t.getWidth();
             return where;
        }

      public:
               .....
               .....
      private:
                ....
                ....
};
////////////////////////////////////////
class cBox : public cRectangle2 {
        // trying to scope to cRectangle2 ostream first then print extra
      friend ostream & operator<<(ostream & where, const cBox & t);
        {
          //      where << t.cRectangle2:: << t.getHeight();  // Does not work
          //      where << static_cast<cRectangle2>(t) << t.getHeight();  //Works but extra constructor
          //      where << cRectangle2(t) << t.getHeight();  //Works but extra constructor
       
                return where;
        }
      public:
               .....
               .....
               .....
      private:
                ....
                ....
};
>>rstaveley does not really have what i was looking for (have more time to go over it... was on a road trip)...

If you try his implementation, you'll find that it does have what you're looking for.

You need to transfer the logic to a virtual function, and the way to do that, is to have the friend function call the virtual function.
i found the answeri was looking for all i need to do was this

base & temp = t;
where << t;
OK, I understand your question now. The static cast would amount to the same thing. You wouldn't be calling the copy constructor, but don't take my word for it. Test.
its not that your code did not work its that i could not change my code around like u had because it was for homwork and part of it was to keep that format
ASKER CERTIFIED SOLUTION
Avatar of rstaveley
rstaveley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial