澄清 - 从析构函数调用虚函数 [英] clarification - calling virtual function from destructor
问题描述
你好
A级
{
公开:
virtual void Func1()= 0;
void Func1Caller()
{
Func1();
}
~A()
{
Func1Caller();
}
};
class B:public A
{
public:
virtual void Func1 ()
{
printf(这是func1 \ n);
}
};
int main()
{
B t;
返回0;
}
似乎VC不允许Func1Caller()从
析构函数中调用Func1()。
不知道这是C ++问题还是编译器问题。
有人可以澄清并提供解决方法吗?
-
Elias
2004年2月6日星期五12:16:46 + 0200,lallous < LA ***** @ lgwm.org>写道:
A类
公开:
virtual void Func1()= 0;
void Func1Caller()
{
Func1();
}
~A()
{
Func1Caller();
}
};
这里的析构函数有未定义的行为,因为它正在调用
a纯虚函数。
随着一个好的C ++编译器,这会导致运行时错误,但
正式它可以做任何事情(包括看似工作)。
提示:在担心这些问题之前先按顺序进行缩进,并查看常见问题解答,以回答此类问题。
" ; Alf P. Steinbach <人*** @ start.no>在消息中写道
news:40 **************** @ News.CIS.DFN.DE ...On周五,2004年2月6日12:16:46 +0200,lallous < LA ***** @ lgwm.org>写道:
A类
公开:
virtual void Func1()= 0;
void Func1Caller()
{
Func1();
}
~A()
{
Func1Caller();
}
};
这里的析构函数具有未定义的行为,因为它正在调用纯粹的虚函数。
但是不是通过B类定义的纯虚函数吗?
你不能创建A类实例,除非它的纯虚方法
是定义的。
使用一个好的C ++编译器会导致运行时错误,但正式它可以做任何事情(包括看似工作)。
它确实产生了运行时错误。
提示:在担心这些事情之前先按顺序缩进,并查看常见问题解答,以回答这些问题。
代码是完美的,但是NG作曲家没有把它搞定。
我读过C ++ Lite常见问题,但不知道如何解决。
-
Elias
2004年2月6日星期五12:47:13 +0200 ,lallous < LA ***** @ lgwm.org>写道:
我读过C ++ Lite常见问题,但不知道如何解决。
请参阅有关构造函数的部分。
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
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.
"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
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.
这篇关于澄清 - 从析构函数调用虚函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!