×

Discussion Board

Results 1 to 14 of 14
  1. #1
    Regular Contributor
    Join Date
    Apr 2006
    Location
    Romania, Cluj-Napoca
    Posts
    82

    Pure virtual methods...

    Hei!

    I am facing the following problem:
    I have a base class A, from qt framework (actually QMainWindow).
    My B class inherits A, and declares 3 pure virtual methods, lets'say:

    Code:
    class B: public A
    {....
    protected:
     virtual void B_1() = 0;
     virtual void B_2() = 0;
     virtual void B_3() = 0;
    
    public:
     void B()
     {
     B_1(); <- this lines leaves with KERN-EXEC 3
     };
    };
    Class C inherits B, and implements methods:
    Code:
    class C: public B
    {
    public:
     C():
       B()
     {
     }
    protected:
     virtual void B_1(){};
     virtual void B_2(){};
     virtual void B_3(){};
    }
    In main have the following:

    Code:
    C c_instance;
    The marked line leaves with KERN-EXEC 3.
    What would be the reason for this to happen?
    It seems to have something related to virtual methods table...

    BR,
    gr3go
    Last edited by Gr3go; 2010-05-25 at 17:04.

  2. #2
    Regular Contributor
    Join Date
    Apr 2006
    Location
    Romania, Cluj-Napoca
    Posts
    82

    Re: Pure virtual methods...

    Found the reason:
    Never Call Virtual Functions during Construction or Destruction:
    http://www.artima.com/cppsource/nevercall.html

  3. #3
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Pure virtual methods...

    This example demonstrates virtual function call in constructor without any problems
    Code:
    #include <QtGui>
    
    class A {
    protected:
        virtual void pureVirtual() = 0;
    
    public:
        A()
        {
        };
    };
    
    class B: public A {
    protected:
        void pureVirtual() {};
    
    public:
        B() : A()
        {
           pureVirtual();
        }
    };
    
    int main (int argc, char **argv)
    {
        QApplication app(argc, argv);
    
        B b;
    
        QMainWindow win;
        win.show();
    
        return app.exec();
    }

  4. #4
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Pure virtual methods...

    More over, this code doesn't link:
    Code:
    #include <QtGui>
    
    class A {
    protected:
        virtual void pureVirtual() = 0;
    
    public:
        A()
        {
            pureVirtual();
        };
    };
    
    class B: public A {
    protected:
        virtual void pureVirtual() {};
    
    public:
        B() : A()
        {
        }
    };
    
    int main (int argc, char **argv)
    {
        QApplication app(argc, argv);
    
        B b;
    
        QMainWindow win;
        win.show();
    
        return app.exec();
    }
    Code:
    $ make
    g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -o main.o main.cpp
    main.cpp: In constructor ‘A::A()’:
    main.cpp:10: warning: abstract virtual ‘virtual void A::pureVirtual()’ called from constructor
    g++ -Wl,-O1 -o virtual main.o    -L/usr/lib -lQtGui -lQtCore -lpthread 
    main.o: In function `main':
    main.cpp:(.text+0x47): undefined reference to `A::pureVirtual()'
    collect2: ld returned 1 exit status
    make: *** [virtual] Error 1

  5. #5
    Regular Contributor
    Join Date
    Apr 2006
    Location
    Romania, Cluj-Napoca
    Posts
    82

    Re: Pure virtual methods...

    And how many times B:ureVirtual is called?
    I noticed that you made a call to it from A() and B()...

  6. #6
    Regular Contributor
    Join Date
    Apr 2006
    Location
    Romania, Cluj-Napoca
    Posts
    82

    Re: Pure virtual methods...

    Quote Originally Posted by divanov View Post
    More over, this code doesn't link:
    That's what I am talking about.
    My code was linked, I faced the problem at runtime...

  7. #7
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Pure virtual methods...

    Quote Originally Posted by Gr3go View Post
    And how many times B:ureVirtual is called?
    Once. Note that example is very abstract, and it's not possible to get your in intention from it.
    Quote Originally Posted by Gr3go View Post
    I noticed that you made a call to it from A() and B()...
    And?

  8. #8
    Regular Contributor
    Join Date
    Apr 2006
    Location
    Romania, Cluj-Napoca
    Posts
    82

    Re: Pure virtual methods...

    Quote Originally Posted by divanov View Post
    Once. Note that example is very abstract, and it's not possible to get your in intention from it.

    And?
    If you call it twice, I am expecting that it should be executed twice...

  9. #9
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Pure virtual methods...

    Quote Originally Posted by Gr3go View Post
    If you call it twice, I am expecting that it should be executed twice...
    I call it once.

  10. #10
    Regular Contributor
    Join Date
    Apr 2006
    Location
    Romania, Cluj-Napoca
    Posts
    82

    Re: Pure virtual methods...

    I call it once.
    Yep, my fault.

    Ok, but that code was linking, but I've problem was on runtime, in my case.

  11. #11
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Pure virtual methods...

    Quote Originally Posted by Gr3go View Post
    Yep, my fault.

    Ok, but that code was linking, but I've problem was on runtime, in my case.
    Could you please try my sample application with your compiler?

  12. #12
    Regular Contributor
    Join Date
    Apr 2006
    Location
    Romania, Cluj-Napoca
    Posts
    82

    Re: Pure virtual methods...

    Ok, I'll try it when I get home, later.
    Right here, I don't have an environment for that operation.

    Thanks,
    Gr3go

  13. #13
    Regular Contributor
    Join Date
    Mar 2008
    Location
    Bangalore, India
    Posts
    68

    Re: Pure virtual methods...

    Hi Gr3go,
    Please make sure you should never call virtual functions during construction or destruction. Please follow the links below.

    http://www.artima.com/cppsource/nevercall.html
    http://www.dev102.com/2008/07/08/cal...-constructors/

    The virtual pointer will not be available in the memory when the class constructor is called. I believe the virtual table is constructed only after the class constructor completes. So you should avoid calling virtual function in construtors.

    Pramod
    "Nobody will believe in you unless you believe in yourself."

  14. #14
    Regular Contributor
    Join Date
    Apr 2006
    Location
    Romania, Cluj-Napoca
    Posts
    82

    Re: Pure virtual methods...

    Thanks,

    That was my approach also (after reading the article from artima site)

    Gr3go

Similar Threads

  1. Problem occured when run the project
    By d.devendran in forum Symbian
    Replies: 72
    Last Post: 2009-09-02, 06:15
  2. to get ssid
    By d.devendran in forum Symbian
    Replies: 4
    Last Post: 2009-07-15, 09:42
  3. CPbkContactEditorDlg customize
    By asdfghjkl_asdfghjkl in forum Symbian
    Replies: 5
    Last Post: 2008-08-05, 02:56
  4. Dialogs within Symbian 6.0 based SDK
    By georgica1979 in forum Symbian User Interface
    Replies: 14
    Last Post: 2003-04-07, 15:29
  5. Unresolved external symbols problem
    By georgica1979 in forum Symbian User Interface
    Replies: 1
    Last Post: 2003-04-06, 13:26

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×