How to create class derived from custom class and access protected members in Qt
-
wrote on 4 Jun 2020, 14:39 last edited by
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...?
-
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 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.
-
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...?
Lifetime Qt Championwrote on 4 Jun 2020, 14:47 last edited by jsulm 6 Apr 2020, 14:47@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... -
wrote on 4 Jun 2020, 15:18 last edited by nikhil30 6 Apr 2020, 15:24
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..? -
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..?wrote on 4 Jun 2020, 15:42 last edited by Pl45m4 6 Apr 2020, 15:52I 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
andMEL
do? Consider makingFEL
a virtual base class (or even abstract base class) and letMEL
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) -
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..?wrote on 4 Jun 2020, 15:43 last edited by JonB 6 Apr 2020, 15:44@nikhil30
If you have separate objects/instances,FEL
&MEL
orFEL *
&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. -
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 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.
1/6