Auto Calling Derived Fun

This is a discussion on Auto Calling Derived Fun within the C++ Programming forums, part of the General Programming Boards category; Hello, say I have a base class that has a virtual function called Fun(). What do I have to do ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    16

    Auto Calling Derived Fun

    Hello,
    say I have a base class that has a virtual function called Fun(). What do I have to do in my base class to make sure that all the Fun() functions in the derived classes are automatically called? I tried playing around with the 'this' pointer, but it goes into infinite loop. Any suggestions?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Make it a virtual function. That's all there is to it.
    Remember that the derived ones also need Fun functions and that you must call the function via a pointer or reference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    What do you mean by "all the Fun() functions in the derived classes are automatically called"? The Fun() override in the derived classes will be called if the object is actually of that derived class, and if the member function is invoked via a pointer or reference.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    16
    It is virtual.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you have a Derived2 which is based on Derived1 which is based on Base, and each has a "Fun" function in it? To call all levels of Fun, you'd have to implement the call back to the previous one inside the current one, e.g.
    Code:
    class Base
    {
    ...
       public:
    ...
          virtual void Fun() { ... do stuff ... };
    ...
    };
    
    class Derived1
    {
    ...
       public:
    ...
          virtual void Fun() { Base::Fun(); ... do stuff ... };
    ...
    };
    
    class Derived2
    {
    ...
       public:
    ...
          virtual void Fun() { Derived1::Fun(); ... do stuff ... };
    ...
    };
    You don't have to repeat all levels in Derived2, since Base::Fun is automatically called in Derived2.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Quote Originally Posted by Arlenik View Post
    It is virtual.
    You also need to call the function via a pointer or reference to the base class.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    16
    Code:
    class Base{
    protected:
    virtual void Fun();
    
    /* Base::Fun() gets called somewhere.  There is also code to make sure all the Fun() in derived classes are called too. */
    };
    
    class Derived : public Base{
    
    protected:
    void Fun();
    };
    
    int main(){
    
    Derived A1;  // Base::Fun() is called then Derived::Fun() is called.
    
    return 0;
    }
    Okay, I hope that code shows what I want to do. Fun() is called somewhere in the Base class, and it also calls all the Fun() in the derived classes. Fun() doesn't get called in the Derived class.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Did you look at what I posted?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    Let's use a more concrete example:
    Code:
    #include <iostream>
    
    class Base{
    public:
        void Foo()
        {
            Fun();
            // ...
        }
    protected:
        virtual void Fun()
        {
            std::cout << "Base::Fun()" << std::endl;
            // ...
        }
    };
    
    class Derived : public Base
    {
    protected:
        virtual void Fun()
        {
            std::cout << "Derived::Fun()" << std::endl;
            // ...
        }
    };
    
    class FurtherDerived : public Derived
    {
    protected:
        virtual void Fun()
        {
            std::cout << "FurtherDerived::Fun()" << std::endl;
            // ...
        }
    };
    
    int main()
    {
        FurtherDerived x;
        x.Foo();
    }
    Suppose that the functions are implemented as you want them to be. What is the expected output?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    16
    Quote Originally Posted by matsp View Post
    Did you look at what I posted?

    --
    Mats
    Yes, I did, but I guess you didn't read my post. Base::Fun() or Derived::Fun() never get called in the Derived class. So, you're example is the opposite of what I want to do.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's sort of what I expect you'd say. But I must confess that now I do not know what you are asking for. Please post a piece of realistic code, and explain where you are calling some Fun() function, and what you want to have happen.

    --
    mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    To elaborate on what I am getting at:

    If your expected output is:
    Code:
    FurtherDerived::Fun()
    then you do not need to do anything since the code is correct as-is.

    If your expected output is:
    Code:
    Base::Fun()
    Derived::Fun()
    FurtherDerived::Fun()
    then you should apply matsp's solution. If you cannot apply matsp's solution, then you should relook your design, since it would then require the base class to know about all possible derived classes, thus violating the open-closed principle.

    If your expected output is:
    Code:
    Base::Fun()
    FurtherDerived::Fun()
    then you should pull out the Base::Fun() implementation to Base::Foo(), then call Fun() in Base::Foo().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    May 2008
    Posts
    16
    Quote Originally Posted by laserlight View Post
    Let's use a more concrete example:
    Code:
    #include <iostream>
    
    class Base{
    public:
        void Foo()
        {
            Fun();
            // ...
        }
    protected:
        virtual void Fun()
        {
            std::cout << "Base::Fun()" << std::endl;
            // ...
        }
    };
    
    class Derived : public Base
    {
    protected:
        virtual void Fun()
        {
            std::cout << "Derived::Fun()" << std::endl;
            // ...
        }
    };
    
    class FurtherDerived : public Derived
    {
    protected:
        virtual void Fun()
        {
            std::cout << "FurtherDerived::Fun()" << std::endl;
            // ...
        }
    };
    
    int main()
    {
        FurtherDerived x;
        x.Foo();
    }
    Suppose that the functions are implemented as you want them to be. What is the expected output?
    I'm going to ignore 'x.Foo();' cause there is no need for it. The expected output of your could would be:
    Code:
    Base::Fun()
    Derived::Fun()
    FurtherDerived::Fun()
    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.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    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.

Page 1 of 2 12 LastLast
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, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21