由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - *(&b1)=b编译不过,b1=b可以,区别是?
相关主题
发现一个有趣的现象。C++编程问题:union inside struct
one question about initializaiton listC++的"初始化"小结
包含指针的类和vector的问题强迫症爱好者进来做题了
请教几个C++问题C++ pointer to function is buggy
c++ questioncopy constructor问题。
Is the order of initialization a, b, c or c, b, a?C++编程原则的问题
IBM高级软件工程师老印的示例代码,大家看看有多少个bug? (问个copy constructor的问题
C#的Dictionary赋值操作必须先ContainsKey检查吗?请教各路C++大神 为什么f(3) 输出是 'dd'
相关话题的讨论汇总
话题: b1话题: int话题: uu话题: operator
进入Programming版参与讨论
1 (共1页)
d******i
发帖数: 7160
1
代码如下.不懂啊。
class B
{
public:
B(int& a):m_a(a){}
int& m_a;
};
void main()
{
int uu;
B b(uu);
B b1=b; //pass
*(&b1)=b; //fail
}
d******i
发帖数: 7160
2
奇怪的是下面不带参数的能过:
class B
{
public:
/*
B(int& a):m_a(a){}
int& m_a;
*/
};
int main(int argc, char* argv[])
{
/*
int uu;
B b(uu);
B b1=b; //pass
// *(&b1)=b; //fail
*/
B b;
B b1=b;
*(&b1)=b;
return 0;
}
d******i
发帖数: 7160
3
还有更神的,先B b1(uu),再b1=b,又fail了。
int main(int argc, char* argv[])
{
int uu;
B b(uu);
//B b1=b; //pass
B b1(uu);
b1=b; //fail
return 0;
}
按 effective c++ 第五条的说法"如果有引用或者const成员变量,编译器拒绝合成赋
值操作符。",B b1=b就该fail了,不会和以上code有任何区别啊。
请指教。
X****r
发帖数: 3557
4
B b1=b;不调用赋值操作符,而是调用构建函数。

【在 d******i 的大作中提到】
: 还有更神的,先B b1(uu),再b1=b,又fail了。
: int main(int argc, char* argv[])
: {
: int uu;
: B b(uu);
: //B b1=b; //pass
: B b1(uu);
: b1=b; //fail
: return 0;
: }

m*********t
发帖数: 527
5
B b1=b; 调用的是 copy constructor.
为啥你类里面非要放个 reference ? 用指针不好么。。。

【在 d******i 的大作中提到】
: 还有更神的,先B b1(uu),再b1=b,又fail了。
: int main(int argc, char* argv[])
: {
: int uu;
: B b(uu);
: //B b1=b; //pass
: B b1(uu);
: b1=b; //fail
: return 0;
: }

l*********s
发帖数: 5409
6
if you have reference member, compiler does not synthesize a default
assignment operator.
S**I
发帖数: 15689
7

// call implicitly defined copy constructor
// call implicitly defined copy assignment operator; however, B::m_a is a
// reference type, default assignment operator cannot be used

【在 d******i 的大作中提到】
: 代码如下.不懂啊。
: class B
: {
: public:
: B(int& a):m_a(a){}
: int& m_a;
: };
: void main()
: {
: int uu;

S**I
发帖数: 15689
8
What kind of sick compiler are you using? :) This shouldn't compile: B has a
user-defined constructor, so default constructor is not declared. The
statement "B b;" should fail the compilation.

【在 d******i 的大作中提到】
: 奇怪的是下面不带参数的能过:
: class B
: {
: public:
: /*
: B(int& a):m_a(a){}
: int& m_a;
: */
: };
: int main(int argc, char* argv[])

S**I
发帖数: 15689
9
same reason as your first post: no user-defined copy assignment operator and
the implicitly one generated by the compiler cannot handle assignment of a
data member with reference type.
Seriously, if you read the error message generated by the compiler, you don'
t need to ask these questions here.

【在 d******i 的大作中提到】
: 还有更神的,先B b1(uu),再b1=b,又fail了。
: int main(int argc, char* argv[])
: {
: int uu;
: B b(uu);
: //B b1=b; //pass
: B b1(uu);
: b1=b; //fail
: return 0;
: }

d******i
发帖数: 7160
10
谢谢楼上众大大。
俺怎么觉得B b1=b;该调用的是赋值构造函数呢?
而B b1(b);才该调用你们所说的copy constructor?
如果俺非要"*(&b1)=b;"能过,该怎么重载这个operator=呢?
好像根本没法写啊。
相关主题
Is the order of initialization a, b, c or c, b, a?C++编程问题:union inside struct
IBM高级软件工程师老印的示例代码,大家看看有多少个bug? (C++的"初始化"小结
C#的Dictionary赋值操作必须先ContainsKey检查吗?强迫症爱好者进来做题了
进入Programming版参与讨论
d******i
发帖数: 7160
11
是老了点,vc 6.

a

【在 S**I 的大作中提到】
: What kind of sick compiler are you using? :) This shouldn't compile: B has a
: user-defined constructor, so default constructor is not declared. The
: statement "B b;" should fail the compilation.

S**I
发帖数: 15689
12
throw it away; use VC 2010 or later.

【在 d******i 的大作中提到】
: 是老了点,vc 6.
:
: a

d******i
发帖数: 7160
13
自搞了一下assignment constructor,能编译过了
B& operator=(const B& b0){memcpy(this,&b0,sizeof(B));return *this;}
可是感觉怪怪的。
本来编译器不给你default产生这个赋值构造函数,就是认为里面的引用不该被改变。现
在可好,还是生给变了。这时编译器咋不说话了?
还是tnd这个vc6太弱了?
BTW,workplace的choice,不由我选的。
S**I
发帖数: 15689
14

Standard said otherwise :)

【在 d******i 的大作中提到】
: 谢谢楼上众大大。
: 俺怎么觉得B b1=b;该调用的是赋值构造函数呢?
: 而B b1(b);才该调用你们所说的copy constructor?
: 如果俺非要"*(&b1)=b;"能过,该怎么重载这个operator=呢?
: 好像根本没法写啊。

d******i
发帖数: 7160
15

你的这个comment我保留意见(其实这原因在上述effecitve c++的第几条已经点到了,即
'不情愿'给已赋过值的引用型变量再次赋值)。而非技术上有任何的differecne。所以
充其量是给程序员提个醒,洗脱责任而已。
俺要非实现了这个复制构造也就实现了,还不就是个bitwise copy而已呗。所以最合理
的解释是old compiler stupid.还能说什么呢?

【在 S**I 的大作中提到】
:
: Standard said otherwise :)

m*********t
发帖数: 527
16
This is really BAD design and dangerous.... memcpy only does a shallow copy.
If you have any data structure memcpy is going to cause unexpected results.

again, why do you need a int& inside the class instead of int* ?
PS: There is no "Assignment Constuctor" in C++. From your previous posts you
did not seem to even understand when copy constructor is used. Again,
instead of blaming "stupid compilers", you'd better learn why the compiler
was NOT doing that and why what you were doing was against the C++ standards
.

【在 d******i 的大作中提到】
: 自搞了一下assignment constructor,能编译过了
: B& operator=(const B& b0){memcpy(this,&b0,sizeof(B));return *this;}
: 可是感觉怪怪的。
: 本来编译器不给你default产生这个赋值构造函数,就是认为里面的引用不该被改变。现
: 在可好,还是生给变了。这时编译器咋不说话了?
: 还是tnd这个vc6太弱了?
: BTW,workplace的choice,不由我选的。

S**I
发帖数: 15689
17
If you explicitly define the copy assigment operator, you can simply use
B& operator=(const B& b0){m_a = b0.m_a;return *this;}
instead of memcpy.
Nothing prevents you from explicitly assigning a value to a data member with
reference type. It is just not the compiler's responsibility to do this for
you.
While VC 6 is a buggy compiler, it does nothing wrong here. All modern
decent C++ compilers should generate the same error in your first post.

【在 d******i 的大作中提到】
:
: 你的这个comment我保留意见(其实这原因在上述effecitve c++的第几条已经点到了,即
: '不情愿'给已赋过值的引用型变量再次赋值)。而非技术上有任何的differecne。所以
: 充其量是给程序员提个醒,洗脱责任而已。
: 俺要非实现了这个复制构造也就实现了,还不就是个bitwise copy而已呗。所以最合理
: 的解释是old compiler stupid.还能说什么呢?

l*********s
发帖数: 5409
18
probably lz are only familiar with C but are now forced to write C++.

for

【在 S**I 的大作中提到】
: If you explicitly define the copy assigment operator, you can simply use
: B& operator=(const B& b0){m_a = b0.m_a;return *this;}
: instead of memcpy.
: Nothing prevents you from explicitly assigning a value to a data member with
: reference type. It is just not the compiler's responsibility to do this for
: you.
: While VC 6 is a buggy compiler, it does nothing wrong here. All modern
: decent C++ compilers should generate the same error in your first post.

d******i
发帖数: 7160
19

这个反了吧?其实本意应该是不给this的m_a赋值,但给所有其他的data member赋值。
好像只能写成这样了吧:
B& operator=(const B& b0){int m_a0=m_a;memcpy(this,&b0,sizeof(B));m_a=m_a0;
return *this;}
是难看了点,不过没别的写法了。
for

【在 S**I 的大作中提到】
: If you explicitly define the copy assigment operator, you can simply use
: B& operator=(const B& b0){m_a = b0.m_a;return *this;}
: instead of memcpy.
: Nothing prevents you from explicitly assigning a value to a data member with
: reference type. It is just not the compiler's responsibility to do this for
: you.
: While VC 6 is a buggy compiler, it does nothing wrong here. All modern
: decent C++ compilers should generate the same error in your first post.

S**I
发帖数: 15689
20
If you don't want this->m_a changed,
B& operator=(const B& b0){return *this;}
is enough; memcpy is not necessary, assuming no data member other than m_a
exists.

【在 d******i 的大作中提到】
:
: 这个反了吧?其实本意应该是不给this的m_a赋值,但给所有其他的data member赋值。
: 好像只能写成这样了吧:
: B& operator=(const B& b0){int m_a0=m_a;memcpy(this,&b0,sizeof(B));m_a=m_a0;
: return *this;}
: 是难看了点,不过没别的写法了。
: for

相关主题
C++ pointer to function is buggy问个copy constructor的问题
copy constructor问题。请教各路C++大神 为什么f(3) 输出是 'dd'
C++编程原则的问题请教unique_ptr vs auto_ptr
进入Programming版参与讨论
d******i
发帖数: 7160
21

FT。别的成员变量是要copy的啊。我上面那是最简单的例子。B要是有其他的成员还是
要显式地copy过去,逃不掉的。

【在 S**I 的大作中提到】
: If you don't want this->m_a changed,
: B& operator=(const B& b0){return *this;}
: is enough; memcpy is not necessary, assuming no data member other than m_a
: exists.

S**I
发帖数: 15689
22
Of course; but using memcpy is bad.

【在 d******i 的大作中提到】
:
: FT。别的成员变量是要copy的啊。我上面那是最简单的例子。B要是有其他的成员还是
: 要显式地copy过去,逃不掉的。

s*w
发帖数: 729
23
B b1=b; called constructor, not copy constructor
=b; here is initialization instead of assignment

【在 m*********t 的大作中提到】
: B b1=b; 调用的是 copy constructor.
: 为啥你类里面非要放个 reference ? 用指针不好么。。。

t****t
发帖数: 6806
24
actually, it is called "copy constructor"...

【在 s*w 的大作中提到】
: B b1=b; called constructor, not copy constructor
: =b; here is initialization instead of assignment

m*********t
发帖数: 527
25
Re

【在 t****t 的大作中提到】
: actually, it is called "copy constructor"...
t****t
发帖数: 6806
26
简单地说, reference是一次性的. 如果你要换被refer的对象, 就不要用reference.
另外, 用memcpy一类的函数覆盖一个reference应该是属于undefined行为.

【在 d******i 的大作中提到】
:
: FT。别的成员变量是要copy的啊。我上面那是最简单的例子。B要是有其他的成员还是
: 要显式地copy过去,逃不掉的。

d******i
发帖数: 7160
27
同意。
忍不住看了int& m_a的内存位置存的值,居然就是值本身,不是什么地址。
用memcpy覆盖后原值没变,新值后移了4个字节。
但从vc里看已经变了。
好神奇啊。
到底有没有标准说引用型变量的内存位置该放什么东西呢?原值本身,还是原值的内存
地址?

【在 t****t 的大作中提到】
: 简单地说, reference是一次性的. 如果你要换被refer的对象, 就不要用reference.
: 另外, 用memcpy一类的函数覆盖一个reference应该是属于undefined行为.

d***a
发帖数: 13752
28
引用变量里,实际存的是地址。

【在 d******i 的大作中提到】
: 同意。
: 忍不住看了int& m_a的内存位置存的值,居然就是值本身,不是什么地址。
: 用memcpy覆盖后原值没变,新值后移了4个字节。
: 但从vc里看已经变了。
: 好神奇啊。
: 到底有没有标准说引用型变量的内存位置该放什么东西呢?原值本身,还是原值的内存
: 地址?

1 (共1页)
进入Programming版参与讨论
相关主题
请教各路C++大神 为什么f(3) 输出是 'dd'c++ question
请教unique_ptr vs auto_ptrIs the order of initialization a, b, c or c, b, a?
c++ 里面 this pointer 是完全 un necessary 的吗?IBM高级软件工程师老印的示例代码,大家看看有多少个bug? (
基本功不扎实,问个问题C#的Dictionary赋值操作必须先ContainsKey检查吗?
发现一个有趣的现象。C++编程问题:union inside struct
one question about initializaiton listC++的"初始化"小结
包含指针的类和vector的问题强迫症爱好者进来做题了
请教几个C++问题C++ pointer to function is buggy
相关话题的讨论汇总
话题: b1话题: int话题: uu话题: operator