Results 1 to 4 of 4

Thread: How to access to methods of parent class

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How to access to methods of parent class

    I dont know if what I want have to be done using inheritance or simply writing correct 'friend' keyword.

    I have the next code :
    Qt Code:
    1. ClassA
    2. public :
    3. void methodA
    4. private :
    5. ClassB my_classb
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. ClassB
    2. void methodB
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void ClassB::methodB
    2. {
    3. if (condition)
    4. classB.methodA ();
    5. }
    To copy to clipboard, switch view to plain text mode 

    As you can see I'd want to acces to a methodA from metodB.
    I can do the work using inheritance, but I would not want access to all public members, only to one of them.
    I have see also the friend keyword, but I dont know how to write the correct code.

    I'm sure that this is easy to do, but I dont find the way.
    Any help will be appreciated.

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: How to access to methods of parent class

    If the methodA could and is designed to be used in that way (which would me no reference or dependence to the members of class A), it should be made either 1) a function or 2) a static method of a third utility class.

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to access to methods of parent class

    If you want to use some functions provided by ClassA from ClassB, then you can do it this way

    Qt Code:
    1. ClassB::methodB()
    2. {
    3. ClassA objectA;
    4.  
    5. if(test_condition)
    6. objectA.methodA();
    7. }
    To copy to clipboard, switch view to plain text mode 

    This is sufficient if methodA() is defined in public interface, if methodA() is a private, then declare ClassB as friend of ClassA.

    If you feel by design that ClassB is a kind of ClassA with added functions / features, then make methodA() as protected in ClassA and derive ClassB from ClassA, then no need to declare as friend.

    from your example code, looks like you need have a kind of parent-child / or ownership relationship between ClassA and ClassB then you can use as in this example

    Qt Code:
    1. //ClassA.h
    2. class ClassB;
    3. class ClassA
    4. {
    5. public:
    6. ClassA();
    7. void methodA();
    8. private:
    9. ClassB* memberB;
    10. };
    11.  
    12. //ClassA.cpp
    13. #include "ClassA.h"
    14. #include "ClassB.h"
    15. ClassA::ClassA() : memberB(new ClassB(this))
    16. {
    17. }
    18.  
    19. ClassB::methodA()
    20. {
    21. //do anything, except calling memberB->methodB(), this will cause recurssion
    22. }
    23.  
    24. //ClassB.h
    25. class ClassA;
    26. class ClassB
    27. {
    28. public:
    29. explicit ClassB(ClassA * parent);
    30. methodB();
    31. private:
    32. ClassA* parentA;
    33. }
    34.  
    35. //ClassB.cpp
    36. #include "ClassA.h"
    37. #include "ClassB.h"
    38. ClassB::ClassB(ClassA * parent) : parentA(parent)
    39. {
    40. ;
    41. }
    42.  
    43. ClassB::methosB()
    44. {
    45. parentA->methodA();
    46. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 29th June 2011 at 03:42.

  4. #4
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to access to methods of parent class

    Thanks.
    ClassB(ClassA * parent); can be the solution.

Similar Threads

  1. How to access OCX methods and events
    By joy in forum Qt Programming
    Replies: 4
    Last Post: 1st May 2018, 16:37
  2. Dynamic plugin: accessing parent's methods
    By codeslicer in forum Qt Programming
    Replies: 1
    Last Post: 4th January 2011, 09:16
  3. Replies: 6
    Last Post: 7th December 2010, 12:32
  4. Replies: 4
    Last Post: 29th May 2010, 12:56
  5. cannot access QLineEdit text in other methods of the class...
    By Leoha_Zveri in forum Qt Programming
    Replies: 2
    Last Post: 29th September 2009, 12:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.