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
Forum Updated to NodeBB v4.3 + New Features

How to set the right method

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 6 Posters 1.1k Views 3 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
    Zunneh
    wrote on 22 Apr 2019, 09:46 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

    J 1 Reply Last reply 22 Apr 2019, 11:10
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 22 Apr 2019, 10:01 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
        22 Apr 2019, 09:46

        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?

        J Offline
        J Offline
        JonB
        wrote on 22 Apr 2019, 11:10 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 22 Apr 2019, 12:31
        3
        • J JonB
          22 Apr 2019, 11:10

          @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 22 Apr 2019, 12:31 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

          M 1 Reply Last reply 22 Apr 2019, 12:35
          0
          • Z Zunneh
            22 Apr 2019, 12:31

            @JonB an if i delete the object , for exemple

            B *b=new B();
            delete b;
            test(); // this will work ?
            
            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 22 Apr 2019, 12:35 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.

            P 1 Reply Last reply 22 Apr 2019, 14:05
            0
            • M mrjj
              22 Apr 2019, 12:35

              @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.

              P Offline
              P Offline
              Pl45m4
              wrote on 22 Apr 2019, 14:05 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 23 Apr 2019, 04:53
              0
              • P Pl45m4
                22 Apr 2019, 14:05

                @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 Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 23 Apr 2019, 04:53 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

                P 1 Reply Last reply 23 Apr 2019, 10:52
                4
                • jsulmJ jsulm
                  23 Apr 2019, 04:53

                  @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();
                  
                  P Offline
                  P Offline
                  Pl45m4
                  wrote on 23 Apr 2019, 10:52 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 23 Apr 2019, 12:05 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

                    P 1 Reply Last reply 23 Apr 2019, 12:18
                    4
                    • KillerSmathK KillerSmath
                      23 Apr 2019, 12:05

                      @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;
                      
                      
                      P Offline
                      P Offline
                      Pl45m4
                      wrote on 23 Apr 2019, 12:18 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 23 Apr 2019, 12:20
                      0
                      • P Pl45m4
                        23 Apr 2019, 12:18

                        @KillerSmath

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

                        KillerSmathK Offline
                        KillerSmathK Offline
                        KillerSmath
                        wrote on 23 Apr 2019, 12:20 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

                        1/11

                        22 Apr 2019, 09:46

                        • Login

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