当一个纯抽象基类的构造函数被调用时,悖论? [英] paradox when constructor of an pure abstract base class called?

查看:61
本文介绍了当一个纯抽象基类的构造函数被调用时,悖论?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有以下疑问..


class abstractclass

{

public:

abstractclass(){}

virtual void method()= 0;

};


class concreteclass:public abstractclass

{

public:

concreteclass(){}

void方法(){}

};


void main()

{


具体类c;

}


现在当我创建concreteclass的对象时,

的构造函数将调用abstractclass。并且根据逻辑构造函数在创建对象时调用
,abstractclass的对象正在创建。我们怎么能创建一个对象一个抽象类?

或以另一种方式放置是否有任何悖论当

的构造函数被调用时?


谢谢和问候,

Yogesh Joshi

[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

Hello All,

I have following doubt..

class abstractclass
{
public:
abstractclass(){}
virtual void method()=0;
};

class concreteclass:public abstractclass
{
public:
concreteclass(){}
void method(){}
};

void main()
{

concreteclass c;
}

now when I create the object of concreteclass, the constructor of
abstractclass will be called.and as per the logic constructors are
called while creating the object,object of abstractclass is being
created..so how come we can create an object of a abstract class?
or to put in another way is there any paradox when the constructor of
an abstract class gets called?

Thanks and Regards,
Yogesh Joshi
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

推荐答案

yp *********@indiatimes.com 写道:
我有以下疑问......

类抽象类
{
public:
abstractclass(){}
virtual void method()= 0;
};

类具体类:public abstractclass
{
public:
concreteclass(){}
void method(){}
};

void main()


int main()

{

具体类c;
}
现在当我创建concreteclass的对象时,将调用
abstractclass的构造函数。根据逻辑构造函数在创建对象时调用,抽象类的对象正在创建。那么我们怎么能创建一个对象呢?一个抽象类?


我们只能作为另一个对象的子对象。我们不能创建

a _stand-alone_抽象类的对象。

或以另一种方式放置是否有任何悖论当
的构造函数为抽象时class被叫?
I have following doubt..

class abstractclass
{
public:
abstractclass(){}
virtual void method()=0;
};

class concreteclass:public abstractclass
{
public:
concreteclass(){}
void method(){}
};

void main()
int main()
{

concreteclass c;
}

now when I create the object of concreteclass, the constructor of
abstractclass will be called.and as per the logic constructors are
called while creating the object,object of abstractclass is being
created..so how come we can create an object of a abstract class?
We can, only as a subobject of another object. We can''t create
a _stand-alone_ object of abstract class.
or to put in another way is there any paradox when the constructor of
an abstract class gets called?




No.


V



No.

V



yp*********@indiatimes.com 写道:
大家好,

我有以下疑问..

类抽象类
{
公开:
abstractclass() {}
虚拟void方法()= 0;
};

类具体类:public abstractclass
{
public:
concreteclass( ){}
void方法(){}
};

void main()


这应该是int main()顺便说一句。

{

具体类c;
}
现在当我创建concreteclass的对象时,构造函数
abstractclass将被调用。并且根据逻辑构造函数被称为whi在创建对象时,是否正在创建abstractclass的对象。那么为什么我们可以创建一个抽象类的对象?
或者以另一种方式放置是否有任何悖论当

您的前提是,根据定义,创建一个抽象类的

实例是不可能的。这导致您认为在

concreteclass中创建abstractclass实例时,您有一个

逻辑悖论。但是,你的前提是错误的。

从10.4 / 2
Hello All,

I have following doubt..

class abstractclass
{
public:
abstractclass(){}
virtual void method()=0;
};

class concreteclass:public abstractclass
{
public:
concreteclass(){}
void method(){}
};

void main()
This should be int main() by the way.
{

concreteclass c;
}

now when I create the object of concreteclass, the constructor of
abstractclass will be called.and as per the logic constructors are
called while creating the object,object of abstractclass is being
created..so how come we can create an object of a abstract class?
or to put in another way is there any paradox when the constructor of
an abstract class gets called?
Your premise is that it is impossible, by definition, to create an
instance of an abstract class. This leads you think that you have a
logical paradox when an instance of abstractclass is created within
concreteclass. However, your premise is wrong.
From 10.4/2



抽象类是一个只能用作

其他一些班级;除了

作为从它派生的类的子对象之外,不能创建抽象类的任何对象。


无法更清楚。如果你将逻辑应用于一个不正确的前提,那么如果你得出的结论没有意义,你不应该感到惊讶。


Gavin Deane

[见 http://www.gotw。 ca / resources / clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]


An abstract class is a class that can be used only as a base class of
some other class; no objects of an abstract class can be created except
as subobjects of a class derived from it.

Couldn''t be clearer. If you apply logic to an incorrect premise, you
shouldn''t be surprised if the conclusion you reach doesn''t make sense.

Gavin Deane
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


* yp ***** ****@indiatimes.com

class abstractclass
{
public:
abstractclass(){}
virtual void method()= 0;
};

类具体类:public abstractclass
{
public:
concreteclass(){}
void method(){}
};

void main()
{

具体类c;
}


''main''必须有结果类型''int''。


如果您的编译器接受上述内容,那么它是非符合这个

尊重。


现在当我创建concreteclass的对象时,将调用
abstractclass的构造函数。在创建对象时调用逻辑构造函数,正在创建抽象类的对象。那么为什么我们可以创建抽象类的对象呢?
或以另一种方式放置任何p aradox当抽象类的构造函数被调用时?

class abstractclass
{
public:
abstractclass(){}
virtual void method()=0;
};

class concreteclass:public abstractclass
{
public:
concreteclass(){}
void method(){}
};

void main()
{

concreteclass c;
}
''main'' must have result type ''int''.

If your compiler accepts the above, then it''s non-conforming in this
respect.

now when I create the object of concreteclass, the constructor of
abstractclass will be called.and as per the logic constructors are
called while creating the object,object of abstractclass is being
created..so how come we can create an object of a abstract class?
or to put in another way is there any paradox when the constructor of
an abstract class gets called?




C ++禁止你自己实例化一个抽象类的原因

是它没有意义:一个抽象类_depends_在它的纯粹的b $ b虚拟函数上做关键的事情(这就是为什么它是抽象的)。


作为另一个对象的基类部分,由派生类定义的纯虚函数是

,即你有完全不同的

情况,这就是重点,它的使用方式。


然而,有可能会错误地称之为纯粹的虚拟

函数来自抽象类构造函数,通过其他一些成员

函数。结果是未定义的行为,但很可能会检测到
并导致崩溃。它只是意味着静态

(编译时)类型检查和基于此类检查的规则不能防止所有运行时错误,我们知道这些错误。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么会这样一件好事?

A:热门发布。

问:usenet和电子邮件中最烦人的是什么?


[见 http://www.gotw.ca/resources/ clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]



The reason C++ forbids you to instantiate an abstract class on its own
is that it''s not meaningful: an abstract class _depends_ on its pure
virtual functions to do the crucial things (that''s why it''s abstract).

As a base class part of another object those pure virtual functions are
defined, by the derived class, i.e. you have totally different
situation, and that''s the whole point, the way it''s meant to be used.

However, there is a possibility of erronously calling a pure virtual
function from the abstract class constructor, via some other member
function. The result of that is undefined behavior, but most likely it
will be detected and cause a crash. It simply means that static
(compile-time) type checking and rules based on such checking can not
protect against all run-time errors, which we knew anyway.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


这篇关于当一个纯抽象基类的构造函数被调用时,悖论?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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