D
Daniel Wilcox
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.
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.