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. C++ Daimond Problem

C++ Daimond Problem

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
6 Posts 6 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.
  • Z Offline
    Z Offline
    Zeeshan Riaz
    wrote on last edited by
    #1

    What is the solution of the diamond problem of Inheriting Classes. A class is inheritting from 2 base classes and both the base classes have a function/method with the same name. From the object of the child class how we can call a function of the one base class.

    N R J S T 5 Replies Last reply
    0
    • Z Zeeshan Riaz

      What is the solution of the diamond problem of Inheriting Classes. A class is inheritting from 2 base classes and both the base classes have a function/method with the same name. From the object of the child class how we can call a function of the one base class.

      N Offline
      N Offline
      Nelek
      wrote on last edited by
      #2

      If the functions have the same name but they get different parameters... only the function that fits the parameters should be called. Function (int Par1, CString Par2); Function (double Par1, int Par2); is not possible to call both at once although they have the same name. If both functions get the same number and typ of parameters, in the same order... then let's wait to another explanation from the high level users and I will learn it as well ;)

      Greetings. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson Rating helpfull answers is nice, but saying thanks can be even nicer.

      1 Reply Last reply
      0
      • Z Zeeshan Riaz

        What is the solution of the diamond problem of Inheriting Classes. A class is inheritting from 2 base classes and both the base classes have a function/method with the same name. From the object of the child class how we can call a function of the one base class.

        R Offline
        R Offline
        Rinu_Raj
        wrote on last edited by
        #3

        Hope you are aware of run time polymorhism. Keep the same name function/method as virtual function in the base class. For the derived class object pointer allocate the corresponding base class (whose function needs to be invoked) then call the function resulting the corresponding base class get invoked. See below to make things clear

        class A
        {
        public:
        virtual void show(){cout<<"Class A invoked"};
        }
        class B
        {
        public:
        virtual void show(){cout<<"Class B invoked"};
        }
        class C: public A, public B
        {
        }

        main()
        {
        C* ptr = new A;
        ptr->show(); // "Class A invoked" will be printed
        C* ptr1 = new B;
        ptr1->show(); // "Class B invoked" will be printed
        }

        1 Reply Last reply
        0
        • Z Zeeshan Riaz

          What is the solution of the diamond problem of Inheriting Classes. A class is inheritting from 2 base classes and both the base classes have a function/method with the same name. From the object of the child class how we can call a function of the one base class.

          J Offline
          J Offline
          Jijo Raj
          wrote on last edited by
          #4

          Muhammad Zeeshan wrote:

          A class is inheritting from 2 base classes and both the base classes have a function/method with the same name.

          The class structure is as follows?

          Base1       Base2
            \\           /
             \\         /
          	    \\       /
               Derived
          

          If this is the class structure, then its not a classic C++ diamond problem.

          Muhammad Zeeshan wrote:

          From the object of the child class how we can call a function of the one base class.

          Just prefix the base class name before the call. See the code snippet.

          Derived DerivedObject;
          DerivedObject.**Base1::**CommonFunction();

          // If its allocated as pointer.
          Derived* pDerivedObject = new Derived;
          pDerivedObject->**Base1::**CommonFunction();

          Does this solve your problem? Or is it something else? Regards, Jijo.

          _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

          1 Reply Last reply
          0
          • Z Zeeshan Riaz

            What is the solution of the diamond problem of Inheriting Classes. A class is inheritting from 2 base classes and both the base classes have a function/method with the same name. From the object of the child class how we can call a function of the one base class.

            S Offline
            S Offline
            SandipG
            wrote on last edited by
            #5

            Use virtual public for deriving classes.

            1 Reply Last reply
            0
            • Z Zeeshan Riaz

              What is the solution of the diamond problem of Inheriting Classes. A class is inheritting from 2 base classes and both the base classes have a function/method with the same name. From the object of the child class how we can call a function of the one base class.

              T Offline
              T Offline
              ThatsAlok
              wrote on last edited by
              #6

              scope resolution operator [::]

              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
              Never mind - my own stupidity is the source of every "problem" - Mixture

              cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/codeProject$$>

              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