参考问题(长) [英] References Question (Long)

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

问题描述

我有一个问题,我已经获得了一段代码

显然

在Visual C ++下编译但我无法在
$下编译b $ b g ++ 3.2。

我已经阅读了常见问题解答并深入研究了Stroustrup书中的

以及

O''Reilly但是请我不是一个天生的C ++程序员所以

允许我

一些范围可以提供一些不用脑子的东西;)


现在查看这里的代码是一个简单的例子,我想要解决这个问题:

我想要解决这个问题:


int f(int& x,int& y)

{

int local;


//做一些不正当的事情与x& y商店结果在当地

返回(本地);

}


int g(int& x,int& y)

{

int local;


//做一些贬低x& y store结果在本地

返回(本地);

}


据我所知,它在C ++中传回局部变量是

被认为是一个

badidea(tm),我不确定我完全理解这个推理

这个
$ b虽然$ b。虽然我猜这是一个功能性的东西而不是真正的

有利于物体的想法。


类矩阵{

matrix :: matrix();

matrix :: matrix(int& i,int& j){

//做一些与i& amp; j创建矩阵

}

};


然后构造函数被称为这样的东西,


矩阵M(g(a,b)+ f(c + d),f(h,t)* g(o,p));


现在这甚至没有编译投掷无法转换

g(..)到

int&错误。我理解这一点,为了让它编译我玩弄了



创建g和f返回引用的想法,如int

& g(int& ....,"

然而,这种情况立即变得很明显

尽管它是b / b
确实编译了警告,在C中创建指针堆叠

结果

也会被视为坏形式。另一个明显的kludge

来获取它

工作将把返回的结果存储在

中,调用

函数并将参数替换为构造函数,如:


int tmp_g = g(a,b);

int tmp_f = f(a,b);


矩阵M (tmp_g,tmp_f);


这应该有效,但我很感激它又回来了

当地人,

但是我需要一些工作很快,这将是短期内合理的

吗?原始代码据说可以在

下运行Windows bu如果修改如上所述,那么这将是Linux的情况,我会理解

这可能会导致未定义行为?


我应该咬紧牙关思考a)重写

或b)得到

那家伙谁写了它重做它。我真的很讨厌

因为我没有b $ b没有时间去做或者给一个即兴的OO

当然

作者。


问题是为什么原版甚至在MSVC上编译

更不用说

跑吗?到目前为止我一直无法抓住

的作者代码

所以我只说他的话有效,但看起来很奇怪。


感谢您的任何建议,


Daniel。

解决方案

< blockquote> Daniel Wilcox< dv ***** @ yahoo.co.uk>写道:

int f(int& x,int& y)
int g(int& x,int& y)
class matrix {
matrix :: matrix();
matrix :: matrix(int& i,int& j){
//做一些与i& j创建一个矩阵
}
};
然后构造函数被称为这样的东西,
矩阵M(g(a,b)+ f(c + d),f(h,t)* g(o,p));
现在这甚至没有编译投掷无法转换
g(..)到
int&错误。




您可以将matrix :: matrix的参数转换为整数而不是

对整数的引用。也许你也应该这样做你的功能

f如果你想把c + d传递给它。


再见,

Chris Dams


change" int&"到const int&在你的声明中(对于f,g,矩阵,......)

(除非你想修改函数中的参数 - 但是确实如此确实是
,这样的调用就像矩阵(f(..),g(..))毫无意义。


Chris Dams写道:

Daniel Wilcox< dv ***** @ yahoo.co.uk>写道:

矩阵M(g(a,b)+ f(c + d),f(h,t)* g(o,p));



现在这甚至没有编译投掷无法转换
g(..)到
int&错误。



您可以将matrix :: matrix的参数转换为整数而不是
对整数的引用。也许你也应该对你的功能这样做如果你想把c + d传递给它。

再见,
Chris Dams




应该是c,d。谢谢。


I have a question, I have been given a piece of code that
apparantly
compiles under Visual C++ but I cannot get to compile under
g++ 3.2.
I have read the FAQ and delved into the Stroustrup book as
well as an
O''Reilly one but please I am not a natural C++ programmer so
allow me
some scope to commit some no-brainers as it were ;)

Now looking at the code here is a simplified example of the
problem I
am trying to resolve:

int f(int &x, int &y)
{
int local;

// do something perverse with x & y store result in local
return(local);
}

int g(int &x, int &y)
{
int local;

// do something depraved with x & y store result in local
return(local);
}

As I understand it passing back local variables in C++ is
considered a
badidea(tm), I''m not sure I fully appreciate the reasoning
behind this
though. Although I guess it''s a functional thing and not really
conducive to the idea of objects.

class matrix {
matrix::matrix();
matrix::matrix(int &i, int &j) {
// do something kinky with i & j to create a matrix
}
};

Then the constructor is called something like this,

matrix M(g(a,b) + f(c+d), f(h,t) * g(o,p));

Now this doesn''t even compile throwing a "could not convert
g(..) to
int&" error. I understand this, to make it compile I toyed
with the
idea of making g and f return references, as in "int
&g(int&....,"
however the ungoodness of this became immediately apparent
although it
did compile with warnings, creating pointers in C to stack
results
would be considered bad form as well. Another obvious kludge
to get it
to work would be to store the results of the returns in the
calling
function and replace the parameters to the constructor as in:

int tmp_g = g(a,b);
int tmp_f = f(a,b);

matrix M(tmp_g, tmp_f);

This should work but I appreciate that again it is returning
locals,
however I need something working quickly, would this be
reasonable in
the short term? The original code allegedly works under
Windows but
will this be the case with Linux if modified as above, I
understand
this may lead to "undefined" behaviour?

Should I bite the bullet and think about either a) re-write
or b) get
the guy who wrote it to redo it. I am loathe really to do
either as I
havn''t really got time to do it or give an impromptu OO
course to the
author.

The questions is why does the original even compile on MSVC
let alone
run? I have so far been unable to get hold of the author of
the code
so I only have his word that it works, but it seems strange.

Thanks for any advice,

Daniel.

解决方案

Daniel Wilcox <dv*****@yahoo.co.uk> writes:

int f(int &x, int &y) int g(int &x, int &y) class matrix {
matrix::matrix();
matrix::matrix(int &i, int &j) {
// do something kinky with i & j to create a matrix
}
}; Then the constructor is called something like this, matrix M(g(a,b) + f(c+d), f(h,t) * g(o,p)); Now this doesn''t even compile throwing a "could not convert
g(..) to
int&" error.



You can turn the arguments of matrix::matrix into integers instead of
references to integers. Perhaps you should also do this to your function
f if you want to pass c+d to it.

Bye,
Chris Dams


change "int&" to "const int&" in your declarations (for f,g,matrix,...)
(unless you want to modify the arguments in the functions - but then
indeed, a call like matrix(f(..),g(..)) makes no sense).


Chris Dams wrote:

Daniel Wilcox <dv*****@yahoo.co.uk> writes:

matrix M(g(a,b) + f(c+d), f(h,t) * g(o,p));



Now this doesn''t even compile throwing a "could not convert
g(..) to
int&" error.


You can turn the arguments of matrix::matrix into integers instead of
references to integers. Perhaps you should also do this to your function
f if you want to pass c+d to it.

Bye,
Chris Dams



That should have been "c,d". Thanks.


这篇关于参考问题(长)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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