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 | |
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.
|