clarification - calling virtual function from destructor

Discussion in 'C++' started by james, Feb 5, 2004.

  1. james

    james Guest

    Every class is better has own constructor and deconstructor itself, even if
    compiler allow you do that. Class A func1 alway be called In your code. I
    revised your code for you. every class will call func1 itself. then you
    don't need to name"virturl" in class B.
    class A
    {
    public:
    virtual void Func1()
    {
    printf("this is class A");
    };
    void Func1Caller()
    {
    Func1();
    }
    ~A()
    {
    Func1Caller();
    }
    };

    class B : public A
    {
    public:
    B();
    ~B();
    void Func1(){ printf("this is class B\n");}
    B::B() {}
    B::~B(){ Func1Caller();}
    };

    int main()

    {
    B t;
    return 0;
    }
     
    james, Feb 5, 2004
    #1
    1. Advertising

  2. james

    james Guest

    another example is more clear about virtual function
    declareation is as yours
    B mean;
    A meam1;
    B *meam2;
    meam2=&meam1;
    meam2->Func1(); // callB::Func1();
    meam2=&A;
    meam2->Func1(); //call A::Func1();


    "james" <> ¦b¶l¥ó
    news:c0017c$odl$ ¤¤¼¶¼g...
    > Every class is better has own constructor and deconstructor itself, even

    if
    > compiler allow you do that. Class A func1 alway be called In your code. I
    > revised your code for you. every class will call func1 itself. then you
    > don't need to name"virturl" in class B.
    > class A
    > {
    > public:
    > virtual void Func1()
    > {
    > printf("this is class A");
    > };
    > void Func1Caller()
    > {
    > Func1();
    > }
    > ~A()
    > {
    > Func1Caller();
    > }
    > };
    >
    > class B : public A
    > {
    > public:
    > B();
    > ~B();
    > void Func1(){ printf("this is class B\n");}
    > B::B() {}
    > B::~B(){ Func1Caller();}
    > };
    >
    > int main()
    >
    > {
    > B t;
    > return 0;
    > }
    >
    >
     
    james, Feb 5, 2004
    #2
    1. Advertising

  3. james

    james Guest

    I talked about virtual function, so it is not serious for formal porgram,
    anyway thanks a lot
    "Sharad Kala" <> ¦b¶l¥ó
    news:c002gs$10kj6e$-berlin.de ¤¤¼¶¼g...
    >
    > "james" <> wrote in message
    > news:c0017c$odl$...
    > > Every class is better has own constructor and deconstructor itself, even

    if
    > > compiler allow you do that. Class A func1 alway be called In your code.

    I
    > > revised your code for you. every class will call func1 itself. then you
    > > don't need to name"virturl" in class B.
    > > class A
    > > {
    > > public:
    > > virtual void Func1()
    > > {
    > > printf("this is class A");
    > > };
    > > void Func1Caller()
    > > {
    > > Func1();
    > > }
    > > ~A()
    > > {
    > > Func1Caller();
    > > }
    > > };
    > >
    > > class B : public A
    > > {
    > > public:
    > > B();
    > > ~B();
    > > void Func1(){ printf("this is class B\n");}
    > > B::B() {}
    > > B::~B(){ Func1Caller();}
    > > };
    > >
    > > int main()
    > >
    > > {
    > > B t;
    > > return 0;
    > > }

    >
    >
    > The code you posted won't even compile dear friend!
    > As pointed by others calling pure virtual function inside a destructor is
    > calling in for undefined behavior.
    >
    >
     
    james, Feb 5, 2004
    #3
  4. james

    james Guest

    then I agree with Sharard Kala' opion. It is a bed idea to put virtual
    function in destructor. see my another example at previou post.
     
    james, Feb 5, 2004
    #4
  5. james

    lallous Guest

    Hello


    class A

    {

    public:

    virtual void Func1() = 0;

    void Func1Caller()

    {

    Func1();

    }

    ~A()

    {

    Func1Caller();

    }

    };

    class B : public A

    {

    public:

    virtual void Func1()

    {

    printf("this is func1\n");

    }

    };

    int main()

    {

    B t;

    return 0;

    }



    It seems that VC is not allowing Func1Caller() to call Func1() from the
    destructor.
    Don't know if this a C++ issue or a compiler issue.

    Can someone clarify and provide a workaround?

    --
    Elias
     
    lallous, Feb 6, 2004
    #5
  6. On Fri, 6 Feb 2004 12:16:46 +0200, "lallous" <> wrote:

    >class A
    >{
    >public:
    >virtual void Func1() = 0;
    >void Func1Caller()
    >{
    >Func1();
    >}
    >~A()
    >{
    >Func1Caller();
    >}
    >};


    The destructor here has undefined behavior because it's calling
    a pure virtual function.

    With a good C++ compiler this will cause a run-time error, but
    formally it could do anything (including seemingly "working").

    Hint: get your indentation in order before worrying about such
    things, and also see the FAQ, which answers such questions.
     
    Alf P. Steinbach, Feb 6, 2004
    #6
  7. james

    lallous Guest

    "Alf P. Steinbach" <> wrote in message
    news:...
    > On Fri, 6 Feb 2004 12:16:46 +0200, "lallous" <> wrote:
    >
    > >class A
    > >{
    > >public:
    > >virtual void Func1() = 0;
    > >void Func1Caller()
    > >{
    > >Func1();
    > >}
    > >~A()
    > >{
    > >Func1Caller();
    > >}
    > >};

    >


    > The destructor here has undefined behavior because it's calling
    > a pure virtual function.

    But isn't the pure virtual function defined through class B?

    And you cannot create an instance of class A unless its pure virtual method
    is defined.

    >
    > With a good C++ compiler this will cause a run-time error, but
    > formally it could do anything (including seemingly "working").

    It did produce a runtime error.

    > Hint: get your indentation in order before worrying about such
    > things, and also see the FAQ, which answers such questions.

    The code is idented, but the NG composer got it unidented.

    I read C++ Lite FAQ but didn't know how to solve.

    --
    Elias
     
    lallous, Feb 6, 2004
    #7
  8. On Fri, 6 Feb 2004 12:47:13 +0200, "lallous" <> wrote:

    >I read C++ Lite FAQ but didn't know how to solve.


    See the section on constructors.
     
    Alf P. Steinbach, Feb 6, 2004
    #8
  9. james

    lilburne Guest

    lallous wrote:
    > "Alf P. Steinbach" <> wrote in message
    > news:...
    >
    >
    >>The destructor here has undefined behavior because it's calling
    >>a pure virtual function.

    >
    > But isn't the pure virtual function defined through class B?
    >
    > And you cannot create an instance of class A unless its pure virtual method
    > is defined.
    >


    When you enter A's destructor B has already been destroyed, it is no
    more, it is an ex-B.
     
    lilburne, Feb 6, 2004
    #9
  10. james

    Sharad Kala Guest

    "james" <> wrote in message
    news:c0017c$odl$...
    > Every class is better has own constructor and deconstructor itself, even if
    > compiler allow you do that. Class A func1 alway be called In your code. I
    > revised your code for you. every class will call func1 itself. then you
    > don't need to name"virturl" in class B.
    > class A
    > {
    > public:
    > virtual void Func1()
    > {
    > printf("this is class A");
    > };
    > void Func1Caller()
    > {
    > Func1();
    > }
    > ~A()
    > {
    > Func1Caller();
    > }
    > };
    >
    > class B : public A
    > {
    > public:
    > B();
    > ~B();
    > void Func1(){ printf("this is class B\n");}
    > B::B() {}
    > B::~B(){ Func1Caller();}
    > };
    >
    > int main()
    >
    > {
    > B t;
    > return 0;
    > }



    The code you posted won't even compile dear friend!
    As pointed by others calling pure virtual function inside a destructor is
    calling in for undefined behavior.
     
    Sharad Kala, Feb 6, 2004
    #10
  11. On Fri, 06 Feb 2004 12:16:46 +0200, lallous wrote:

    > It seems that VC is not allowing Func1Caller() to call Func1() from the
    > destructor.
    > Don't know if this a C++ issue or a compiler issue.
    >
    > Can someone clarify and provide a workaround?


    It's normal C++ behaviour. At the time of the destructor (the same goes
    for the constrctor) the dynamic type is set to that of the destructor.
    What does that mean?

    class A
    {
    public:
    virtual f1() { puts("A"); }
    };

    class B
    {
    public:
    virtual f1() { puts("B"); }
    ~B()
    {
    f1(); // Prints B, we are a B, we are no longer a C
    A *p = this;
    p->f1(); // Again prints B, virtual functions still work,
    // we are a B, so calls B::f1
    }
    };

    class C
    {
    public:
    virtual f1() { puts("C"); }
    };

    int main();
    {
    C c;
    }

    When C gets destroyed, first the destuctor for C is executed, then the
    destructor for the B subobject *but* with it's type set to B, then the A
    subobject is destroyed, but with it's type set to A.

    Think about it. The C object is already destroyed, it would make a fine
    mess if virtual functions worked the way you assumed they do.

    HTH,
    M4
     
    Martijn Lievaart, Feb 6, 2004
    #11
  12. james

    red floyd Guest

    lilburne wrote:

    >
    > When you enter A's destructor B has already been destroyed, it is no
    > more, it is an ex-B.
    >


    It has ceased to "B"? Of course, it could just be pining for the fjords.
     
    red floyd, Feb 6, 2004
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Calvin Lai
    Replies:
    7
    Views:
    574
    Calvin Lai
    Dec 18, 2003
  2. Chunhui Han
    Replies:
    2
    Views:
    530
  3. Andreas Lagemann
    Replies:
    8
    Views:
    530
    Mike Wahler
    Jan 10, 2005
  4. tiwy
    Replies:
    0
    Views:
    461
  5. a
    Replies:
    7
    Views:
    383
    dasjotre
    Jun 28, 2007
Loading...

Share This Page