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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Virtual Classes

Virtual Classes

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 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.
  • J Offline
    J Offline
    JKallen
    wrote on last edited by
    #1

    When inheriting a virtual class, do I have to completely redefine virtual functions of the base class. For instance, does the function following COMENT A work, or do I have to completely rebuild the operator = function? Thanks. class Base{ public: Base(); ~Base(); virtual Base& operator = (const Base& base); }; class Inherited : virtual public Base{ public: Inherited(); ~Inherited(); virtual Inherited& operator = (const Inherited& inherited); }; //COMENT A Inherited& Inherited::operator = (const Inherited& inherited){ if (&inherited != this){ Base::operator = (inherited); //do some other copying } return *this; }

    B 1 Reply Last reply
    0
    • J JKallen

      When inheriting a virtual class, do I have to completely redefine virtual functions of the base class. For instance, does the function following COMENT A work, or do I have to completely rebuild the operator = function? Thanks. class Base{ public: Base(); ~Base(); virtual Base& operator = (const Base& base); }; class Inherited : virtual public Base{ public: Inherited(); ~Inherited(); virtual Inherited& operator = (const Inherited& inherited); }; //COMENT A Inherited& Inherited::operator = (const Inherited& inherited){ if (&inherited != this){ Base::operator = (inherited); //do some other copying } return *this; }

      B Offline
      B Offline
      Bob Ciora
      wrote on last edited by
      #2

      Why are you using virtual inheritance here? Generally, you inherit virtually when you're using multiple inheritance, and when those base classes share some common base class at some point. For single inheritance, as in your example, you don't need to use virtual inheritance when declaring the class. For your example, why not try compliling and single-stepping through the code? That way, you'll know if it works or not. Personally, I'm not sure :) Bob Ciora

      J 1 Reply Last reply
      0
      • B Bob Ciora

        Why are you using virtual inheritance here? Generally, you inherit virtually when you're using multiple inheritance, and when those base classes share some common base class at some point. For single inheritance, as in your example, you don't need to use virtual inheritance when declaring the class. For your example, why not try compliling and single-stepping through the code? That way, you'll know if it works or not. Personally, I'm not sure :) Bob Ciora

        J Offline
        J Offline
        JKallen
        wrote on last edited by
        #3

        Hi. Thanks for your response. I am using multiple inheritance. I just didn't want to burden a potential responder with reading unecessary code. With respect to your comment, I have stepped through, and it doesn't appear that I have to completely redefine the function. I can call the base function in the derived function even when the base function is virtual and it calls the right (or wrong depending on your view) version. The documentation suggests this should not be the case.

        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