数据成员 [英] data members

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

问题描述

在私有标签后定义的数据成员无法访问代码

使用该类。

那么,我可以将私有数据成员定义为const吗? />

data members defined after a private label are not accessible to code
that uses the class.
So, can I define the private data members as const?

推荐答案

asdf写道:
asdf wrote:

私有标签后定义的数据成员是无法访问代码

使用该类。

那么,我可以将私有数据成员定义为const吗?
data members defined after a private label are not accessible to code
that uses the class.
So, can I define the private data members as const?



你试过吗?为什么你认为你不能?


-

Ian Collins。

Have you tried it? Why do you think you could not?

--
Ian Collins.


* asdf :
* asdf:

在私有标签无法访问代码后定义的数据成员
使用该类的

data members defined after a private label are not accessible to code
that uses the class.



你似乎大概有正确的想法,但考虑一下:


*如果在私人之后怎么办? "我把一个公开:和一些数据成员

声明?然后他们肯定是/之后/'private:'。


*如果使用该类的代码在

的成员函数中该怎么办?类本身?

You seem to have the right idea, roughly, but consider:

* What if after a "private:" I put a "public:" and some data member
declarations? Then they are certainly /after/ the "private:".

* What if the code that uses the class is in member functions of
the class itself?


那么,我可以将私有数据成员定义为const吗?
So, can I define the private data members as const?



是的。但一般来说,你应该只使用const成员来设计一个不可复制的类(不太精确):对于一个

个实例是不可复制的类按设计)。使用const或引用

成员阻止普通赋值(成员不能更改)但

不复制构造(可以将成员复制到新实例),以及

因为这对所有人来说并不完全直观,所以可能会令人惊讶,因此会导致错误。


所以简单的答案是,为了简单起见,不要使用const或

参考成员。


另一方面,它通常可以很好想要禁用复制,通过声明私有拷贝构造函数和

a私有拷贝赋值运算符,你可以做(​​对于客户端代码)


class Foo

{

private:

Foo(Foo const&); //没有这样的。

Foo& operator =(Foo const&); //没有这样的。

公众:

//随便。

};


禁用复制 - 不可复制的类 - 删除

的问题,例如复制句柄或动态分配的对象拥有每个

实例。


但它有自己的问题,例如使用当前的C ++,C ++ 2003,它将
禁止将临时的实际参数传递给''Foo const&''

正式参数。


-

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

问:为什么这么糟糕?

A:热门发布。

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

Yes. But generally you should only use const members for a class that''s
designed to be non-copyable (less imprecisely: for a class whose
instances are non-copyable by design). Using a const or reference
member prevents ordinary assignment (the member can not be changed) but
not copy construction (the member can be copied to a new instance), and
since that''s not entirely intuitive to all, it can be surprising and
thus lead to bugs.

So the simple answer is, to keep things simple, don''t use const or
reference members.

On the other hand it can often be a good idea to disable copying, which
you can do (for client code) by declaring a private copy constructor and
a private copy assignment operator, like

class Foo
{
private:
Foo( Foo const& ); // No such.
Foo& operator=( Foo const& ); // No such.
public:
// Whatever.
};

Disabling copying -- non-copyable class -- removes problems with
e.g. copying handles or dynamically allocated objects "owned" by each
instance.

But it has problems of its own, e.g. with current C++, C++2003, it
prohibits passing a temporary as actual argument to a ''Foo const&''
formal argument.

--
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?


我明白了,但我的想法很简单,我可以定义:


class Foo

{

const int x;

公开:

Foo();

};





Alf P. Steinbach写道:
I see, but my idea is quite simple, may I define:

class Foo
{
const int x;
public:
Foo();
};

?

Alf P. Steinbach wrote:

* asdf:
* asdf:

私有标签后定义的数据成员无法访问使用该类的代码


data members defined after a private label are not accessible to code
that uses the class.



您似乎大概有正确的想法,但请考虑:


*如果在私人之后怎么办? "我把一个公开:和一些数据成员

声明?然后他们肯定是/之后/'private:'。


*如果使用该类的代码在

的成员函数中该怎么办?类本身?


You seem to have the right idea, roughly, but consider:

* What if after a "private:" I put a "public:" and some data member
declarations? Then they are certainly /after/ the "private:".

* What if the code that uses the class is in member functions of
the class itself?


那么,我可以将私有数据成员定义为const吗?
So, can I define the private data members as const?



是的。但一般来说,你应该只使用const成员来设计一个不可复制的类(不太精确):对于一个

个实例是不可复制的类按设计)。使用const或引用

成员阻止普通赋值(成员不能更改)但

不复制构造(可以将成员复制到新实例),以及

因为这对所有人来说并不完全直观,所以可能会令人惊讶,因此会导致错误。


所以简单的答案是,为了简单起见,不要使用const或

参考成员。


另一方面,它通常可以很好想要禁用复制,通过声明私有拷贝构造函数和

a私有拷贝赋值运算符,你可以做(​​对于客户端代码)


class Foo

{

private:

Foo(Foo const&); //没有这样的。

Foo& operator =(Foo const&); //没有这样的。

公众:

//随便。

};


禁用复制 - 不可复制的类 - 删除

的问题,例如复制句柄或动态分配的对象拥有每个

实例。


但它有自己的问题,例如使用当前的C ++,C ++ 2003,它将
禁止将临时的实际参数传递给''Foo const&''

正式参数。


-

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

问:为什么这么糟糕?

A:热门发布。

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


Yes. But generally you should only use const members for a class that''s
designed to be non-copyable (less imprecisely: for a class whose
instances are non-copyable by design). Using a const or reference
member prevents ordinary assignment (the member can not be changed) but
not copy construction (the member can be copied to a new instance), and
since that''s not entirely intuitive to all, it can be surprising and
thus lead to bugs.

So the simple answer is, to keep things simple, don''t use const or
reference members.

On the other hand it can often be a good idea to disable copying, which
you can do (for client code) by declaring a private copy constructor and
a private copy assignment operator, like

class Foo
{
private:
Foo( Foo const& ); // No such.
Foo& operator=( Foo const& ); // No such.
public:
// Whatever.
};

Disabling copying -- non-copyable class -- removes problems with
e.g. copying handles or dynamically allocated objects "owned" by each
instance.

But it has problems of its own, e.g. with current C++, C++2003, it
prohibits passing a temporary as actual argument to a ''Foo const&''
formal argument.

--
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?


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

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