结构和类之间的差异 [英] difference between a structure and a class

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

问题描述

我用C ++编程超过4年。我想*我知道

a struct可能有一个构造函数但我今天决定再挖一点

,发现之间差别很小

struct和C ++中的类。两者都有继承,访问修饰符,

等。我看到的唯一差异是默认访问级别是公共与

私有。


我一直只使用结构作为最小类,因为这是教科书中使用它们的方式。


结构具有所有这些纯C的功能?如果没有,那么为什么c ++

会在添加Class类型时更改它们?是否比我列出的更多?b $ b差异?性能差异?我有

总是只使用结构作为最小类,因为这是他们在教科书中使用
的方式。


想法,想法?


~S

I have been programming in C++ for over 4 years. I *think* I knew that
a struct could have a constructor but I decided to dig into it a little
more today, and found that there is very little difference between a
struct and a class in C++. Both have inheritance, access modifiers,
etc. The only diff I see is the default access level is public vs.
private.

I have always just used structs as a minimal class, as that is the way
they were used in textbooks.

Do structs have all these features in pure C? If not then why did c++
change them, when they were adding the Class type? Are there more
differences than those I have listed? Performance differences? I have
always just used structs as a minimal class, as that is the way they
were used in textbooks.

Thoughts, Ideas?

~S

推荐答案



" Shea马丁" < SA ************** @ hotmail.com>在消息中写道

news:h1 ******************** @ ursa-nb00s0.nbnet.nb.ca ...

"Shea Martin" <sa**************@hotmail.com> wrote in message
news:h1********************@ursa-nb00s0.nbnet.nb.ca...
我用C ++编程超过4年。我认为*我知道
一个结构可以有一个构造函数,但我今天决定更多地挖掘它,并发现一个结构和一个结构之间几乎没有什么区别C ++中的类。两者都有继承,访问修饰符,
等。我看到的唯一差异是默认访问级别是public vs.
private。


这确实是唯一的区别。请注意,在''access''中还包括

是默认继承:class有''private'',

和struct有''public''。这就是为什么你会经常看到

派生类声明为:


B级:公众A

我总是只使用结构作为最小类,因为这是在教科书中使用它们的方式。


任何可以用类完成的东西也可以用结构来完成。


结构在纯C中拥有所有这些功能?


No.


如果不是那么为什么c ++
会改变它们,


因为C ++不是C.

当他们添加Class类型时?


''class''不是一种类型。 ''class''(和''struct'')是一个关键字

程序员可以用它来创建自定义(聚合)类型。


是否有比我列出的更多的差异?


No.

性能差异?


这是一个实现细节,不是用

语言来解决的。请参阅实施质量。

我总是只使用结构作为最小类,因为这是它们在教科书中使用的方式。


从''struct''定义创建的对象比从''class''定义创建的对象少



所以''minimal''不适用。

思想,想法?
I have been programming in C++ for over 4 years. I *think* I knew that
a struct could have a constructor but I decided to dig into it a little
more today, and found that there is very little difference between a
struct and a class in C++. Both have inheritance, access modifiers,
etc. The only diff I see is the default access level is public vs.
private.
That is indeed the only difference. Note that also included
in ''access'' is the default inheritance: class has ''private'',
and struct has ''public''. That''s why you''ll typicall see
derived classes declared as:

class B : public A
I have always just used structs as a minimal class, as that is the way
they were used in textbooks.
Anything that can be done with a class can also be
done with a struct.

Do structs have all these features in pure C?
No.

If not then why did c++
change them,
Because C++ is not C.
when they were adding the Class type?
''class'' is not a type. ''class'' (and ''struct'') is a keyword
the programmer can use to create custom (aggregate) types.

Are there more
differences than those I have listed?
No.
Performance differences?
That''s an implementation detail not addresses by the
language. See "Quality of Implementation".
I have
always just used structs as a minimal class, as that is the way they
were used in textbooks.
Objects created from a ''struct'' definition are no
less than those created from a ''class'' definition,
so ''minimal'' does not apply.
Thoughts, Ideas?




因为''机制''是同样,我建议使用

不同的关键词''class''和''struct''

你的意图指标:即当你用特殊的

功能(例如多态)创建抽象类型时,使用''class''

,使用''struct''

如果只是简单地分组相关的简单数据项(例如

a对整数对象)。但最终,

的选择是你的(或者通常是你雇主的选择/

客户)。

-Mike



Since the ''mechanics'' are the same, I suggest using
the different keywords ''class'' and ''struct'' as
indicators of your intentions: I.e. use ''class''
when you''re creating abstract types with special
functionality (e.g. polymorphism), use ''struct''
if simply grouping related simple data items (e.g.
a pair of integer objects). But ultimately, the
choice is yours (or often that of your employer/
client).
-Mike


" Shea Martin" < SA ************** @ hotmail.com>在消息中写道

news:h1 ******************** @ ursa-nb00s0.nbnet.nb.ca
"Shea Martin" <sa**************@hotmail.com> wrote in message
news:h1********************@ursa-nb00s0.nbnet.nb.ca
我用C ++编程超过4年。我认为*我知道
一个结构可以有一个构造函数,但我决定今天再挖一个
,发现结构和结构之间几乎没有区别。 C ++中的类。两者都有继承,访问
修饰符等。我看到的唯一差异是默认访问级别是公共与私有。

我一直只使用结构作为最小这是教科书中使用它们的方式。

结构在纯C中具有所有这些功能吗?


不,他们没有任何这些功能。 C中的结构只是数据类型的容器



如果没有那么为什么c ++在添加Class类型时会改变它们?


我想你不得不问Stroustrup(或读他的书),但你可以

完全按照在C中使用的结构使用结构,或者你可以使用它们添加了

功能。你的选择。是不是更好,只能在C中使用它们作为



是否有比我列出的更多的差异?性能差异?我总是只使用结构作为最小类,因为这是在教科书中使用它们的方式。
I have been programming in C++ for over 4 years. I *think* I knew
that a struct could have a constructor but I decided to dig into it a
little more today, and found that there is very little difference
between a struct and a class in C++. Both have inheritance, access
modifiers, etc. The only diff I see is the default access level is
public vs. private.

I have always just used structs as a minimal class, as that is the way
they were used in textbooks.

Do structs have all these features in pure C?
No. They don''t have any of those features. Structs in C are just containers
for data types.
If not then why did c++ change them, when they were adding the Class type?
I suppose you would have to ask Stroustrup (or read his books), but you can
use structs exactly as they are used in C or you can use them with the added
features. Your choice. Isn''t that better that only being able to use them as
in C?
Are there more
differences than those I have listed? Performance differences? I have
always just used structs as a minimal class, as that is the way they
were used in textbooks.




默认继承,在除了访问,是公共结构。我认为

是关于所有差异的,但也许还有一个。

-

John Carson



Default inheritance, in addition to access, is public with structs. I think
that is about all the differences, but maybe there is one more.
--
John Carson


2005年6月3日星期五16:03:44 +0000,Mike Wahler写道:
On Fri, 03 Jun 2005 16:03:44 +0000, Mike Wahler wrote:
" Shea Martin" < SA ************** @ hotmail.com>在消息中写道
新闻:h1 ******************** @ ursa-nb00s0.nbnet.nb.ca ...
"Shea Martin" <sa**************@hotmail.com> wrote in message
news:h1********************@ursa-nb00s0.nbnet.nb.ca...
我用C ++编程超过4年。我认为*我知道
一个结构可以有一个构造函数,但我今天决定更多地挖掘它,并发现一个结构和一个结构之间几乎没有什么区别C ++中的类。两者都有继承,访问修饰符,
等。我看到的唯一差异是默认访问级别是公共与
私有。
这确实是唯一的区别。请注意,''access'中包含
是默认继承:class具有'private'',
和struct具有'public''。这就是为什么你们通常会看到
派生类声明为:

B类:公共A
I have been programming in C++ for over 4 years. I *think* I knew that
a struct could have a constructor but I decided to dig into it a little
more today, and found that there is very little difference between a
struct and a class in C++. Both have inheritance, access modifiers,
etc. The only diff I see is the default access level is public vs.
private.
That is indeed the only difference. Note that also included
in ''access'' is the default inheritance: class has ''private'',
and struct has ''public''. That''s why you''ll typicall see
derived classes declared as:

class B : public A



[...]由于机制是相同的,我建议使用不同的关键词''class''和'struct'作为你意图的指标:


[...] Since the ''mechanics'' are the same, I suggest using
the different keywords ''class'' and ''struct'' as
indicators of your intentions:



[...]


或者更好的是,为了避免在你的代码中放入大量的公共,你可以使用

当你声明类时,`struct''。许多(大多数?)

的人似乎将公共数据成员/职能放在他们的

类的顶部(并且在底部是私有的);

`class''的默认访问说明符对于这种布局样式很不方便(对于阅读和

的写作)。


B类:公共A,公共N,公共Q< B> {

public:

void f();

};


vs.
void f();

};


-

Jordan DeLong
fr******@allusion.net


[...]

Or better yet, to avoid putting tons of `public'' in your code, you can
just use `struct'' instead when you declare classes. Many (most?)
people seem to put public data members/functions at the top of their
class (and private at the bottom); the default access specifiers for
`class'' are inconvenient for this style of layout (both for reading and
for writing).

class B : public A, public N, public Q<B> {
public:
void f();
};

vs.

struct B : A, N, Q<B> {
void f();
};

--
Jordan DeLong
fr******@allusion.net


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

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