const类成员的赋值 [英] assignment of const class members

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

问题描述




看来我不能在A类中分配const变量u,单程来解决问题可能是
构建一个复制构造函数。然而,为什么

C ++或vector类不喜欢这段代码?我的g ++是:gcc版本

4.0.1(Apple Inc. build 5465)。感谢您的帮助。


编译错误摘要:

-------------------- -------------------

cpp.C:4:错误:非静态const成员''const unsigned int A :: u' ',

不能使用默认赋值运算符

/usr/include/c++/4.0.0/bits/vector.tcc:260:警告:合成

方法''​​A&这里首先需要A :: operator =(const A&)'


代码:

-------

1

2 #include< vector>

3

4 struct A {

5 A( const unsigned int a):u(a){}

6 private:const unsigned int u;

7};

8 br />
9 int main(){

10

11 std :: vector< A y;

12 y.push_back (A(2));

13}

完全编译错误:

------------- ------------

[16:34]:Macintosh:tmp 7+ g ++ cpp.C

cpp.C:在成员函数中 A和A :: operator =(const A&)'':

cpp.C:4:从''void std :: vector< _Tp,

_Alloc>实例化: :_M_insert_aux(__ gnu_cxx :: __ normal_iterato r< typename

_Alloc :: pointer,std :: vector< _Tp,_Alloc,const _Tp&)[with _Tp =

A, _Alloc = std :: allocator< A>]''

/usr/include/c++/4.0.0/bits/stl_vector.h:610:从

'实例化'void std :: vector< _Tp,_Alloc> :: push_back(const _Tp&)[with _Tp = A,

_Alloc = std :: allocator< A>]''

cpp.C:12:从这里实例化

cpp.C:4:错误:非静态const成员''const unsigned int A :: u'',

不能使用默认赋值运算符

/usr/include/c++/4.0.0/bits/vector.tcc:在成员函数中''void

std :: vector< _Tp,

_Alloc> :: _ M_insert_aux(__ gnu_cxx :: __ normal_iterato r< typename

_Alloc :: pointer,std :: vector< _Tp,_Alloc, const _Tp&)[w ith _Tp =

A,_Alloc = std :: allocator< A>]'':

/usr/include/c++/4.0.0/bits/vector.tcc :260:警告:合成

方法''​​A&这里首先需要A :: operator =(const A&)'



it seems i can''t assign the const variable u in class A, one way to
solve the problem may be to build a copy constructor. however, why
does C++ or vector class not like this code? my g++ is: gcc version
4.0.1 (Apple Inc. build 5465). thanks for the help.

summary of compile error:
---------------------------------------
cpp.C:4: error: non-static const member ''const unsigned int A::u'',
can''t use default assignment operator
/usr/include/c++/4.0.0/bits/vector.tcc:260: warning: synthesized
method ''A& A::operator=(const A&)'' first required here

code:
-------
1
2 #include <vector>
3
4 struct A {
5 A(const unsigned int a) : u(a) { }
6 private: const unsigned int u;
7 };
8
9 int main () {
10
11 std::vector<A y;
12 y.push_back(A(2));
13 }
full compile error:
-------------------------
[16:34]:Macintosh:tmp 7+ g++ cpp.C
cpp.C: In member function ''A& A::operator=(const A&)'':
cpp.C:4: instantiated from ''void std::vector<_Tp,
_Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterato r<typename
_Alloc::pointer, std::vector<_Tp, _Alloc, const _Tp&) [with _Tp =
A, _Alloc = std::allocator<A>]''
/usr/include/c++/4.0.0/bits/stl_vector.h:610: instantiated from
''void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = A,
_Alloc = std::allocator<A>]''
cpp.C:12: instantiated from here
cpp.C:4: error: non-static const member ''const unsigned int A::u'',
can''t use default assignment operator
/usr/include/c++/4.0.0/bits/vector.tcc: In member function ''void
std::vector<_Tp,
_Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterato r<typename
_Alloc::pointer, std::vector<_Tp, _Alloc, const _Tp&) [with _Tp =
A, _Alloc = std::allocator<A>]'':
/usr/include/c++/4.0.0/bits/vector.tcc:260: warning: synthesized
method ''A& A::operator=(const A&)'' first required here

推荐答案

我发现这段代码编译得很好,唯一的差异是const关键字
变量u中的
。很奇怪。


1 #include< vector>

2

3 struct A {

4 A(const unsigned int a):u(a){}

5 private:unsigned int u;

6};

7

8 int main(){

9

10 std :: vector< A y;

11 y .push_back(A(2));

12}


12月21日,4:38 * pm,hweek ... @ yahoo.com写道:
i found this code compiled fine, the only diff is the "const" keyword
in the variable u. strange.

1 #include <vector>
2
3 struct A {
4 A(const unsigned int a) : u(a) {}
5 private: unsigned int u;
6 };
7
8 int main () {
9
10 std::vector<A y;
11 y.push_back(A(2));
12 }

On Dec 21, 4:38*pm, hweek...@yahoo.com wrote:




似乎我不能在A类中指定const变量u,单向

解决问题可能是构建一个拷贝构造函数。然而,为什么

C ++或vector类不喜欢这段代码?我的g ++是:gcc版本

4.0.1(Apple Inc. build 5465)。感谢您的帮助。


编译错误摘要:

-------------------- -------------------

cpp.C:4:错误:非静态const成员''const unsigned int A :: u' ',

不能使用默认赋值运算符

/usr/include/c++/4.0.0/bits/vector.tcc:260:警告:合成

方法''​​A&这里首先需要A :: operator =(const A&)'


代码:

-------

* 1

* 2 #include< vector>

* 3

* 4 struct A {

* 5 * A(const unsigned int a):u(a){}

* 6 * private:const unsigned int u;

* 7};

* 8

* 9 int main(){

* 10

* 11 * std :: vector< A * y;

* 12 * y.push_back(A(2));

* 13}


完整编译错误:

-------------------------

[16:34]:Macintosh: tmp 7+ g ++ cpp.C

cpp.C:在成员函数''A& A :: operator =(const A&)'':

cpp.C:4:*从''void std :: vector< _Tp,

_Alloc>实例化:: _ M_insert_aux(__ gnu_cxx :: __ normal_iterato r< typename

_Alloc :: pointer,std :: vector< _Tp,_Alloc,const _Tp&)[with _Tp =

A ,_Alloc = std :: allocator< A>]''

/usr/include/c++/4.0.0/bits/stl_vector.h:610:*从
$ b $实例化b''void std :: vector< _Tp,_Alloc> :: push_back(const _Tp&)[with _Tp = A,

_Alloc = std :: allocator< A>]''

cpp.C:12:*从这里实例化

cpp.C:4:错误:非静态const成员''const unsigned int A :: u'',

不能使用默认赋值运算符

/usr/include/c++/4.0.0/bits/vector.tcc:在成员函数中''void

std :: vector< _Tp,

_Alloc> :: _ M_insert_aux(__ gnu_cxx :: __ normal_iterato r< typename

_Alloc :: pointer,std :: vector< _Tp ,_Alloc,const _Tp&)[w ith _Tp =

A,_Alloc = std :: allocator< A>]'':

/usr/include/c++/4.0.0/bits/vector.tcc :260:警告:合成

方法''​​A& A :: operator =(const A&)''首先需要这里


it seems i can''t assign the const variable u in class A, one way to
solve the problem may be to build a copy constructor. however, why
does C++ or vector class not like this code? my g++ is: gcc version
4.0.1 (Apple Inc. build 5465). thanks for the help.

summary of compile error:
---------------------------------------
cpp.C:4: error: non-static const member ''const unsigned int A::u'',
can''t use default assignment operator
/usr/include/c++/4.0.0/bits/vector.tcc:260: warning: synthesized
method ''A& A::operator=(const A&)'' first required here

code:
-------
* 1
* 2 #include <vector>
* 3
* 4 struct A {
* 5 * A(const unsigned int a) : u(a) { }
* 6 * private: const unsigned int u;
* 7 };
* 8
* 9 int main () {
*10
*11 * std::vector<A*y;
*12 * y.push_back(A(2));
*13 }

full compile error:
-------------------------
[16:34]:Macintosh:tmp 7+ g++ cpp.C
cpp.C: In member function ''A& A::operator=(const A&)'':
cpp.C:4: * instantiated from ''void std::vector<_Tp,
_Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterato r<typename
_Alloc::pointer, std::vector<_Tp, _Alloc, const _Tp&) [with _Tp =
A, _Alloc = std::allocator<A>]''
/usr/include/c++/4.0.0/bits/stl_vector.h:610: * instantiated from
''void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = A,
_Alloc = std::allocator<A>]''
cpp.C:12: * instantiated from here
cpp.C:4: error: non-static const member ''const unsigned int A::u'',
can''t use default assignment operator
/usr/include/c++/4.0.0/bits/vector.tcc: In member function ''void
std::vector<_Tp,
_Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterato r<typename
_Alloc::pointer, std::vector<_Tp, _Alloc, const _Tp&) [with _Tp =
A, _Alloc = std::allocator<A>]'':
/usr/include/c++/4.0.0/bits/vector.tcc:260: warning: synthesized
method ''A& A::operator=(const A&)'' first required here


12月21日凌晨3:38,hweek ... @ yahoo。 com写道:
On Dec 21, 3:38 am, hweek...@yahoo.com wrote:




似乎我不能在A类中分配const变量u,一个方式来解决问题可能是建立一个复制构造函数。然而,为什么

C ++或vector类不喜欢这段代码?我的g ++是:gcc版本

4.0.1(Apple Inc. build 5465)。感谢您的帮助。


编译错误摘要:

-------------------- -------------------

cpp.C:4:错误:非静态const成员''const unsigned int A :: u' ',

不能使用默认赋值运算符

/usr/include/c++/4.0.0/bits/vector.tcc:260:警告:合成

方法''​​A&这里首先需要A :: operator =(const A&)'


代码:

-------

1

2 #include< vector>

3

4 struct A {

5 A( const unsigned int a):u(a){}

6 private:const unsigned int u;

7};

8 br />
9 int main(){

10

11 std :: vector< A y;

12 y.push_back (A(2));

13}


it seems i can''t assign the const variable u in class A, one way to
solve the problem may be to build a copy constructor. however, why
does C++ or vector class not like this code? my g++ is: gcc version
4.0.1 (Apple Inc. build 5465). thanks for the help.

summary of compile error:
---------------------------------------
cpp.C:4: error: non-static const member ''const unsigned int A::u'',
can''t use default assignment operator
/usr/include/c++/4.0.0/bits/vector.tcc:260: warning: synthesized
method ''A& A::operator=(const A&)'' first required here

code:
-------
1
2 #include <vector>
3
4 struct A {
5 A(const unsigned int a) : u(a) { }
6 private: const unsigned int u;
7 };
8
9 int main () {
10
11 std::vector<A y;
12 y.push_back(A(2));
13 }



[snip]


之一诸如std :: vector之类的容器的要求是它的

元素是可复制和可分配的。

你的类型有一个const成员,所以默认赋值失败(如果你

考虑一下 - 这样更安全,也很精彩。)

解决方案:定义你自己的op =(...)假设分配const

成员还可以。


struct A

{

A(const unsigned a):u(a){}

A& operator =(const A& rhs)

{

if(& rhs == this)//自检!

return * this ;

u = rhs.u;

返回* this;

}

私人:

unsigned u;

};

[ snip ]

One of the requirements of containers such as std::vector is that its
elements be copyable and assigneable.
Your type has a const member, so default assignment fails (if you
think about it - thats safer and quite brilliant).
Solution: define your own op=(...) assuming that assigning that const
member is ok.

struct A
{
A(const unsigned a) : u(a) { }
A& operator=(const A& rhs)
{
if(&rhs == this) // self check!
return *this;
u = rhs.u;
return *this;
}
private:
unsigned u;
};


On 2007-12-21 03:38:33 -0500,< a href =mailto:hw ****** @ yahoo.com> hw ****** @ yahoo.com 说:
On 2007-12-21 03:38:33 -0500, hw******@yahoo.com said:

hi,


似乎我不能在A类中分配const变量u,一种方式来解决问题可能是建立问题复制构造函数。然而,为什么

C ++或vector类不喜欢这段代码?我的g ++是:gcc版本

4.0.1(Apple Inc. build 5465)。感谢您的帮助。


编译错误摘要:

-------------------- -------------------

cpp.C:4:错误:非静态const成员''const unsigned int A :: u' ',

不能使用默认赋值运算符

/usr/include/c++/4.0.0/bits/vector.tcc:260:警告:合成

方法''​​A&这里首先需要A :: operator =(const A&)'


代码:

-------

1

2 #include< vector>

3

4 struct A {

5 A( const unsigned int a):u(a){}

6 private:const unsigned int u;

7};

8 br />
9 int main(){

10

11 std :: vector< A y;

12 y.push_back (A(2));

13}


it seems i can''t assign the const variable u in class A, one way to
solve the problem may be to build a copy constructor. however, why
does C++ or vector class not like this code? my g++ is: gcc version
4.0.1 (Apple Inc. build 5465). thanks for the help.

summary of compile error:
---------------------------------------
cpp.C:4: error: non-static const member ''const unsigned int A::u'',
can''t use default assignment operator
/usr/include/c++/4.0.0/bits/vector.tcc:260: warning: synthesized
method ''A& A::operator=(const A&)'' first required here

code:
-------
1
2 #include <vector>
3
4 struct A {
5 A(const unsigned int a) : u(a) { }
6 private: const unsigned int u;
7 };
8
9 int main () {
10
11 std::vector<A y;
12 y.push_back(A(2));
13 }



此代码存在许多问题。

首先,当执行(12)时,std :: vector尝试分配一个

struct A的数组。不幸的是,struct A没有默认构造函数。所以,它
不能构造数组中的每个元素。


其次,std :: vector将尝试将A(2)分配给其中一个元素。

新分配的数组中的可用条目。不幸的是,你确实没有提供赋值运算符,所以默认值就是。

此外不幸的是,默认的赋值运算符试图复制

const成员u,你不能改变const成员。这是您在下面看到的

错误消息。

You have a number of problems with this code.

First, when (12) is executed, std::vector tries to allocate an array of
struct A. Unfortunately, struct A has no default constructor. So, it
cannot construct each element in the array.

Second, std::vector will then try to assign A(2) to one of the
available entry in the newly allocated array. Unfortunately, you did
not provide an assignment operator, so the default one kicks in.
Furthermore unfortunate, the default assignment operator tries to copy
the const member u, and you cannot change const members. This is the
error message you are seeing below.


>


完整编译错误:

-------------------------

[16:34]:Macintosh :tmp 7+ g ++ cpp.C

cpp.C:在成员函数''A& A :: operator =(const A&)'':

cpp.C:4:从''void std :: vector< _Tp,

_Alloc>实例化: :_M_insert_aux(__ gnu_cxx :: __ normal_iterato r< typename

_Alloc :: pointer,std :: vector< _Tp,_Alloc,const _Tp&)[with _Tp =

A, _Alloc = std :: allocator< A>]''

/usr/include/c++/4.0.0/bits/stl_vector.h:610:从

'实例化'void std :: vector< _Tp,_Alloc> :: push_back(const _Tp&)[with _Tp = A,

_Alloc = std :: allocator< A>]''

cpp.C:12:从这里实例化

cpp.C:4:错误:非静态const成员''const unsigned int A :: u'',

不能使用默认赋值运算符

/usr/include/c++/4.0.0/bits/vector.tcc:在成员函数中''void

std :: vector< _Tp,

_Alloc> :: _ M_insert_aux(__ gnu_cxx :: __ normal_iterato r< typename

_Alloc :: pointer,std :: vector< _Tp,_Alloc, const _Tp&)[w ith _Tp =

A,_Alloc = std :: allocator< A>]'':

/usr/include/c++/4.0.0/bits/vector.tcc :260:警告:合成

方法''​​A& A :: operator =(const A&)''首先需要这里
>

full compile error:
-------------------------
[16:34]:Macintosh:tmp 7+ g++ cpp.C
cpp.C: In member function ''A& A::operator=(const A&)'':
cpp.C:4: instantiated from ''void std::vector<_Tp,
_Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterato r<typename
_Alloc::pointer, std::vector<_Tp, _Alloc, const _Tp&) [with _Tp =
A, _Alloc = std::allocator<A>]''
/usr/include/c++/4.0.0/bits/stl_vector.h:610: instantiated from
''void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = A,
_Alloc = std::allocator<A>]''
cpp.C:12: instantiated from here
cpp.C:4: error: non-static const member ''const unsigned int A::u'',
can''t use default assignment operator
/usr/include/c++/4.0.0/bits/vector.tcc: In member function ''void
std::vector<_Tp,
_Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterato r<typename
_Alloc::pointer, std::vector<_Tp, _Alloc, const _Tp&) [with _Tp =
A, _Alloc = std::allocator<A>]'':
/usr/include/c++/4.0.0/bits/vector.tcc:260: warning: synthesized
method ''A& A::operator=(const A&)'' first required here



-


-kira

--

-kira


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

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