传递构造函数的指针 [英] Passing a pointer of constructor
问题描述
在浏览我公司现有的代码库时,我看到了一堆
奇怪的行。
看看这个:
A级
{
公开:
A(Foo * f):_ f(f ){}
私人:
Foo * _f;
};
级B:公共A
{
公开:
B():A(&(Foo&)Foo()) ){}
};
如何传递Foo()作为A的构造函数的参数?
分配给Foo()的内存不会很快被销毁
因为我们超出范围B()?
这是什么意思?
While going through my company''s existing codebase, I saw a bunch of
weird lines.
Take a look at this one:
class A
{
public:
A(Foo *f) : _f(f) {}
private:
Foo *_f;
};
class B : public A
{
public:
B() : A(&(Foo &)Foo())) {}
};
How can you pass "Foo()" as the argument to the constructor of A ?
Wouldn''t the memory that is allocated to Foo() get destroyed as soon
as we are out of the scope of B()?
What does this mean?
推荐答案
有趣......你确定这是可编辑的代码吗?
g ++返回以下错误:
错误:类型''Foo''的右值表达式无效转换为
''Foo&''
5月3日下午1点14分,seank76< sean ... @ gmail.comwrote:
Interesting...Are you sure this is compilable code?
g++ returns the following error :
error: invalid cast of an rvalue expression of type ''Foo'' to type
''Foo&''
On May 3, 1:14 pm, seank76 <sean...@gmail.comwrote:
在浏览我公司现有的代码库时,我看到了一堆
奇怪的行。
看看这个一个:
A级
{
公开:
A(Foo * f): _f(f){}
私人:
Foo * _f;
};
B级:公共A
{
公开:
B():A(&(Foo&)Foo())){}
};
你怎么能传递Foo()作为A的构造函数的参数?
分配给Foo()的内存不会很快被销毁
因为我们超出范围B()?
这是什么意思?
While going through my company''s existing codebase, I saw a bunch of
weird lines.
Take a look at this one:
class A
{
public:
A(Foo *f) : _f(f) {}
private:
Foo *_f;
};
class B : public A
{
public:
B() : A(&(Foo &)Foo())) {}
};
How can you pass "Foo()" as the argument to the constructor of A ?
Wouldn''t the memory that is allocated to Foo() get destroyed as soon
as we are out of the scope of B()?
What does this mean?
5月3日下午2:16,pmouse< pmo ... @ cogeco.cawrote:
On May 3, 2:16 pm, pmouse <pmo...@cogeco.cawrote:
有趣......你确定这是可编辑的代码吗?
g ++返回以下错误:
错误:无效的强制转换类型''Foo''的右值表达式类型
''Foo&''
5月3日下午1:14,seank76< sean ... @ gmail.comwrote:
Interesting...Are you sure this is compilable code?
g++ returns the following error :
error: invalid cast of an rvalue expression of type ''Foo'' to type
''Foo&''
On May 3, 1:14 pm, seank76 <sean...@gmail.comwrote:
在浏览我公司现有的代码库时,我看到了一堆
奇怪的行。
While going through my company''s existing codebase, I saw a bunch of
weird lines.
看看这个:
Take a look at this one:
class A
{
public:
A(Foo * f):_ f(f){}
class A
{
public:
A(Foo *f) : _f(f) {}
私人:
Foo * _f;
private:
Foo *_f;
};
};
class B:public A
{
public:
B():A(&(Foo&)Foo())){}
class B : public A
{
public:
B() : A(&(Foo &)Foo())) {}
};
};
如何传递Foo()作为A的构造函数的参数?
分配给Foo()的内存不会很快被销毁
因为我们超出范围B()?
How can you pass "Foo()" as the argument to the constructor of A ?
Wouldn''t the memory that is allocated to Foo() get destroyed as soon
as we are out of the scope of B()?
这是什么意思?
What does this mean?
我绝对可以使用Sun forte 6.2编译器编译它。
你确定正确定义了类Foo吗? br />
I can definitely compile this using Sun forte 6.2 compiler.
Did you make sure to define class Foo correctly?
seank76写道:
seank76 wrote:
在浏览我公司现有的代码库时,我看到了一堆< br $>
怪异的线条。
看看这个:
A级
{
public:
A(Foo * f):_ f(f){}
private:
Foo * _f;
};
B级:公共A
{
public:
B():A(&(Foo&)Foo())){}
};
>
你怎么能传递Foo()作为A的构造函数的参数?
分配给Foo()的内存不会很快被销毁
因为我们超出范围B()?
这是什么意思?
While going through my company''s existing codebase, I saw a bunch of
weird lines.
Take a look at this one:
class A
{
public:
A(Foo *f) : _f(f) {}
private:
Foo *_f;
};
class B : public A
{
public:
B() : A(&(Foo &)Foo())) {}
};
How can you pass "Foo()" as the argument to the constructor of A ?
Wouldn''t the memory that is allocated to Foo() get destroyed as soon
as we are out of the scope of B()?
What does this mean?
这意味着你有问题。
首先想到的是&(Foo&) Foo())可能会返回
临时地址。这意味着A(Foo * f):_ f(f){}被初始化为将在A返回的
构造函数后不久销毁的东西。
到处都是,这是一件非常糟糕的事情。
It means you have problems.
The first thing that comes to mind is that &(Foo &)Foo()) might return
the address of a temporary. This means that A(Foo *f) : _f(f) {} is
being initialized to somthing that will be destroyed shortly after the
constructor for A returns.
All around, a very bad thing to be doing.
这篇关于传递构造函数的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!