473,378 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

clarification - calling virtual function from destructor

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
Jul 22 '05 #1
11 3304
On Fri, 6 Feb 2004 12:16:46 +0200, "lallous" <la*****@lgwm.org> 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.

Jul 22 '05 #2
"Alf P. Steinbach" <al***@start.no> wrote in message
news:40****************@News.CIS.DFN.DE...
On Fri, 6 Feb 2004 12:16:46 +0200, "lallous" <la*****@lgwm.org> 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
Jul 22 '05 #3
On Fri, 6 Feb 2004 12:47:13 +0200, "lallous" <la*****@lgwm.org> wrote:
I read C++ Lite FAQ but didn't know how to solve.


See the section on constructors.

Jul 22 '05 #4


lallous wrote:
"Alf P. Steinbach" <al***@start.no> wrote in message
news:40****************@News.CIS.DFN.DE...

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.

Jul 22 '05 #5
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;
}
Jul 22 '05 #6

"james" <jj****@dcs.warwick.ac.uk> wrote in message
news:c0**********@mail.dcs.warwick.ac.uk...
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.
Jul 22 '05 #7
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

Jul 22 '05 #8
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" <jj****@dcs.warwick.ac.uk> ¦b¶l¥ó
news:c0**********@mail.dcs.warwick.ac.uk ¤¤¼¶¼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;
}

Jul 22 '05 #9
I talked about virtual function, so it is not serious for formal porgram,
anyway thanks a lot
"Sharad Kala" <no*****************@yahoo.com> ¦b¶l¥ó
news:c0*************@ID-221354.news.uni-berlin.de ¤¤¼¶¼g...

"james" <jj****@dcs.warwick.ac.uk> wrote in message
news:c0**********@mail.dcs.warwick.ac.uk...
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.

Jul 22 '05 #10
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.
Jul 22 '05 #11
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.
Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: BigMan | last post by:
Is it safe to call nonvirtual member functions from ctors and dtors? What about virtual ones?
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
10
by: mark | last post by:
I have this class: class Selections { OSStatus Init(); protected: CFMutableSetRef selectionObjects; static void CFASelectionsApplier(const void* value, void* ctx); OSType ready; public:...
3
by: marcwentink | last post by:
Say I have a class A, and a class B that inherits from A. Now A (and B) has a virtual destructor and a virtual function F(); If I now make these statements A* ptrA = new B; ptrA->F(); delete...
6
by: Henrik Goldman | last post by:
Hi I've had a problem with gcc mac osx which I think I figured out. I would like to double check with people here to see if my understanding is correct: I have a class A which class B inherit...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
3
by: a | last post by:
Hi, I need clarification for virtual method and pure virtual method. e.g Class Base{ virtual void func(){ ---- } } Class Child : public Base{ void func()
1
by: newbie | last post by:
This is a snippet from C++ FAQs, which I have never done--- when I do such a thing, I would declare function used by constructor ( in this example, init() ) as static. But I do understand that it...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.