Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. operator << inherited?

operator << inherited?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Budric B
    wrote on last edited by
    #1

    Hi, I was wondering if the << operator is inherited? I can't get the following to work:

    class A
    {
    public:
    A& operator << (double & arg)
    {
    return *this;
    }
    };
    class B : public A
    {
    public:
    B & operator<<(int & arg)
    {
    return *this;
    }
    };
    ...
    B test;
    test << 5.0;

    with the error that no operator that takes double as argument.

    L 1 Reply Last reply
    0
    • B Budric B

      Hi, I was wondering if the << operator is inherited? I can't get the following to work:

      class A
      {
      public:
      A& operator << (double & arg)
      {
      return *this;
      }
      };
      class B : public A
      {
      public:
      B & operator<<(int & arg)
      {
      return *this;
      }
      };
      ...
      B test;
      test << 5.0;

      with the error that no operator that takes double as argument.

      L Offline
      L Offline
      Lea Hayes
      wrote on last edited by
      #2

      Hi, Class 'B' is overloading the operator '<<'. In quick mockup test I managed to get the following to work: class A { public: A& operator << (double arg) { return *this; } }; class B : public A { public: B& operator << (int arg) { return *this; } using A::operator <<; // Provide both implementations. }; I ran into difficulties when using the reference operator '&' for the two arguments. I hope this is of use! Lea Hayes

      B 1 Reply Last reply
      0
      • L Lea Hayes

        Hi, Class 'B' is overloading the operator '<<'. In quick mockup test I managed to get the following to work: class A { public: A& operator << (double arg) { return *this; } }; class B : public A { public: B& operator << (int arg) { return *this; } using A::operator <<; // Provide both implementations. }; I ran into difficulties when using the reference operator '&' for the two arguments. I hope this is of use! Lea Hayes

        B Offline
        B Offline
        Budric B
        wrote on last edited by
        #3

        It appears that when I remove the reference operator, I don't have to specify "using". It's a shame though as I was hoping to subclass an existing implementation and just add a few of my own << operators to write my own types. Looks like I'll have to try defining the operators outside of the class.

        L 1 Reply Last reply
        0
        • B Budric B

          It appears that when I remove the reference operator, I don't have to specify "using". It's a shame though as I was hoping to subclass an existing implementation and just add a few of my own << operators to write my own types. Looks like I'll have to try defining the operators outside of the class.

          L Offline
          L Offline
          Lea Hayes
          wrote on last edited by
          #4

          Hi, Whilst not necessarily the perfect solution, you could inline the operators. class B : public A { ... public: inline A& operator << (double & atr) { return A::operator << (atr); } }; This might be a useful workaround for your scenario. Lea Hayes

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups