Polymorphism & Overloaded Operators :: C++

This is a discussion on Polymorphism & Overloaded Operators :: C++ within the C++ Programming forums, part of the General Programming Boards category; Hi. I would like to know is it possible to apply polymorphism to overloaded operators? For examples, is it possible ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    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
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    i think it is possible. try it and see for yourself.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Thanks.

    I tried implementing a friend function as a virtual functions. Visual C++ compiler showed an error. I believe it said the friend function is not part of the class.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Problem with overloaded operators, templates and inheritance
    By bleakcabal in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2004, 05:07 AM
  3. Polymorphism and class operators
    By Yoshi in forum C++ Programming
    Replies: 5
    Last Post: 01-20-2003, 02:55 PM
  4. overloaded operators
    By ivandn in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2001, 03:52 PM
  5. help with overloaded operators
    By doleman19 in forum C++ Programming
    Replies: 23
    Last Post: 10-23-2001, 03:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21