h****b 发帖数: 157 | 1 class Foo {
public:
virtual ~Foo() {}
};
class Bar : public Foo {
};
class Bar2 : public Foo {
};
class FooBar : public Bar {
};
class FooBar2 : public Bar2 {
};
以下哪个对?
1. Foo &foo = static_cast(FooBar2 f);
2. Foo &foo = dynamic_cast(*(new FooBar2)); 编译不过。
2肯定对,为啥1不对?我试了,如果1写成,
FooBar2 1;
Foo &foo = static_cast(f);
编译没问题。
指教,谢谢 |
h****8 发帖数: 599 | 2 有没有可能是因为Foo &foo = static_cast(FooBar2 f) 中的f在cast完以后就
生命结束了,这样foo是一个无效地址的引用,所以不可以
而FooBar2 f;
Foo &foo = static_cast(f);
中f还是有效的,所以编译通过了 |
z****e 发帖数: 2024 | 3 1. static_cast(FooBar2 f)
FooBar2 f; is a "statement", not a "variable".
if you need something should be like this:
const Foo& foo=static_cast(FooBar2());
i guess hhxk18 means the above. |
h****8 发帖数: 599 | 4 啊你说的对
【在 z****e 的大作中提到】 : 1. static_cast(FooBar2 f) : FooBar2 f; is a "statement", not a "variable". : if you need something should be like this: : const Foo& foo=static_cast(FooBar2()); : i guess hhxk18 means the above.
|
J*****n 发帖数: 4859 | 5
我印象中,如果不是buildin type,static_cast只能对指针进行操作吧。
【在 h****b 的大作中提到】 : class Foo { : public: : virtual ~Foo() {} : }; : class Bar : public Foo { : }; : class Bar2 : public Foo { : }; : class FooBar : public Bar { : };
|
t****t 发帖数: 6806 | 6 印象不对.
【在 J*****n 的大作中提到】 : : 我印象中,如果不是buildin type,static_cast只能对指针进行操作吧。
|
h*******u 发帖数: 15326 | 7 g++ 3.4.4
FooBar2 f;
Foo& foo=static_cast(f);
通过
Foo& foo=static_cast(FooBar2());
通不过
const Foo& foo=static_cast(FooBar2());
通过
谁给讲讲第二个为什么通不过?
【在 z****e 的大作中提到】 : 1. static_cast(FooBar2 f) : FooBar2 f; is a "statement", not a "variable". : if you need something should be like this: : const Foo& foo=static_cast(FooBar2()); : i guess hhxk18 means the above.
|
d****p 发帖数: 685 | 8 What's the point to get reference of a temporary?
This will fail in vritually all compilers except vs.
【在 h*******u 的大作中提到】 : g++ 3.4.4 : FooBar2 f; : Foo& foo=static_cast(f); : 通过 : Foo& foo=static_cast(FooBar2()); : 通不过 : const Foo& foo=static_cast(FooBar2()); : 通过 : 谁给讲讲第二个为什么通不过?
|
|
d****p 发帖数: 685 | 9 The third one is only syntactically right : converting a temporary into a r-
value is ok.
But it is a programming error.
【在 h*******u 的大作中提到】 : g++ 3.4.4 : FooBar2 f; : Foo& foo=static_cast(f); : 通过 : Foo& foo=static_cast(FooBar2()); : 通不过 : const Foo& foo=static_cast(FooBar2()); : 通过 : 谁给讲讲第二个为什么通不过?
|
h*******u 发帖数: 15326 | 10 先不提临时变量,问题是为什么第三个语义上对,而第二个编译却不通过.const区别在
哪里.
【在 d****p 的大作中提到】 : The third one is only syntactically right : converting a temporary into a r- : value is ok. : But it is a programming error.
|
|
|
d****p 发帖数: 685 | 11 T const & is r-value so it accepts T
T& is l-value so it rejects T as a temporary.
【在 h*******u 的大作中提到】 : 先不提临时变量,问题是为什么第三个语义上对,而第二个编译却不通过.const区别在 : 哪里.
|
z****e 发帖数: 2024 | 12 什么programming error?
take a const reference of a temporary is a programming error, why?
the reference can be used later on, so why it is an error?
r-
【在 d****p 的大作中提到】 : The third one is only syntactically right : converting a temporary into a r- : value is ok. : But it is a programming error.
|
d****p 发帖数: 685 | 13 I think we are gonna start a tricky discussion :-)
The lifetime of a temporary is ended when the expression generating the
temporary is evaluated.
So,
Foo const& foo = static_cast(Foo()) will yield a dead reference,
pointing to destroyed object (the
foo). Isn't it a programming error?
So never explictly construct a reference to a temporary - doing so will
invalidate the object behind the
reference when the initialization statement completes.
The form DoSthForFoo(Foo()) is valid (supp
【在 z****e 的大作中提到】 : 什么programming error? : take a const reference of a temporary is a programming error, why? : the reference can be used later on, so why it is an error? : : r-
|
z****e 发帖数: 2024 | 14 so now i see your point that in the function call, temporary lifetime is
determined in the caller's scope.
i totally agree that the temporary will be destroyed after the expression is
evaluated.
But i also expect there are some situations when the temporary will not be
physically destroyed if a reference is attached to it.
sometimes, i tried this kind of coding, and it usually works.
So, if you are sure that the temporary will be destroyed even if there is a
const reference attached to, then i w
【在 d****p 的大作中提到】 : I think we are gonna start a tricky discussion :-) : The lifetime of a temporary is ended when the expression generating the : temporary is evaluated. : So, : Foo const& foo = static_cast(Foo()) will yield a dead reference, : pointing to destroyed object (the : foo). Isn't it a programming error? : So never explictly construct a reference to a temporary - doing so will : invalidate the object behind the : reference when the initialization statement completes.
|
d****p 发帖数: 685 | 15 OK, could you tell if the following code is OK? What's the value of a in
main()?
class Foo
{
public:
....Foo(int a) : a(a) {}
....int GetValue() { return a; }
private:
....int a;
};
int main()
{
....const Foo& foo = static_cast(Foo(1));
... int a = foo.GetValue();
....return 0;
}
is
a
do
【在 z****e 的大作中提到】 : so now i see your point that in the function call, temporary lifetime is : determined in the caller's scope. : i totally agree that the temporary will be destroyed after the expression is : evaluated. : But i also expect there are some situations when the temporary will not be : physically destroyed if a reference is attached to it. : sometimes, i tried this kind of coding, and it usually works. : So, if you are sure that the temporary will be destroyed even if there is a : const reference attached to, then i w
|
z****e 发帖数: 2024 | 16 you may need to have a const member function
int GetValue() const {return a;}
in my machine, the value of 'a' in main is the same as the value of 'a' in the
temporary Foo(x); (assuming the GetValue is a const mem fun.)
so, i mean, i still agree with you that tmp will be destroyed when expression is done the evaluation.
but when there is a const ref attached to the tmp, i'm not quite sure if the tmp will be deleted physically or not.
【在 d****p 的大作中提到】 : OK, could you tell if the following code is OK? What's the value of a in : main()? : class Foo : { : public: : ....Foo(int a) : a(a) {} : ....int GetValue() { return a; } : private: : ....int a; : };
|
d****p 发帖数: 685 | 17 Good catch - the GetValue() should be const.
So do you think the code is valid? :-)
the
【在 z****e 的大作中提到】 : you may need to have a const member function : int GetValue() const {return a;} : in my machine, the value of 'a' in main is the same as the value of 'a' in the : temporary Foo(x); (assuming the GetValue is a const mem fun.) : so, i mean, i still agree with you that tmp will be destroyed when expression is done the evaluation. : but when there is a const ref attached to the tmp, i'm not quite sure if the tmp will be deleted physically or not.
|
z****e 发帖数: 2024 | 18 yes of course, it compiles fine and shows the correct value.
【在 d****p 的大作中提到】 : Good catch - the GetValue() should be const. : So do you think the code is valid? :-) : : the
|
z****e 发帖数: 2024 | 19 when a ref is declared, it must be defined. so the ref is attached to some
piece of memory.
if this piece of memory is destoryed, i think the above code shows "
undefined" behavior even the correct result is shown.
let me try an other snip example to see if my understanding is right or not. |
z****e 发帖数: 2024 | 20 add this to the code
~Foo(){
cout<<"Foo::~Foo()"<
}
output
Foo::~Foo()
1
so the result should be "undefined" when const refer to a tmp.
i now agree with you about this point.
thanks for digging this out. |
|
|
d****p 发帖数: 685 | 21 Cool :-)
Offer letter on the way...
【在 z****e 的大作中提到】 : add this to the code : ~Foo(){ : cout<<"Foo::~Foo()"<: } : output : Foo::~Foo() : 1 : so the result should be "undefined" when const refer to a tmp. : i now agree with you about this point. : thanks for digging this out.
|
z****e 发帖数: 2024 | 22 55555555555555555
and hope it can really happen within a year.
【在 d****p 的大作中提到】 : Cool :-) : Offer letter on the way...
|
t****t 发帖数: 6806 | 23 i think you are wrong here.
the lifetime of a temporary is ended when expression generating the temp is
fully-evaluated, that's usually correct; except in two cases: 1. temp appear
s in the initializer of a declarator, then temp persists until initializatio
n completes. 2. temp is bind to a (const) reference; then temp persists unti
l the reference is out of bound. see 12.2, clause 3-5.
reference,
【在 d****p 的大作中提到】 : I think we are gonna start a tricky discussion :-) : The lifetime of a temporary is ended when the expression generating the : temporary is evaluated. : So, : Foo const& foo = static_cast(Foo()) will yield a dead reference, : pointing to destroyed object (the : foo). Isn't it a programming error? : So never explictly construct a reference to a temporary - doing so will : invalidate the object behind the : reference when the initialization statement completes.
|
t****t 发帖数: 6806 | 24 however, you are right that your example is not OK. let me make it clear:
const A& = A(); //OK
const A& = static_cast(A()); //wrong
you must bind the temp *directly* to the const reference, then clause 5 is
in effect. if cast with static_cast, then clause 5 is not in effect.
【在 d****p 的大作中提到】 : OK, could you tell if the following code is OK? What's the value of a in : main()? : class Foo : { : public: : ....Foo(int a) : a(a) {} : ....int GetValue() { return a; } : private: : ....int a; : };
|
d****p 发帖数: 685 | 25 You are right; thanks for pointing it out.
The static cast expression bypasses the rule for extending temporary's
lifetime: the right side of the initializer
is not a temporary of type T (instead, it is another expression yielding a
const reference const T&). So the
temporary's lifetime is only as long as the expression (in this case the
cast).
Hmmm, a cast sometimes does make a difference.
is
【在 t****t 的大作中提到】 : however, you are right that your example is not OK. let me make it clear: : const A& = A(); //OK : const A& = static_cast(A()); //wrong : you must bind the temp *directly* to the const reference, then clause 5 is : in effect. if cast with static_cast, then clause 5 is not in effect.
|
z****e 发帖数: 2024 | 26 !!!
is
【在 t****t 的大作中提到】 : however, you are right that your example is not OK. let me make it clear: : const A& = A(); //OK : const A& = static_cast(A()); //wrong : you must bind the temp *directly* to the const reference, then clause 5 is : in effect. if cast with static_cast, then clause 5 is not in effect.
|
h*******u 发帖数: 15326 | 27 顶。几位都是大牛
细节上见高低
is
【在 t****t 的大作中提到】 : however, you are right that your example is not OK. let me make it clear: : const A& = A(); //OK : const A& = static_cast(A()); //wrong : you must bind the temp *directly* to the const reference, then clause 5 is : in effect. if cast with static_cast, then clause 5 is not in effect.
|