Thread: Auto Calling Derived Fun

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The Base class is implemented in a way that the function Fun() in the future derived classes are automatically called without having to call them.
    Then as I have noted, your requirements make this impossible, or rather possible but horribly broken. For example: how many derived classes, both direct and indirect, will Base ever have, and what are their names?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    16
    Here is a real-world example of how this is done, and I need to understand how it's done. If you have used Qt before then this code should make sense.

    Code:
    #include <QApplication>
    #include <QPushButton>
    #include <QPainter>
    #include <QPaintEvent>
    
    class mBut : public QPushButton{
    
     public:
      void paintEvent(QPaintEvent *event){
        QPainter painter(this);
        painter.drawEllipse(2,2,100,25);
      };
    };
    
    int main(int argc, char *argv[]){
    
    	QApplication app(argc, argv);
    	mBut *p = new mBut();
    	
    	p->show();
    	return app.exec();
    }
    If you are on linux with Qt you can compile it with this:
    Code:
    g++ -o main main.cpp `pkg-config --cflags --libs QtGui`
    mBut is my derived class. It inherits QPushButton and implements it's own version of paintEvent(). When my widget is shown, mBut:: paintEvent() is automatically called. mBut is derived from QPushButton, which is derived from QAbstractButton, which is derived from QWidget. paintEvent() is a protected virtual function in QWidget.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling a pure virtual function from a constructor...
    By starcatcher in forum C++ Programming
    Replies: 4
    Last Post: 02-10-2009, 09:12 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 12:13 AM
  4. Calling constructor of the base class of a derived class..
    By CaptainPenguin in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2003, 01:47 PM
  5. Inheiritance and derived classes
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2001, 03:50 PM