Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set the right method

How to set the right method

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 6 Posters 974 Views
  • 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
    Zunneh
    wrote on last edited by
    #1

    Hello guys , i have a question and this is an exemple to understand my question

    class A {
     A();
     ~A();
     virtual void test();
    
    ;}
    
    class B :public A{
     B();
     ~B();
     virtual void test();
     ;}
    
     class C:public A {
     C();
     ~C();
     virtual void test();
    
     ;}
    

    as you see , i m using Heritage her , for exemple

    B.test() 
    

    will call the method in class B ,
    Now if i write Alone

               test(); // 
    

    Which method test() he will call ? the A,B or C?

    my english is average, please use simple words and try to be the most explicit, thank you

    JonBJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      since its a member function, you need a class instance to call it.
      so
      test(); //
      wont work.

      1 Reply Last reply
      1
      • Z Zunneh

        Hello guys , i have a question and this is an exemple to understand my question

        class A {
         A();
         ~A();
         virtual void test();
        
        ;}
        
        class B :public A{
         B();
         ~B();
         virtual void test();
         ;}
        
         class C:public A {
         C();
         ~C();
         virtual void test();
        
         ;}
        

        as you see , i m using Heritage her , for exemple

        B.test() 
        

        will call the method in class B ,
        Now if i write Alone

                   test(); // 
        

        Which method test() he will call ? the A,B or C?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @Zunneh
        As @mrjj has said, you will need to create instance, e.g. one of new A(), new B(), new C(), before you can call test() on that instance. And it will call the test() of whichever class you created the instance of, e.g.

        B *b = new B();
        b->test();
        // or
        B b;
        b.test();
        

        will call B's test().

        Z 1 Reply Last reply
        3
        • JonBJ JonB

          @Zunneh
          As @mrjj has said, you will need to create instance, e.g. one of new A(), new B(), new C(), before you can call test() on that instance. And it will call the test() of whichever class you created the instance of, e.g.

          B *b = new B();
          b->test();
          // or
          B b;
          b.test();
          

          will call B's test().

          Z Offline
          Z Offline
          Zunneh
          wrote on last edited by
          #4

          @JonB an if i delete the object , for exemple

          B *b=new B();
          delete b;
          test(); // this will work ?
          

          my english is average, please use simple words and try to be the most explicit, thank you

          mrjjM 1 Reply Last reply
          0
          • Z Zunneh

            @JonB an if i delete the object , for exemple

            B *b=new B();
            delete b;
            test(); // this will work ?
            
            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Zunneh
            hi
            Nope. There is no way to call a normal member function without the object.
            Why do you want to do this ?
            you could just have a normal non class function then.

            Pl45m4P 1 Reply Last reply
            0
            • mrjjM mrjj

              @Zunneh
              hi
              Nope. There is no way to call a normal member function without the object.
              Why do you want to do this ?
              you could just have a normal non class function then.

              Pl45m4P Online
              Pl45m4P Online
              Pl45m4
              wrote on last edited by
              #6

              @mrjj

              This works, if he calls test() inside the B class (e.g. constructor).

              @Zunneh
              Where do you want to call the test()-function?

              Back to your question: B inherits from A, C inherits from A. Because "test" is a virtual function in A, it will call the test()-function in B, but of course your function call wont work :)


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              jsulmJ 1 Reply Last reply
              0
              • Pl45m4P Pl45m4

                @mrjj

                This works, if he calls test() inside the B class (e.g. constructor).

                @Zunneh
                Where do you want to call the test()-function?

                Back to your question: B inherits from A, C inherits from A. Because "test" is a virtual function in A, it will call the test()-function in B, but of course your function call wont work :)

                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on last edited by jsulm
                #7

                @Pl45m4 said in How to set the right method:

                This works, if he calls test() inside the B class

                No, it doesn't. Constructor is a method of a class also. So, if you call a method inside the constructor you're calling it with a class instance already (not necessarily completely constructed one though).
                This does not make any sense in C++ (if test() is a method of B or a base class of B), not even for static methods:

                B *b=new B();
                delete b;
                test();
                

                @Zunneh You simply can't call a non-static method without a class instance. You can have a static method, then you don't need an instance:

                class B
                {
                public:
                    static void test() {}
                };
                B::test();
                

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                Pl45m4P 1 Reply Last reply
                4
                • jsulmJ jsulm

                  @Pl45m4 said in How to set the right method:

                  This works, if he calls test() inside the B class

                  No, it doesn't. Constructor is a method of a class also. So, if you call a method inside the constructor you're calling it with a class instance already (not necessarily completely constructed one though).
                  This does not make any sense in C++ (if test() is a method of B or a base class of B), not even for static methods:

                  B *b=new B();
                  delete b;
                  test();
                  

                  @Zunneh You simply can't call a non-static method without a class instance. You can have a static method, then you don't need an instance:

                  class B
                  {
                  public:
                      static void test() {}
                  };
                  B::test();
                  
                  Pl45m4P Online
                  Pl45m4P Online
                  Pl45m4
                  wrote on last edited by
                  #8

                  @jsulm

                  I meant if he calls it in constructor, he doesn't have to "B->test ()".
                  But yeah, to call the constructor, you'll need an object instance first :)


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  1 Reply Last reply
                  0
                  • KillerSmathK Offline
                    KillerSmathK Offline
                    KillerSmath
                    wrote on last edited by KillerSmath
                    #9

                    @Zunneh
                    To understand what the virtual term, i suggest you to read this IBM article.

                    Static vs Dynamic Binding
                    https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_72/rzarg/cplr139.htm

                    Virtual functions are dynamically bound, meaning that the particular
                    function called is determined by the dynamic type of the object
                    through which it’s invoked (Mayer S. Effective C++: 55 Specific Ways to Improve Your Programs and Designs. Third edition. p. 181)

                    A *a = new A();
                    B *b = new B();
                    
                    a->test() // call A::test()
                    b->test() // call B::test()
                    
                    a = b;
                    
                    a->test() // call B::test() because A::test() is virtual
                    
                    delete a;
                    delete b;
                    
                    

                    @Computer Science Student - Brazil
                    Web Developer and Researcher
                    “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                    Pl45m4P 1 Reply Last reply
                    4
                    • KillerSmathK KillerSmath

                      @Zunneh
                      To understand what the virtual term, i suggest you to read this IBM article.

                      Static vs Dynamic Binding
                      https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_72/rzarg/cplr139.htm

                      Virtual functions are dynamically bound, meaning that the particular
                      function called is determined by the dynamic type of the object
                      through which it’s invoked (Mayer S. Effective C++: 55 Specific Ways to Improve Your Programs and Designs. Third edition. p. 181)

                      A *a = new A();
                      B *b = new B();
                      
                      a->test() // call A::test()
                      b->test() // call B::test()
                      
                      a = b;
                      
                      a->test() // call B::test() because A::test() is virtual
                      
                      delete a;
                      delete b;
                      
                      
                      Pl45m4P Online
                      Pl45m4P Online
                      Pl45m4
                      wrote on last edited by
                      #10

                      @KillerSmath

                      Why you mentioned me?! I know what virtual functions are :)
                      I guess you meant @Zunneh


                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      KillerSmathK 1 Reply Last reply
                      0
                      • Pl45m4P Pl45m4

                        @KillerSmath

                        Why you mentioned me?! I know what virtual functions are :)
                        I guess you meant @Zunneh

                        KillerSmathK Offline
                        KillerSmathK Offline
                        KillerSmath
                        wrote on last edited by
                        #11

                        @Pl45m4
                        Just a little mistake xD.

                        @Computer Science Student - Brazil
                        Web Developer and Researcher
                        “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                        1 Reply Last reply
                        1

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved