由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Can we define pure virtual function?
相关主题
C++ online Test 一题C++ virtrual destructor
one C++ question?virtual destructor (C++)问题
各位总结一下区别:virtual function and virtual destructor?C++ Q60 calling virtual function in constructor (JPMorgan)
谁给个c++虚函数完全总结啊C++ Q52: (C6)
A design question一般电面C++会问到什么专业问题?
One C++ question为什么虚函数定义了就要实现, 普通函数 只定义不实现是可以的
C++ Q40: virtual destructor (C2)新Qualcomm面经
贡献一道 C++ 题目包子呼唤大牛--问关于C++Destructor的问题 (转载)
相关话题的讨论汇总
话题: virtual话题: pure话题: function话题: destructor话题: class
进入JobHunting版参与讨论
1 (共1页)
c**********e
发帖数: 2007
1
class A {
public:
virtual int foo()=0;
};
A::foo() { return 0; }
That is can we have a function as pure virtual function
and also have defination like above?
Justify your answer. If yes, then tell me one situation
where we use that. If not tell me why not.
t****t
发帖数: 387
2
我知道是有这种情况的。。
但是忘了为什么要这样。。
z*********8
发帖数: 2070
3
我依稀记得只有destructor可以这么做吧

【在 t****t 的大作中提到】
: 我知道是有这种情况的。。
: 但是忘了为什么要这样。。

h*****f
发帖数: 248
4
I think it wants to force the derived class to either "override/implement"
foo() or call A::foo(), so that A::foo() won't be called by accident.
l***i
发帖数: 1309
5
modern compiler should support explicit keyword.
f**********2
发帖数: 2401
6
虚函数是为了实现多态,纯虚函数是为了保证该方法必然实现多态,提供重写方法。实
现纯虚函数岂不是跟多态目的矛盾?从没见过这样例子
m********1
发帖数: 31
7
#include
class Base
{
public:
virtual void Fun()=0;
};
void Base::Fun()
{
cout<<"\n I am in Pure virtual function\n";
}
class Derived : public Base
{
public:
void Fun()
{
cout<<"\n I am in the Derived class";
Base::Fun( );
}
};
int main(int argc, char * const argv[])
{
Derived d;
Base *b = &d;
b->Fun();
return 0;
}
这种情况是可以的
m********1
发帖数: 31
8
"Effective C++" Meyers mentions a reason for a pure virtual function to have
a body: Derived classes that implement this pure virtual function may call
this implementation smwhere in their code. If part of the code of two
different derived classes is similar then it makes sense to move it up in
the hierarchy, even if the function should be pure virtual.
ISO C++ 12.4 (7):
A destructor can be declared virtual (10.3) or pure virtual (10.4); if any
objects of that class or any derived class are created in the program, the
destructor shall be defined. If a class has a base class with a virtual
destructor, its destructor (whether user or implicitly declared) is virtual.
ISO C++ 10.4 (2):
A pure virtual function need be defined only if explicitly called with the
qualified id syntax (5.1).
c**********e
发帖数: 2007
9
On Item 7, page 43 of "Effective C++", Meyers mentioned a situation you want
to use a pure virtual destructor, that is when you want a class to be
abstract but you do not have a virtual function. You can make the destructor
pure virtual.
Meyers claimed: you must provide a definition for the pure virtual
destructor!
Why? simple. The destructor of base class will be called.

have
call
virtual.

【在 m********1 的大作中提到】
: "Effective C++" Meyers mentions a reason for a pure virtual function to have
: a body: Derived classes that implement this pure virtual function may call
: this implementation smwhere in their code. If part of the code of two
: different derived classes is similar then it makes sense to move it up in
: the hierarchy, even if the function should be pure virtual.
: ISO C++ 12.4 (7):
: A destructor can be declared virtual (10.3) or pure virtual (10.4); if any
: objects of that class or any derived class are created in the program, the
: destructor shall be defined. If a class has a base class with a virtual
: destructor, its destructor (whether user or implicitly declared) is virtual.

B******5
发帖数: 4676
10
话说destructor可以pure吗?
q****x
发帖数: 7404
11
yes, you can. the derived class can directly reuse the definition.

【在 c**********e 的大作中提到】
: class A {
: public:
: virtual int foo()=0;
: };
: A::foo() { return 0; }
: That is can we have a function as pure virtual function
: and also have defination like above?
: Justify your answer. If yes, then tell me one situation
: where we use that. If not tell me why not.

1 (共1页)
进入JobHunting版参与讨论
相关主题
包子呼唤大牛--问关于C++Destructor的问题 (转载)A design question
c++ class default functions?One C++ question
请教一个C++问题C++ Q40: virtual destructor (C2)
C++ Question贡献一道 C++ 题目
C++ online Test 一题C++ virtrual destructor
one C++ question?virtual destructor (C++)问题
各位总结一下区别:virtual function and virtual destructor?C++ Q60 calling virtual function in constructor (JPMorgan)
谁给个c++虚函数完全总结啊C++ Q52: (C6)
相关话题的讨论汇总
话题: virtual话题: pure话题: function话题: destructor话题: class