Results 1 to 10 of 10

Thread: How to call super class method from sub class

  1. #1
    Join Date
    Oct 2009
    Posts
    35
    Thanks
    12
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11

    Lightbulb How to call super class method from sub class

    Hi to all,

    Please tell me how to call super class methods from subclass ,when iam calling superclass method from subclass me getting error as shown below.

    Qt Code:
    1. In file included from eclogic_lnx.h:8,
    2. /eclogic_lnx.h:8: In file included from eclogic_lnx.h:8,
    3. /eclogic.h:98: error: ISO C++ forbids declaration of ‘eclogic_lnx’ with no type
    4. /eclogic.h:98: error: expected ‘;’ before ‘*’ token
    To copy to clipboard, switch view to plain text mode 

    and method iam calling like this
    Qt Code:
    1. // eclogic_lnx is the subclass
    2. void eclogic_lnx::on_buttonBox_clicked(QAbstractButton* button)
    3. {
    4. if(button)
    5.  
    6. eclogic->createTreewidget(); //eclogic is the super class and createTreewidget is the super class method name
    7. }
    To copy to clipboard, switch view to plain text mode 

    plz tell me how to call and write the code ,
    thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to call super class method from sub class

    Read some C++ tutorials.
    You can't program if you didn't learn/read anything about it.
    It is not in the scope of this forum to teach elementary C++ (OOP) concepts and syntax.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to call super class method from sub class

    are you sure the error is because of calling super class function issue ?
    and I would suggest please read some basic C++ book first. You can easily find answer to your question.

  4. #4
    Join Date
    Oct 2009
    Posts
    35
    Thanks
    12
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11

    Default Re: How to call super class method from sub class

    Quote Originally Posted by aamer4yu View Post
    are you sure the error is because of calling super class function issue ?
    and I would suggest please read some basic C++ book first. You can easily find answer to your question.


    ok thakq, As iam new to this Qt,here some conventions are different ,so getting problem, as i didnt understand relating to that error?

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to call super class method from sub class

    here some conventions are different
    Can you point some ? Qt is a C++ framework. All C++ conventions follow.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to call super class method from sub class

    This has nothing to do with "different" conventions, but with basic C++.
    In your case, calling a mother class function (note: 'mother' here means the class from which you derived, not the a parent object) can be done by ClassName::methodName(). (as long as the method is not private)
    But you didn't even specify if the mother class should be an instance, or the current class just wants to access functionality in the mother class.

    Again, you wont be able to avoid reading the basics first!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to call super class method from sub class

    Given the initial comments posted to the thread, I expected this thread to die, but it has not. I thought that moderator might move the thread to the General thread, but it is still here, so....

    You can directly call a method for a specific class in C++ providing you have visibility to see the class and the method in the class. The example that I provided to the original poster using a direct email is as follows:

    Qt Code:
    1. class LTop
    2. {
    3. public:
    4. virtual void notInMid() const
    5. {
    6. qDebug("Method LTop::notInMode()");
    7. }
    8. virtual void allDo() const
    9. {
    10. qDebug("Method LTop::allDo()");
    11. }
    12. };
    13. class LMid : public LTop
    14. {
    15. typedef LTop Base;
    16. public:
    17. virtual void allDo() const
    18. {
    19. Base::allDo();
    20. qDebug("Method LMid::allDo()");
    21. }
    22. };
    23. class LBottom : public LMid
    24. {
    25. typedef LMid Base;
    26. public:
    27. virtual void notInMid() const
    28. {
    29. //Cannot make this call because it does not exist
    30. //Base::notInMid();
    31. //Only works if there is public inheritance.
    32. LTop::notInMid();
    33. qDebug("Method LBottom::notInMode()");
    34. }
    35. virtual void allDo() const
    36. {
    37. //this->LMid::allDo();
    38. Base::allDo();
    39. qDebug("Method LBottom::allDo()");
    40. }
    41. };
    To copy to clipboard, switch view to plain text mode 

    Notice a few things that allow this to work

    1. Public inheritance is used. Using "class LMid : LTop" rather than "class LMid : public LTop" prevents LBottom from referencing a method in LTop, but still allows LMid to reference LTop.
    2. The methods must have visibility. In this example, I used public:
    3. You cannot call a method directly that does not exist. Consider the method "notInMid". The method is not implemented in LMid. I dislike that in LBottom I cannot simply say "call the next one up in the heirarchy". I am sure that this is because C++ supports multiple inheritance, so it is not obvious what to call unless you explicitly specify it.

    The probably enough for now.

  8. #8
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to call super class method from sub class

    @Pitonyak
    Please dont encourage such questions. I agree with high_flyer post #2 -
    It is not in the scope of this forum to teach elementary C++ (OOP) concepts and syntax.

  9. #9
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to call super class method from sub class

    Celebrate Ferrum

  10. #10
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to call super class method from sub class

    Quote Originally Posted by aamer4yu View Post
    @Pitonyak
    Please dont encourage such questions. I agree with high_flyer post #2 -
    The slightest thought had not even begun to speculate on the merest possibility of crossing my mind... Until after continued dialog on this thread.

Similar Threads

  1. calling paintEvent() of the super class
    By Cruz in forum Newbie
    Replies: 2
    Last Post: 24th March 2021, 20:29
  2. Locate Class/Method from Dll hex address
    By sivrisinek in forum Installation and Deployment
    Replies: 5
    Last Post: 1st October 2009, 16:29
  3. Replies: 3
    Last Post: 16th May 2007, 11:07
  4. Creating object of other class in Run() method
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 15:05
  5. function 'connect' always look super class for slots.
    By Intaek Lim in forum Qt Programming
    Replies: 7
    Last Post: 12th March 2007, 13:38

Tags for this Thread

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.