关于虚函数和抽象类的问题 [英] questions about virtual functions and abstract classes

查看:68
本文介绍了关于虚函数和抽象类的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


如果我的课程有虚拟但非纯粹的声明,例如


A级{

虚拟空虚f();

};


那么A还是一个抽象类吗?我是否必须拥有虚拟空白

f()= 0;代替?我认为将函数声明为= 0。是不是给定了它的定义,这是对的吗?


另外,如果我有一个抽象类A的虚拟析构函数,

然后为什么我还要定义它?例如,如果我有b

$ B虚拟~B();

虚拟空虚f()= 0 ;

};


然后我的编译器仍然要求我定义~B()。但是,我永远不会创建一个B类型的对象。


另外,我不认为我可以声明虚拟构造函数,但它是

似乎我不能做一个纯粹的构造函数,比如

class C {

C()= 0;

virtual void f()= 0;

};


再次,如果我从未创建C类型的对象,为什么我不能允许

声明构造函数是纯的?


此外,是否有任何函数或运算符不能虚拟,

就像构造函数一样?


谢谢!

Jess

解决方案

Jess写道:


如果我有一个具有虚拟但非纯粹声明的类,比如


class一个{

虚拟空虚f();

};


那么A还是一个抽象类吗?



不,它不是。只有至少有一个虚拟函数的类是纯粹的(无论是自己的还是继承的)是抽象的。


我是否必须有虚拟空白

f()= 0;代替?我认为将函数声明为= 0。因为没有给出定义,所以是相同的

,这是对的吗?



不,这不对。纯函数不必有定义,

但它可能。一个非纯虚函数_must_被定义。


另外,如果我有一个抽象类A的虚拟析构函数,

那么为什么我还要定义它?



因为标准语言要求你这样做。


例如,如果我有


B级{

虚拟~B();

虚拟空虚f()= 0;

} ;


然后我的编译器仍然要求我定义~B()。但是,我永远不会
创建一个B类对象!



你永远不会创建_stand-alone_。您仍然可以将其创建为派生类的子元素。


此外,我不认为我可以声明虚拟构造函数,但它好像b $ b似乎我不能做一个纯粹的构造函数,比如

class C {

C()= 0;

virtual void f()= 0;

};



纯说明符仅适用于虚函数。


再次,如果我从不制作对象键入C,为什么我不被允许

声明构造函数是纯的?



因为只有一个虚函数可以声明为纯。


此外,是否有任何函数或运算符这不可能是虚拟的,

就像构造函数一样?



静态成员函数不能是虚拟的。由于''operator new''

和''operator delete''是隐式静态的,它们不能是虚拟的。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


非常感谢!


6月19日晚上11点02分,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:


Jess写道:


如果我有一个具有虚拟但非纯粹声明的类,如


class A {

virtual void f();

};


那么A还是一个抽象类吗?



不,它不是。只有至少有一个虚拟函数的类是

声明为纯(无论是自己的还是继承的)是抽象的。



纯函数可以是非虚函数吗?我尝试了一个例子和我的编译器

说不。


我是否必须拥有虚拟 void

f()= 0;"代替?我认为将函数声明为= 0。因为没有给出定义,所以是相同的

,这是对的吗?



不,这不对。纯函数不必有定义,

但它可能。定义非纯虚函数_must_。



我明白了,所以我可以在一个类中声明一个纯虚函数并稍后定义

,如


A级{

虚拟空虚f()= 0;

};


无效A: :f(){...}


另外,如果我有一个抽象类A的虚拟析构函数,

然后为什么我还要定义它?



因为标准语言要求你这样做。


例如,如果我有


class B {

virtual~B();

virtual void f()= 0;

};


然后我的编译器仍然要求我定义~B()。但是,我永远不会
创建一个B类对象!



你永远不会创建_stand-alone_。您仍然可以将其创建为派生类的子元素。


此外,我不认为我可以声明虚拟构造函数,但它好像b $ b似乎我不能制作像
这样的纯构造函数


class C {

C()= 0;

virtual void f()= 0;

};



纯说明符仅适用于虚函数。


再次,如果我从不制作对象键入C,为什么我不被允许

声明构造函数是纯的?



因为只有一个虚函数可以声明为纯。


此外,是否有任何函数或运算符这不可能是虚拟的,

就像构造函数一样?



静态成员函数不能是虚拟的。由于''operator new''

和''operator delete''是隐式静态的,因此它们不能是虚拟的。



在这种情况下,如果我想要一个静态成员函数来具有多态的行为,我想我需要静态函数接受一个论证

是指向基类对象的指针。因为这将允许

静态函数通过

指针调用适当的虚函数。这是正确的方法吗?


谢谢,

Jess


2007年-06-19 14:32,Jess写道:


你好,


如果我的班级有虚拟但是非纯粹的声明,比如


A级{

虚拟空虚f();

};


那么A还是一个抽象类吗?我是否必须拥有虚拟空白

f()= 0;代替?我认为将函数声明为= 0。因为没有给出定义,所以是相同的

,这是对的吗?



如果你不能创建它的一个实例,那么这个类是抽象的(当它至少有一个成员发生时会发生
)是纯粹的虚拟)。考虑

这些类:


class NotAbstract {

virtual void foo(){/ * ... * /}

虚拟空格条(){/ * ... * /}

};


类摘要{

virtual void foo()= 0;

virtual void bar(){/ * ... * /}

};
< br $>
class PureAbstract {

virtual void foo()= 0;

virtual void bar()= 0;

};


另外,如果我有一个抽象类A的虚拟析构函数,

那么我为什么还要定义它?例如,如果我有b

$ B虚拟~B();

虚拟空虚f()= 0 ;

};


然后我的编译器仍然要求我定义~B()。但是,我永远不会
创建一个B类对象!



因为没有添加= 0,你告诉编译器你将提供析构函数的

定义。


另外,我不认为我可以声明虚拟构造函数,但它好像是b $ b似乎我不能像
那样制作纯粹的构造函数br $>
class C {

C()= 0;

virtual void f()= 0;

};


再说一次,如果我从来没有创建一个C类型的对象,为什么我不被允许

声明构造函数是纯的?

此外,是否有任何函数或运算符不能虚拟,

就像构造函数一样?



如果你永远不打算创建B类对象,你需要什么?
a构造函数?


如果你想创建一个纯粹的抽象类,那么他们通常会看起来像这样:
class Abstract {

virtual void foo()= 0;

virtual int bar(float)= 0;

// ..

virtual~Abstract()= 0;

}


-

Erik Wikstr?m


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;" instead? I think declaring a function as "=0" is the same
as not giving its definition, is this right?

In addition, if I have a virtual destructor for an abstract class A,
then why do I still have to define it? For example, if I have

class B{
virtual ~B();
virtual void f() = 0;
};

Then my compiler still asks me to define ~B(). However, I will never
create an object of type B!

Additionally, I don''t think I can declare virtual constructor, but it
seems I can''t make a pure constructor like

class C{
C() = 0;
virtual void f()=0;
};

Again, if I never make an object of type C, why am I not allowed to
declare the constructor pure?

Moreover, is there any function or operator that can''t be virtual,
just like constructors?

Thanks!
Jess

解决方案

Jess wrote:

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?

No, it''s not. Only classes with at least one virtual function that is
declared pure (whether its own or inherited) are abstract.

Do I have to have "virtual void
f() = 0;" instead? I think declaring a function as "=0" is the same
as not giving its definition, is this right?

No, it''s not right. A pure function does not have to have a definition,
but it may. A non-pure virtual function _must_ be defined.

In addition, if I have a virtual destructor for an abstract class A,
then why do I still have to define it?

Because the language Standard requires you to.

For example, if I have

class B{
virtual ~B();
virtual void f() = 0;
};

Then my compiler still asks me to define ~B(). However, I will never
create an object of type B!

You will never create it _stand-alone_. You still can create it as
a subobject of a derived class.

Additionally, I don''t think I can declare virtual constructor, but it
seems I can''t make a pure constructor like

class C{
C() = 0;
virtual void f()=0;
};

The pure specifiers only apply to virtual functions.

Again, if I never make an object of type C, why am I not allowed to
declare the constructor pure?

Becuase only a virtual function can be declared pure.

Moreover, is there any function or operator that can''t be virtual,
just like constructors?

Static member functions cannot be virtual. Since ''operator new''
and ''operator delete'' are implicitly static, they cannot be virtual.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Thanks a lot!

On Jun 19, 11:02 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

Jess wrote:

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?


No, it''s not. Only classes with at least one virtual function that is
declared pure (whether its own or inherited) are abstract.

Can a pure-function be non-virtual? I tried an example and my compiler
says no.

Do I have to have "virtual void
f() = 0;" instead? I think declaring a function as "=0" is the same
as not giving its definition, is this right?


No, it''s not right. A pure function does not have to have a definition,
but it may. A non-pure virtual function _must_ be defined.

I see, so I can declare a pure virtual function in a class and define
it later as in

class A{
virtual void f() = 0;
};

void A::f(){...}

In addition, if I have a virtual destructor for an abstract class A,
then why do I still have to define it?


Because the language Standard requires you to.

For example, if I have

class B{
virtual ~B();
virtual void f() = 0;
};

Then my compiler still asks me to define ~B(). However, I will never
create an object of type B!


You will never create it _stand-alone_. You still can create it as
a subobject of a derived class.

Additionally, I don''t think I can declare virtual constructor, but it
seems I can''t make a pure constructor like

class C{
C() = 0;
virtual void f()=0;
};


The pure specifiers only apply to virtual functions.

Again, if I never make an object of type C, why am I not allowed to
declare the constructor pure?


Becuase only a virtual function can be declared pure.

Moreover, is there any function or operator that can''t be virtual,
just like constructors?


Static member functions cannot be virtual. Since ''operator new''
and ''operator delete'' are implicitly static, they cannot be virtual.

In that case, if I''d like a static member function to have polymorphic
behaviour, I think I need the static function to take an argument that
is a pointer to the base class object. Since this would allow the
static function to invoke an appropriate virtual function through the
pointer. Is this a correct approach?

Thanks,
Jess


On 2007-06-19 14:32, Jess wrote:

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;" instead? I think declaring a function as "=0" is the same
as not giving its definition, is this right?

A class is abstract if you can not create an instance of it (which
happens when at least one of it''s members are pure virtual). Consider
these the classes:

class NotAbstract {
virtual void foo() { /* ... */ }
virtual void bar() { /* ... */ }
};

class Abstract {
virtual void foo() = 0;
virtual void bar() { /* ... */ }
};

class PureAbstract {
virtual void foo() = 0;
virtual void bar() = 0;
};

In addition, if I have a virtual destructor for an abstract class A,
then why do I still have to define it? For example, if I have

class B{
virtual ~B();
virtual void f() = 0;
};

Then my compiler still asks me to define ~B(). However, I will never
create an object of type B!

Because by not adding =0 you tell the compiler that you will supply a
definition of the destructor.

Additionally, I don''t think I can declare virtual constructor, but it
seems I can''t make a pure constructor like

class C{
C() = 0;
virtual void f()=0;
};

Again, if I never make an object of type C, why am I not allowed to
declare the constructor pure?

Moreover, is there any function or operator that can''t be virtual,
just like constructors?

If you''ll never going to create an object of type B, what would you need
a constructor for?

If you are trying to create a pure abstract class then they generally
look like this:

class Abstract {
virtual void foo() = 0;
virtual int bar(float) = 0;
// ..
virtual ~Abstract() = 0;
}

--
Erik Wikstr?m


这篇关于关于虚函数和抽象类的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆