最后一堂课 [英] final class

查看:66
本文介绍了最后一堂课的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在用c ++搜索最后一堂课,我偶然发现了几个链接

这表明它有构造函数,最后一堂课,因为

私有,所以任何派生类的构造者都无法访问

相同。


class C

{

私人:

C()

{

printf(在C\ n的私人构造函数中);

}

};


E类:公共C

{

public:E()

{

}

};


E obj; //导致编译时错误,说C()不能

访问。


然而这只是使E类抽象而且不是真的从类C继承

继承。事实上,如果我删除了obj的声明,那么就没有任何错误。有没有办法避免首先从

扩展到C?


提前谢谢!!!

解决方案

12月9日上午10:07,Rahul< sam _... @ yahoo.co.inwrote:


大家好,


我正在用c ++搜索最后一堂课,我遇到了几个链接

这意味着要有最终的构造函数类,因为

私有,所以任何派生类的构造函数都不能访问

相同。


类C $ / $
{

私人:

C()

{

printf( 在C\ n的私人建设者中;

}


};


等级E :公共C

{

public:E()

{

}

};


E obj; //导致编译时错误,说C()不能

访问。


然而这只是使E类抽象而且不是真的从类C继承

继承。事实上,如果我删除了obj的声明,那么就没有任何错误。有没有办法避免首先从

延伸到C?



这种继承的用途是什么?继续继承/ multiple-

继承C类,会有什么用处吗?永远?


12月9日上午10:56,Abhishek Padmanabh< abhishek.padman ... @ gmail.com>

写道:


12月9日上午10:07,Rahul< sam _... @ yahoo.co.inwrote:


大家好,


我正在用c ++搜索最后一堂课,我遇到了几个链接

表示将最终类的构造函数设为

private,以便任何派生类的构造函数都无法访问

相同。


class C

{

private:

C ()

{

printf(在C\ n的私人构造函数中);

}


};


class E:public C

{

public:E()

{

}


};


E obj; //导致编译时错误,说C()不能访问


但是这只是使E类抽象而且并没有真正停止从C类继承
继承。事实上,如果我删除了obj的声明,那么就没有任何错误。有没有办法避免首先从

延伸到C?



这种继承的用途是什么?继续继承/ multiple-

继承C类,会有什么用处吗?自从?



是的,在某种程度上无法创建对象。但我期待

编译器

给出一个错误,就像最终使用java一样...


在2007-12-09 07:33,Rahul写道:


12月9日上午10点56分,Abhishek Padmanabh< abhishek.padman ... @ gmail.com>

写道:


> 12月9日上午10:07,Rahul< sam _... @ yahoo .co.inwrote:


大家好,


我正在寻找最后一堂课在c ++中,我遇到了几个链接

,它建议将最终类的构造函数设为

private,以便任何派生类的构造函数无法访问

相同。


class C

{

private:

C ()

{

printf(在C\ n的私人构造函数中);

}


};


class E:public C

{

public:E()

{

}


};


E obj; //导致编译时错误,说C()不能访问


但是这只是使E类抽象而且并没有真正停止从C类继承
继承。事实上,如果我删除了obj的声明,那么就没有任何错误。有没有办法避免首先从

延伸到C?


这种继承的用途是什么?继续继承/多继承C类,会有什么用处吗?自从?



是的,在某种程度上无法创建对象。但我期待

编译器

给出一个错误,就像最终使用java一样... ...



不,你只会在尝试实例化时得到错误。更好

问题,但最终机制有什么用?对我而言,似乎是一个短视的假设,从来没有一个人需要从班级继承的情况。


-

Erik Wikstr ?? m


Hi Everyone,

I was searching for final class in c++, and i came across few links
which suggests to have the constructor of the, to be final class, as
private so that any derived class''s constructors can''t access the
same.

class C
{
private:
C()
{
printf("in private constructor of C\n");
}
};

class E : public C
{
public: E()
{
}
};

E obj; // causes compile time error saying C() can''t be
accessed.

However this just makes class E abstract and doesn''t really stop
inheritance from class C. In fact, there isn''t any error if i remove
the declaration of obj. Is there any way to avoid extending from C in
the first place?

Thanks in advance!!!

解决方案

On Dec 9, 10:07 am, Rahul <sam_...@yahoo.co.inwrote:

Hi Everyone,

I was searching for final class in c++, and i came across few links
which suggests to have the constructor of the, to be final class, as
private so that any derived class''s constructors can''t access the
same.

class C
{
private:
C()
{
printf("in private constructor of C\n");
}

};

class E : public C
{
public: E()
{
}

};

E obj; // causes compile time error saying C() can''t be
accessed.

However this just makes class E abstract and doesn''t really stop
inheritance from class C. In fact, there isn''t any error if i remove
the declaration of obj. Is there any way to avoid extending from C in
the first place?

And what is the use of that inheritance? Keep on inheriting/multiple-
inheriting class C, will anything useful come out of it? Ever?


On Dec 9, 10:56 am, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:

On Dec 9, 10:07 am, Rahul <sam_...@yahoo.co.inwrote:

Hi Everyone,

I was searching for final class in c++, and i came across few links
which suggests to have the constructor of the, to be final class, as
private so that any derived class''s constructors can''t access the
same.

class C
{
private:
C()
{
printf("in private constructor of C\n");
}

};

class E : public C
{
public: E()
{
}

};

E obj; // causes compile time error saying C() can''t be
accessed.

However this just makes class E abstract and doesn''t really stop
inheritance from class C. In fact, there isn''t any error if i remove
the declaration of obj. Is there any way to avoid extending from C in
the first place?


And what is the use of that inheritance? Keep on inheriting/multiple-
inheriting class C, will anything useful come out of it? Ever?

Yes, to an extent an object can''t be created. But i was expecting the
compiler to
give an error just like how final works with java...


On 2007-12-09 07:33, Rahul wrote:

On Dec 9, 10:56 am, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:

>On Dec 9, 10:07 am, Rahul <sam_...@yahoo.co.inwrote:

Hi Everyone,

I was searching for final class in c++, and i came across few links
which suggests to have the constructor of the, to be final class, as
private so that any derived class''s constructors can''t access the
same.

class C
{
private:
C()
{
printf("in private constructor of C\n");
}

};

class E : public C
{
public: E()
{
}

};

E obj; // causes compile time error saying C() can''t be
accessed.

However this just makes class E abstract and doesn''t really stop
inheritance from class C. In fact, there isn''t any error if i remove
the declaration of obj. Is there any way to avoid extending from C in
the first place?


And what is the use of that inheritance? Keep on inheriting/multiple-
inheriting class C, will anything useful come out of it? Ever?


Yes, to an extent an object can''t be created. But i was expecting the
compiler to
give an error just like how final works with java...

No, you will only get the error when you try to instantiate.A better
question though, what is the use of a final mechanism? To me it seems a
bit short-sighted to assume that there will never be a situation where
one would need to inherit from the class.

--
Erik Wikstr??m


这篇关于最后一堂课的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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