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 create class derived from custom class and access protected members in Qt
Forum Updated to NodeBB v4.3 + New Features

How to create class derived from custom class and access protected members in Qt

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 1.4k 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.
  • N Offline
    N Offline
    nikhil30
    wrote on 4 Jun 2020, 14:39 last edited by
    #1

    Hi,
    I have created derived class (MEL) from custom class (FEL)(this is derived from QObject) and trying to access protected member in derived class. But if i read the protected member, its not showing the updated value in derived class MEL.
    It shows the value assigned in constructor of base custom class FEL.

    Below is my code snippet :-

    class FEL : public QObject
    {
       QObject
    
       protected : //so that it can be accessed in child class
       bool flag;
        
        void foo();
        ...
    }
    
    void foo() updates the flag.
     
    class MEL : public FEL
    {
         QObject
         
         void myfunc();
    }
    
    void MEL ::myfunc()
    {
         bool a = flag; // here not getting flag value assigned in myfunc() of FEL class.
    }
    

    Please suggest how to use base class members (FEL) in derived class (MEL) in Qt...?

    J 1 Reply Last reply 4 Jun 2020, 14:47
    0
    • N nikhil30
      4 Jun 2020, 15:18

      My mistake.

      Actually below is my main class :- which has MEL & FEL type objects as its members.

      class main : public QObject
      {
      FEL * fel;
      MEL * mel;
      }

      Now the query is how can i access the members of FEL in MEL object....?
      Can i pass the object of FEL to MEL type object in Qt..?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 5 Jun 2020, 04:45 last edited by
      #6

      @nikhil30 said in How to create class derived from custom class and access protected members in Qt:

      Now the query is how can i access the members of FEL in MEL object....?

      The confusion is going on, really.
      Do you want to access inherited methods from FEL in MEL? If so then it is done in exactly same way you call methods defined in MEL.
      If you want to call methods of an FEL instance in MEL then of course you need to have access to that instance.
      You could, for example, pass a pointer:

      class main : public QObject
      {
      FEL * fel;
      MEL * mel;
      
      public:
          main()
          {
              fel = new FEL;
              mel = new MEL(fel);
          }
      }
      

      Of course you will need to adjust MEL constructor to take a pointer to FEL.

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

      1 Reply Last reply
      3
      • N nikhil30
        4 Jun 2020, 14:39

        Hi,
        I have created derived class (MEL) from custom class (FEL)(this is derived from QObject) and trying to access protected member in derived class. But if i read the protected member, its not showing the updated value in derived class MEL.
        It shows the value assigned in constructor of base custom class FEL.

        Below is my code snippet :-

        class FEL : public QObject
        {
           QObject
        
           protected : //so that it can be accessed in child class
           bool flag;
            
            void foo();
            ...
        }
        
        void foo() updates the flag.
         
        class MEL : public FEL
        {
             QObject
             
             void myfunc();
        }
        
        void MEL ::myfunc()
        {
             bool a = flag; // here not getting flag value assigned in myfunc() of FEL class.
        }
        

        Please suggest how to use base class members (FEL) in derived class (MEL) in Qt...?

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 4 Jun 2020, 14:47 last edited by jsulm 6 Apr 2020, 14:47
        #2

        @nikhil30 said in How to create class derived from custom class and access protected members in Qt:

        here not getting flag value assigned in myfunc() of FEL class

        I don't get it: you also wrote "void foo() updates the flag.". So, what is it now?
        And I don't see myfunc() in FEL...

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

        1 Reply Last reply
        2
        • N Offline
          N Offline
          nikhil30
          wrote on 4 Jun 2020, 15:18 last edited by nikhil30 6 Apr 2020, 15:24
          #3

          My mistake.

          Actually below is my main class :- which has MEL & FEL type objects as its members.

          class main : public QObject
          {
          FEL * fel;
          MEL * mel;
          }

          Now the query is how can i access the members of FEL in MEL object....?
          Can i pass the object of FEL to MEL type object in Qt..?

          P J J 3 Replies Last reply 4 Jun 2020, 15:42
          0
          • N nikhil30
            4 Jun 2020, 15:18

            My mistake.

            Actually below is my main class :- which has MEL & FEL type objects as its members.

            class main : public QObject
            {
            FEL * fel;
            MEL * mel;
            }

            Now the query is how can i access the members of FEL in MEL object....?
            Can i pass the object of FEL to MEL type object in Qt..?

            P Offline
            P Offline
            Pl45m4
            wrote on 4 Jun 2020, 15:42 last edited by Pl45m4 6 Apr 2020, 15:52
            #4

            @nikhil30

            I dont know what you are trying to do :)
            Have a look at this, since it's more a C++ class inheritance issue.
            https://www.geeksforgeeks.org/inheritance-in-c/amp/

            What should FEL and MEL do? Consider making FEL a virtual base class (or even abstract base class) and let MEL derive from it, but all this depends on what you want to do with your classes later...

            You can do

            FEL *foo = new MEL();
            

            to handle them as FEL instances (used when having multiple classes that are derived from FEL and you want to store all objects in one list or vector, for example).
            (If this was your 2nd question)


            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
            1
            • N nikhil30
              4 Jun 2020, 15:18

              My mistake.

              Actually below is my main class :- which has MEL & FEL type objects as its members.

              class main : public QObject
              {
              FEL * fel;
              MEL * mel;
              }

              Now the query is how can i access the members of FEL in MEL object....?
              Can i pass the object of FEL to MEL type object in Qt..?

              J Offline
              J Offline
              JonB
              wrote on 4 Jun 2020, 15:43 last edited by JonB 6 Apr 2020, 15:44
              #5

              @nikhil30
              If you have separate objects/instances, FEL & MEL or FEL * & MEL * pointing to separate objects, they have nothing to do with one another. protected will not be relevant, one way or the other. This may explain your confusion.

              Please suggest how to use base class members (FEL) in derived class (MEL) in Qt...?

              Qt is a framework/library, which happens to be written in C++. It's not a language, and it has nothing to say about protected or anything similar which is to do with the C++ language.

              1 Reply Last reply
              4
              • N nikhil30
                4 Jun 2020, 15:18

                My mistake.

                Actually below is my main class :- which has MEL & FEL type objects as its members.

                class main : public QObject
                {
                FEL * fel;
                MEL * mel;
                }

                Now the query is how can i access the members of FEL in MEL object....?
                Can i pass the object of FEL to MEL type object in Qt..?

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 5 Jun 2020, 04:45 last edited by
                #6

                @nikhil30 said in How to create class derived from custom class and access protected members in Qt:

                Now the query is how can i access the members of FEL in MEL object....?

                The confusion is going on, really.
                Do you want to access inherited methods from FEL in MEL? If so then it is done in exactly same way you call methods defined in MEL.
                If you want to call methods of an FEL instance in MEL then of course you need to have access to that instance.
                You could, for example, pass a pointer:

                class main : public QObject
                {
                FEL * fel;
                MEL * mel;
                
                public:
                    main()
                    {
                        fel = new FEL;
                        mel = new MEL(fel);
                    }
                }
                

                Of course you will need to adjust MEL constructor to take a pointer to FEL.

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

                1 Reply Last reply
                3

                1/6

                4 Jun 2020, 14:39

                • Login

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