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

    jsulmJ 1 Reply Last reply
    0
    • N nikhil30

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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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

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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #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 last edited by nikhil30
          #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..?

          Pl45m4P JonBJ jsulmJ 3 Replies Last reply
          0
          • N nikhil30

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

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #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

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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #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

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

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 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

                • Login

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