类与结构 [英] class versus struct

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

问题描述

我被告知,类和结构之间的唯一区别是

默认接口 - 类为private,public为struct。我的导师

发表了这样的声明:


实际上类和结构之间只有2个区别:

1)默认界面对于一个类是私有的,而它是一个

结构的公共。

2)除了
之外,struct还支持数组类型的初始化语法构造函数,而类只需要构造函数。


并给出以下代码示例:


//作品

class Point {int x,y; };

点p1 = {4,6};


//编译错误:''p1'':无法使用<初始化非聚合br />
初始化列表

struct Point {int x,y; };

点p1 = {4,6};


这是正确的,这是唯一的两个区别吗?

I have been told that the only difference between a class and a struct is
the default interface - private for class, public for struct. My instructor
made this statement:

Actually there are only 2 differences between classes and structs:
1) The default interface for a class is private whereas it is publie for a
struct.
2) A struct supports array-type initialization syntax in addition to
constructors whereas a class requires constructors only.

And give the following code example:

// Works
class Point { int x, y; };
Point p1 = {4, 6};

// Compile error: ''p1'' : non-aggregates cannot be initialized with
initializer list
struct Point { int x, y; };
Point p1 = {4, 6};

Is this correct, and these are the only 2 differences?

推荐答案

Ook写道:
我被告知,类和结构之间的唯一区别是
默认接口 - 私有class,public for struct。我的导师
发表了这样的声明:

实际上类和结构之间只有两个不同之处:
1)类的默认接口是私有的,而它是一个<结构。
2)除了构造函数之外,struct还支持数组类型初始化语法,而类只需要构造函数。

并给出以下代码示例: br />
//作品
class Point {int x,y;点p1 = {4,6};

//编译错误:''p1'':无法使用
初始化列表初始化非聚合
struct Point {int x,y;点p1 = {4,6};

这是正确的,这是唯一的两个区别吗?
I have been told that the only difference between a class and a struct is
the default interface - private for class, public for struct. My instructor
made this statement:

Actually there are only 2 differences between classes and structs:
1) The default interface for a class is private whereas it is publie for a
struct.
2) A struct supports array-type initialization syntax in addition to
constructors whereas a class requires constructors only.

And give the following code example:

// Works
class Point { int x, y; };
Point p1 = {4, 6};

// Compile error: ''p1'' : non-aggregates cannot be initialized with
initializer list
struct Point { int x, y; };
Point p1 = {4, 6};

Is this correct, and these are the only 2 differences?




。另一个区别是关键字''class''可用于

a模板类型参数声明,作为''typename'的替代品'

和''结构''不能。


这些在FAQ中都没有描述过吗?为什么我们一直在讨论这个问题

什么时候会讨论无数次?你不是吗?
可以访问''groups.google.com''吗?


V



No. Another difference is that the keyword ''class'' can be used in
a template type argument declaration as an alternative to ''typename''
and ''struct'' cannot.

Isn''t all this described in the FAQ? Why do we keep talking about it
when it''s all has be discussed countless number of times? Don''t you
have access to ''groups.google.com''?

V

Victor Bazarov写道:
Victor Bazarov wrote:
< snip>

否。另一个区别是关键字''class''可用于
a模板类型参数声明作为''typename'的替代'
和''struct''不能。

在FAQ中是不是都描述了这一点?为什么我们一直在谈论它什么时候讨论了无数次?你不能访问''groups.google.com''吗?
<snip>

No. Another difference is that the keyword ''class'' can be used in
a template type argument declaration as an alternative to ''typename''
and ''struct'' cannot.

Isn''t all this described in the FAQ? Why do we keep talking about it
when it''s all has be discussed countless number of times? Don''t you
have access to ''groups.google.com''?




除此之外,结构的继承是隐式公开的,而

它是一个类的私有(或受保护的?)。



On top of that, inheritance for a struct is implicitly public, while
it''s private (or protected?) for a class.


>>
这是正确的,这是唯一的两个区别吗?
Is this correct, and these are the only 2 differences?



否。另一个区别是关键字''class''可用于
模板类型参数声明作为''typename'的替代'
和''struct''不能。

在FAQ中是不是都描述了这一点?为什么我们一直在谈论它什么时候讨论了无数次?你不能访问''groups.google.com''吗?

V



No. Another difference is that the keyword ''class'' can be used in
a template type argument declaration as an alternative to ''typename''
and ''struct'' cannot.

Isn''t all this described in the FAQ? Why do we keep talking about it
when it''s all has be discussed countless number of times? Don''t you
have access to ''groups.google.com''?

V




谢谢你你的回应。我们一直在谈论它,因为不是每个人

知道有一个FAQ,或者他们无法在常见问题解答中找到他们问题的答案,或者他们可以'不要回答。



Thank you for your response. We keep talking about it because not everyone
knows there is an FAQ, or they can''t find the answer to their question in
the FAQ, or they can''t google the answer.


这篇关于类与结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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