How do I access private variable of an object ?
-
I have limited access to the object , but I can retrieve data of interest - in this case dimensions of one of the GUI objects.
Form_Widget *FW = new Form_Widget(); // object
QRect dimensions = FW->geometry(); // returns dimensions of specific part of the object
int start_X = dimensions.x2; //can't do, QRect / dimensions are private -
I have limited access to the object , but I can retrieve data of interest - in this case dimensions of one of the GUI objects.
Form_Widget *FW = new Form_Widget(); // object
QRect dimensions = FW->geometry(); // returns dimensions of specific part of the object
int start_X = dimensions.x2; //can't do, QRect / dimensions are privateTry
dimensions.height()
ordimensions.width()
Mircea
-
Try
dimensions.height()
ordimensions.width()
Mircea
-
Well, that works but does not answer the question . I guess height and width are QRect public variables derived from "private" data. QRect doc should confirm that. I think I need to read-up on "friend" - that semms to be the real OOP ticket.
Member 14968771 wrote:
I think I need to read-up on "friend" - that semms to be the real OOP ticket
On the contrary: that might take you into a non-OOP design. The idea of using public member functions is to make your code independent of internal implementation details of other classes. In your case, a class representing a rectangle might keep a corner of the rectangle and its dimensions. Another one might keep two opposite corners or some other combination. If you try to access private members of the class your code will need to change when or if the implementation of the rectangle changes. If you stick to using only public interface, the internal details of the rectangle class may change but your code will remain the same. You cannot "buy" your way inside a class using a friend declaration. Just like in real life, the friendship is not a symmetrical relation. If you put a
friend QRect
declaration inside your class that doesn't give you any special access rights. It gives access rights toQRect
to your class but obviouslyQRect
is not going to use those rights. You would need to modify the QRect header file to insert a friend declaration mentioning your class. This is very intrusive and basically defeats the purpose of OOP design. I cannot make an OOP design course here, but there are some pretty good references available.Mircea
-
Well, that works but does not answer the question . I guess height and width are QRect public variables derived from "private" data. QRect doc should confirm that. I think I need to read-up on "friend" - that semms to be the real OOP ticket.
Member 14968771 wrote:
I guess height and width are QRect public variables derived from "private" data.
No, they are public methods that give you read-only access to private members.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
I have limited access to the object , but I can retrieve data of interest - in this case dimensions of one of the GUI objects.
Form_Widget *FW = new Form_Widget(); // object
QRect dimensions = FW->geometry(); // returns dimensions of specific part of the object
int start_X = dimensions.x2; //can't do, QRect / dimensions are private -
I have limited access to the object , but I can retrieve data of interest - in this case dimensions of one of the GUI objects.
Form_Widget *FW = new Form_Widget(); // object
QRect dimensions = FW->geometry(); // returns dimensions of specific part of the object
int start_X = dimensions.x2; //can't do, QRect / dimensions are privateOne of the steps I invariably do, when debugging, is comment-out private blocks ... essentially rendering all statements public. Now that I get typing ... and that's just for starters. Without the essential error code (warning?) accompanying a successful compilation/link/running blank form ... there's little more time expendable for me, especially if you're already running debugger and just don't care to get descriptive enough with words and include a code. So, I'll just cut to the chase.
-
I have limited access to the object , but I can retrieve data of interest - in this case dimensions of one of the GUI objects.
Form_Widget *FW = new Form_Widget(); // object
QRect dimensions = FW->geometry(); // returns dimensions of specific part of the object
int start_X = dimensions.x2; //can't do, QRect / dimensions are private -
Sorry, I posted this is wrong forum. But to the comedians - do not quit you day job. In the mean time I'll looking at usage of "friend".
Member 14968771 wrote:
In the mean time I'll looking at usage of "friend".
Don't waste your time, since it will not make any difference. You cannot declare your class to be a friend of some other class in the hope of accessing its private variables. If that was possible then the millions of applications would be open to hacking. Do what I suggested above and make use of the information provided in the documentation: that is what it is there for..
-
Member 14968771 wrote:
I guess height and width are QRect public variables derived from "private" data.
No, they are public methods that give you read-only access to private members.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Sorry, I posted this is wrong forum. But to the comedians - do not quit you day job. In the mean time I'll looking at usage of "friend".
It was serious, actually. If you the designer of the class made a member
private
then you have two ways to access it: the publicly exposed interface (for instance theget/set
methods) or a dirty hack on its bare representation in memory. Typically you cannot usefriend
because you are not the designer of the class."In testa che avete, Signor di Ceprano?" -- Rigoletto
-
I have limited access to the object , but I can retrieve data of interest - in this case dimensions of one of the GUI objects.
Form_Widget *FW = new Form_Widget(); // object
QRect dimensions = FW->geometry(); // returns dimensions of specific part of the object
int start_X = dimensions.x2; //can't do, QRect / dimensions are privatePrivate members are only accessible via public functions. Try int start_X = QPoint.SetX(dimentions.x2);