Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



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


  • Qt Champions 2019

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


  • Qt Champions 2019

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



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



  • @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)



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


  • Qt Champions 2019

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


Log in to reply