-=> Quoting Christian S. Pedersen to All <=-
CSP> int* test()
CSP> {
CSP> int i=5;
CSP> return &i;
CSP> }
CSP> Why do I get a "Suspicious pointer conversion"-warning ? The functions
CSP> using this concept seems to work OK. Should I make them differently.
I believe your C++ compiler is probably using incorrect terminology.
Basically, you are
trying to return a pointer to an automatic variable. Try one of the
following (the first
is preferable):
C Style:
int test( int *value )
{
if( value ) {
*value=5 ;
return 1 ;
} else
return 0 ;
}
C++ Style:
void test( int &value )
{
value=5 ;
}
C/C++ Style (not ideal):
int *test( void )
{
static int i=5 ;
return( &i ) ;
}
As you can see, the C++ style is the most easiest to read, and
the least error prone to programmer error. However, the first example
can be used in C and C++ code.
The latter however, allocates a static variable. For something simple like
this,
a static variable is less than ideal. However, in some circumstances (such
s
a multithreaded program), you might have to declare certain functions such as
functions passed to _beginthread() or CreateThread() as static functions so
that
the C++ 'this' pointer is not passed to the function, thereby causing a GPF
in a
C function that doesn't expect the 'this' pointer.
ttfn.
... I don't do Windows, but OS/2 does.
--- Blue Wave v2.12 OS/2 [NR]
---------------
* Origin: The Eclectic Lab (1:153/831)
|