CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2002
    Posts
    5,757

    Polymorphism & Overloaded Operators :: C++

    Hi.

    I would like to know is it possible to apply polymorphism to overloaded operators? For examples, is it possible to rely on polymorphism via creating virtual overloaded operators and friend functions?

    Consider the ostream and istream for instance. I would like to design a hierarchy such as this.

    Code:
    Base *pB;
    Derived *pD = Derived();
    pB = dynamic_cast<Base *>(pD);
    
    // I would like this line to call an ostream friend function in the *Derived* class.
    
    cout << pB;
    My code might be off. Nonetheless, the point I want to get at is applying polymorphism to overloaded functions and friend functions, especially iostream.

    Thanks,
    Kuphryn

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Yeah, but you should do something that seems totally obvious
    AFTER you hear about it This is from Item 25 of Scott Meyers'
    book "More Effective C++":

    ...
    Neither of these solutions is very satisifying. What we want is a
    non-member function called operator<< that exhibits the
    behavior of a virtual function like print. The description of what we
    want is in fact very close to a description of how to get it. We
    define BOTH operator<< and print and have the former call the
    latter!

    Code:
    class NLComponent {
    public:
       virtual ostream& print(ostream& s) const = 0;
       ...
    };
    
    class TextBlock : public NLComponent {
    public:
       virtual ostream& print(ostream& s) const;
       ...
    };
    
    class Graphic : public NLComponent {
    public:
       virtual ostream& print(ostream& s) const;
    };
    
    inline ostream& operator<<(ostream& s, const NLComponent& c)
    {
       return c.print(s);
    }
    Virtual-acting non-member functions, then, are easy. You write
    virtual functions to do the work, then write a non-virtual function
    that does nothing but call the virtual function. To avoid incurring
    the cost of a function call for this syntactic sleight-of-hand, of
    course, you inline the non-virtual function
    --Paul

  3. #3
    Join Date
    Feb 2002
    Posts
    5,757
    Wow!!!!!!!!!!! Nice!!!!!

    Lets slow down and start from the beginning. The code is very clean and effective. Note I have heard recommendations for Scott Meyers' More Effective C++, but I have read it. However, after seeing your post I am beginning to take interest in it.

    Does the inline function belong to the client part of the program?

    Code:
    inline ostream& operator<<(ostream& s, const NLComponent& c)
    {
       return c.print(s);
    }
    Lastly, is Scott Meyers' More Effective C++ a good C++ book? I have read C++ How to Program by Deitel&Deitel, The C++ Standard Library by Nicolai Josuttis , and The C++ Programming Language by Bjarne Stroustrup. Nonetheless, I hear recommendations for More Effective C++, but I have not given much thought to it until now.

    Thanks,
    Kuphryn

  4. #4
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    When I took C++ in college, they made us buy two books:
    1) Deitel and Deitel's second edition C++ book
    2) Bjarne Stroustrup's The C++ Language 2nd Edition

    So we have a similar background. I've found the Scott Meyers
    series to be VERY helpful. I still haven't mastered all of his
    concepts, but you can read these books all the way through and
    then use them as reference materials [like oh I remember reading
    about this in Effective C++ so I'll go look it up] I'm not saying
    they'll be helpful for everyone, but all three of his books were
    very helpful for me. They are:
    Effective C++
    More Effective C++
    Effective STL

    You can put the inline function in the header file for the
    NLComponent class [at least that's where I think I'd put it].

    --Paul

  5. #5
    Join Date
    Feb 2002
    Posts
    5,757
    Okay. Thanks.

    Kuphryn

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured