Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > clarification - calling virtual function from destructor

Reply
Thread Tools

clarification - calling virtual function from destructor

 
 
james
Guest
Posts: n/a
 
      02-05-2004
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;
}


 
Reply With Quote
 
 
 
 
james
Guest
Posts: n/a
 
      02-05-2004
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" <(E-Mail Removed)> 在郵件
news:c0017c$odl$(E-Mail Removed) 中撰寫...
> 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;
> }
>
>



 
Reply With Quote
 
 
 
 
james
Guest
Posts: n/a
 
      02-05-2004
I talked about virtual function, so it is not serious for formal porgram,
anyway thanks a lot
"Sharad Kala" <(E-Mail Removed)> 在郵件
news:c002gs$10kj6e$(E-Mail Removed)-berlin.de 中撰寫...
>
> "james" <(E-Mail Removed)> wrote in message
> news:c0017c$odl$(E-Mail Removed)...
> > 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.
>
>



 
Reply With Quote
 
james
Guest
Posts: n/a
 
      02-05-2004
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.


 
Reply With Quote
 
lallous
Guest
Posts: n/a
 
      02-06-2004
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


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-06-2004
On Fri, 6 Feb 2004 12:16:46 +0200, "lallous" <(E-Mail Removed)> 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.

 
Reply With Quote
 
lallous
Guest
Posts: n/a
 
      02-06-2004
"Alf P. Steinbach" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Fri, 6 Feb 2004 12:16:46 +0200, "lallous" <(E-Mail Removed)> 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


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-06-2004
On Fri, 6 Feb 2004 12:47:13 +0200, "lallous" <(E-Mail Removed)> wrote:

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


See the section on constructors.

 
Reply With Quote
 
lilburne
Guest
Posts: n/a
 
      02-06-2004


lallous wrote:
> "Alf P. Steinbach" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>
>>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.

 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      02-06-2004

"james" <(E-Mail Removed)> wrote in message
news:c0017c$odl$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need clarification for virtual method and pure virtual function a C++ 7 06-28-2007 12:38 PM
calling virtual function results in calling function of base class... Andreas Lagemann C++ 8 01-10-2005 11:03 PM
calling virtual function results in calling function of base class ... tiwy C++ 0 01-09-2005 11:17 PM
Virtual destructor for virtual base class? Chunhui Han C++ 2 06-24-2004 10:13 AM
virtual destructor Vs virtual method Calvin Lai C++ 7 12-18-2003 03:11 AM



Advertisments