Re: A problem with g++ compiler
luca wrote:
suppose we have the following structure:
typedef struct mystruct_t
Firstly, drop this. "struct xxx {...};" will already declare a new type, no
need to wrap this in "typedef xxx xxx". Furter, the "_t" suffix typically
denotes a type alias and I believe is even reserved by POSIX, so its use
might be a bad idea, too.
{
int v1, v2;
unsigned char *v3;
// constructor
mystruct_t( int p_v1, int p_v2, unsigned char *p_v3) : v1(p_v1),
v2(p_v2), v3(p_v3)
{
}
} mystruct_t;
Question: Who owns the unsigned char that v3 points to? Or, who is
responsible for releasing it?
and the following class:
class MyClass
{
public:
static myfunc(mystruct_t &m);
};
Apart from the missing returntype, this class has no own state, no virtual
memberfunctions, no bound memberfunctions at all. Why is there a class at
all? You could have as well used a normal function, optionally in a
separate namespace.
Somewhere in my code i have the following line:
// ..
MyClass::myfunc(mystruct_t(1, 2, NULL));
It compile with Visual C/C++ , but when i try to compile it with g++
(using linux Mandriva + Eclipse IDE), i get the following error:
no matching function for call to 'MyClass::myfunc(mystruct_t &) '
I can't really understand why g++ can't compile that line. I simply
construct on-the-fly a mystruct_t object and pass it to myfunc()
static method...
The problem is that the on-the-fly constructed object is a temporary that
can't be bound to a non-const reference. VC is not standard-conformant in
that aspect. I believe you can squeeze a warning out of VC for that, if you
raise the warning level.
Typical remedies:
1. Use a reference-to-const instead.
2. Make myfunc() take the same arguments as the mystruct_t constructor and
create the temporary inside the function.
3. Refactor. After all, you are creating an object, passing it to a function
which modifies it, and then throwing it away including any modifications.
This doesn't make sense to me.
Uli
--
Sator Laser GmbH
Gesch??ftsf??hrer: Thorsten F??cking, Amtsgericht Hamburg HR B62 932
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]