Solved

scope to operator<<

Posted on 2006-11-24
10
245 Views
Last Modified: 2013-12-14
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
0
Comment
Question by:i_dont_have_a_name
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 18008912
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!
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 18009461
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<--------
0
 

Author Comment

by:i_dont_have_a_name
ID: 18013696
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Axter
ID: 18015104
>>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
0
 

Author Comment

by:i_dont_have_a_name
ID: 18015696
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:
                ....
                ....
};
0
 
LVL 30

Expert Comment

by:Axter
ID: 18015739
>>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.
0
 

Author Comment

by:i_dont_have_a_name
ID: 18048662
i found the answeri was looking for all i need to do was this

base & temp = t;
where << t;
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 18048814
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.
0
 

Author Comment

by:i_dont_have_a_name
ID: 18048919
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
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 125 total points
ID: 18049398
Here you go, with the static cast. No copy construction.

--------8<--------
#include <iostream>

struct base {};

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

struct derived : public base {};

std::ostream& operator<<(std::ostream& os,const derived&)
{
      return os << "derived";
}

int main()
{
      base b;
      derived d;
      std::cout << b << '\n';
      std::cout << d << '\n';
      std::cout << static_cast<base&>(d) << '\n';
}
--------8<--------
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ to C# code conversion issue 4 128
C++ question 3 81
Copy output image from TWindowsMediaPlayer 6 67
Visual Studio 2013 and Eclipse Neon 6 100
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question