m***t 发帖数: 254 | 1 In BJ's book, Sec 10.2.7.2, there are following codes:
struct cache{
bool valid;
string rep;
};
class Date {
cache& c;
void compute_cache_value();
//...
public:
//...
string string_rep() const;
};
string Date::string_rep() const {
if (!c.valid) {
compute_cache_value();
c.valid=true;
}
return c.rep;
}
My question is, if we have:
const Date d4;
string s4=d4.string_rep();
will this be undefined behavior? Thanks. |
m***t 发帖数: 254 | 2 please help!
【在 m***t 的大作中提到】 : In BJ's book, Sec 10.2.7.2, there are following codes: : struct cache{ : bool valid; : string rep; : }; : class Date { : cache& c; : void compute_cache_value(); : //... : public:
|
k****f 发帖数: 3794 | 3 你编译都有问题吧
带const的string_rep不能修改普通的成员c。
【在 m***t 的大作中提到】 : In BJ's book, Sec 10.2.7.2, there are following codes: : struct cache{ : bool valid; : string rep; : }; : class Date { : cache& c; : void compute_cache_value(); : //... : public:
|
S*****n 发帖数: 227 | 4 恐怕这个地方编译是能过的,c是个reference, 所以不会变。
如果那个compute_cashe_value()没有什么副作用的话,应该能编译过。
当然了bad practice..hehe
【在 k****f 的大作中提到】 : 你编译都有问题吧 : 带const的string_rep不能修改普通的成员c。
|
L*********r 发帖数: 92 | 5 你的程序可以compile吗?
还没undefined, 已经死了吧 |
L*********r 发帖数: 92 | 6 reference must be initialized.
const must be initialized by a ctor.
【在 S*****n 的大作中提到】 : 恐怕这个地方编译是能过的,c是个reference, 所以不会变。 : 如果那个compute_cashe_value()没有什么副作用的话,应该能编译过。 : 当然了bad practice..hehe
|
S*****n 发帖数: 227 | 7 做class member的reference可以不在declare的时候initialize。
当然使用的时候另说。
【在 L*********r 的大作中提到】 : reference must be initialized. : const must be initialized by a ctor.
|
S*****n 发帖数: 227 | 8 换句话说,如果他的Date class里
cache& c; 的声明 换成
cache* pc; 当然,后面的所有实现里的c.换成pc->
这个玩意儿编译好象还能过。
总之Date::string_rep()的const看起来很没有道理。
懒得看了。
【在 S*****n 的大作中提到】 : 做class member的reference可以不在declare的时候initialize。 : 当然使用的时候另说。
|
L*********r 发帖数: 92 | 9 Hehe,
const Date d4;
is what I am talking |
m***t 发帖数: 254 | 10 谢谢回答, 这就是我不明白的地方, 如果是const &c, c reference的内容也不能变
吧, 而不是说c本身这个
reference不能变。 在这里貌似说c reference的内容可以随便变, 所以不太明白。
这是讲mutable的时候提到的,貌似BJ认为用这种reference和用mutable等效。
【在 S*****n 的大作中提到】 : 恐怕这个地方编译是能过的,c是个reference, 所以不会变。 : 如果那个compute_cashe_value()没有什么副作用的话,应该能编译过。 : 当然了bad practice..hehe
|
|
|
S*****n 发帖数: 227 | 11 yeah.. d4.c是无效引用,这里肯定要出编译错。
【在 L*********r 的大作中提到】 : Hehe, : const Date d4; : is what I am talking
|
S*****n 发帖数: 227 | 12 不过话也难说,Date的ctor没给出code,没准里面有个没参数的ctor,里面
构造个古怪的cache初值,然后把c指向之,所以
const Date d4也许能过编译,haha..
【在 S*****n 的大作中提到】 : yeah.. d4.c是无效引用,这里肯定要出编译错。
|
S*****n 发帖数: 227 | 13 看SM的EC++ item3吧。
【在 m***t 的大作中提到】 : 谢谢回答, 这就是我不明白的地方, 如果是const &c, c reference的内容也不能变 : 吧, 而不是说c本身这个 : reference不能变。 在这里貌似说c reference的内容可以随便变, 所以不太明白。 : 这是讲mutable的时候提到的,貌似BJ认为用这种reference和用mutable等效。
|
L*********r 发帖数: 92 | 14 if i do not remember wrong, reference should be initialized in initialize
list.
【在 S*****n 的大作中提到】 : 不过话也难说,Date的ctor没给出code,没准里面有个没参数的ctor,里面 : 构造个古怪的cache初值,然后把c指向之,所以 : const Date d4也许能过编译,haha..
|
m***t 发帖数: 254 | 15 That is absolutely correct. However, that was not the point of the question.
Unfortunately Stroustrup did
not give a ctor for this code snippet, but I assume the reference has been
safely initialized. The main
question i am having is if c is a regular const reference, then c.valid=true
in string_rep will be a violation
of constness, thus resulting in undefined behavior, right?
【在 L*********r 的大作中提到】 : if i do not remember wrong, reference should be initialized in initialize : list.
|
S*****n 发帖数: 227 | 16 yes. hehe.. Date():c(0) {} ??
not sure if it passes compilation. hehe
class data memebr 里用reference不是给自己找麻烦么?
反正俺从来没用过,要用也是用指针啊。
【在 L*********r 的大作中提到】 : if i do not remember wrong, reference should be initialized in initialize : list.
|
m***t 发帖数: 254 | 17 Prefer new and delete to malloc and free?
【在 S*****n 的大作中提到】 : 看SM的EC++ item3吧。
|
S*****n 发帖数: 227 | 18 那个那个。。ed.3.. 55个items, 不是50个的那版。
【在 m***t 的大作中提到】 : Prefer new and delete to malloc and free?
|
m***t 发帖数: 254 | 19 sigh.. do not have that in hand..
【在 S*****n 的大作中提到】 : 那个那个。。ed.3.. 55个items, 不是50个的那版。
|
L*********r 发帖数: 92 | 20 I do not think so.
for a const pointer or reference, there are two different meanings. one is
the pointer can not be changed, the other is the value which the pointer
pointed to can not be changed. it depend on the location of the const
declaration.
for a const function as your sample,
the function can not change the data member of the class.
the function can only call const function.
your sample has a lot of grammer problems.
question.
true
【在 m***t 的大作中提到】 : That is absolutely correct. However, that was not the point of the question. : Unfortunately Stroustrup did : not give a ctor for this code snippet, but I assume the reference has been : safely initialized. The main : question i am having is if c is a regular const reference, then c.valid=true : in string_rep will be a violation : of constness, thus resulting in undefined behavior, right?
|
|
|
m***t 发帖数: 254 | 21 So you think c.valid=true is ok? it will not violate the constness of string
_rep() or the constness of s4? The
code is from B. Stroustrup's C++ programming language, 3rd edition, page 233
.
【在 L*********r 的大作中提到】 : I do not think so. : for a const pointer or reference, there are two different meanings. one is : the pointer can not be changed, the other is the value which the pointer : pointed to can not be changed. it depend on the location of the const : declaration. : for a const function as your sample, : the function can not change the data member of the class. : the function can only call const function. : your sample has a lot of grammer problems. :
|
t****t 发帖数: 6806 | 22 你确定没看错?我手里的BJ,那个地方用的是cache*,不是cache&
【在 m***t 的大作中提到】 : 谢谢回答, 这就是我不明白的地方, 如果是const &c, c reference的内容也不能变 : 吧, 而不是说c本身这个 : reference不能变。 在这里貌似说c reference的内容可以随便变, 所以不太明白。 : 这是讲mutable的时候提到的,貌似BJ认为用这种reference和用mutable等效。
|
m***t 发帖数: 254 | 23 我倒。。 白纸黑字啊。 你那是第几版的, 我是第三版。
【在 t****t 的大作中提到】 : 你确定没看错?我手里的BJ,那个地方用的是cache*,不是cache&
|
t****t 发帖数: 6806 | 24 special edition
【在 m***t 的大作中提到】 : 我倒。。 白纸黑字啊。 你那是第几版的, 我是第三版。
|
c*****g 发帖数: 119 | 25 just checked it. i second you.
【在 t****t 的大作中提到】 : 你确定没看错?我手里的BJ,那个地方用的是cache*,不是cache&
|
m***t 发帖数: 254 | 26 wo ft..
http://www.research.att.com/~bs/3rd_printing6.html
and I have a 4th printing...
【在 t****t 的大作中提到】 : special edition
|
t****t 发帖数: 6806 | 27 嗯,说明你看书比较仔细.老家伙偷偷自宫了(well,不是真的偷偷)...
【在 m***t 的大作中提到】 : wo ft.. : http://www.research.att.com/~bs/3rd_printing6.html : and I have a 4th printing...
|
t****t 发帖数: 6806 | 28 不过,抛开BJ是怎么写的不谈,如果有
class A {
T& b;
};
const A a;
那a.b引用的对象的确是可以改变的.也就是说这里b的类型是 [const reference to T
(T& const)], 等价于[reference to T (T&)],而不是[reference to const T (const
T&)],和用T* b来定义是一个道理.
注意你不能直接写T& const,这属于语法错误. "const reference"只能通过typedef或
者template来间接的引入,并会被直接忽略.这很好理解,因为reference一旦bind,就不
能rebind,所以和指针不同,它们永远都是const的.[8.3.2, clause 1]
【在 m***t 的大作中提到】 : In BJ's book, Sec 10.2.7.2, there are following codes: : struct cache{ : bool valid; : string rep; : }; : class Date { : cache& c; : void compute_cache_value(); : //... : public:
|
S*****n 发帖数: 227 | 29 我也ft..
没想到俺改得跟老BS改的一样。bs自己。。
【在 m***t 的大作中提到】 : wo ft.. : http://www.research.att.com/~bs/3rd_printing6.html : and I have a 4th printing...
|