C++ construct on AIX

H

Henrik Goldman

Hi

I have a C++ construct which works fine on a number of platforms. However on
AIX using g++ 3.3.2 it gives a warning:

Here it is:
#include <stdio.h>
class c1
{
public:
char *GetBlah()
{
static char *szPrefix = "ABCD";
return szPrefix;

}

};

int main()

{

c1 c;

printf("%s\n", c.GetBlah());

return 0;

}


What I get is:

test2.cpp: In member function `char* c1::GetBlah()':
test2.cpp:11: warning: sorry: semantics of inline function static data `
char*szPrefix' are wrong (you'll wind up with multiple copies)
test2.cpp:11: warning: you can work around this by removing the
initializer


Isn't this valid code?

Thanks in advance.

-- Henrik
 
A

Alf P. Steinbach

* Henrik Goldman:
I have a C++ construct which works fine on a number of platforms. However on
AIX using g++ 3.3.2 it gives a warning:

#include <stdio.h>
class c1
{
public:
char *GetBlah()
{
static char *szPrefix = "ABCD";
return szPrefix;
}
};

Indentation would be nice.

int main()
{
c1 c;
printf("%s\n", c.GetBlah());
return 0;
}

What I get is:

test2.cpp: In member function `char* c1::GetBlah()':
test2.cpp:11: warning: sorry: semantics of inline function static data `
char*szPrefix' are wrong (you'll wind up with multiple copies)
test2.cpp:11: warning: you can work around this by removing the
initializer

Isn't this valid code?

AFAICS it is (although the lack of indentation makes it difficult to
see), but only because of the C compatibity that allows you to
initialize a char* with a literal string; I'd make that a char const*.

There have been a number of postings pointg out errors in AIX C++
compilers (possibly the same compiler), and I think the warning with
"sorry" is a way to tell you the compiler folks knew that this was wrong
behavior, but didn't have the time to fix it.

Perhaps upgrade the compiler, if possible? Alternatively, put that
function in an implementation file, compiled separately.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top