By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 1,029 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

returning a const char*

P: n/a
Hi,

I think this is slightly OT, (I am not certain that Macros are part of the
standard), but I am hopping that someone could help.

I am trying to use a language file in my system.
the format of the file would be something like

[option]
open=Open
close=Close
....
and so on.

Now I want to create some macros that I could use all over the place in all
the functions that would output text to the screen.

something like

SetMenuText( LANG_CLOSE ); or DisplayMessage( LANG_HELLOWORLD ); and so one
....

where the macro would represent...

#define LANG_CLOSE GetLanguagefile( "option", "close",
"Close" );
#define LANG_HELLOWORLD GetLanguagefile( "option", "hellow", "Hello
world..." );

but my problem is the return value, I would like to return a const char* or
even a char* but I don't think it is possible to return them directly.
I could have a global char *, char * g_szReturn = NULL; but is it really
safe to use it that way?

I need to return the function otherwise the macro are not very useful really
because I would have to add 2 or three lines of code every time.
Like

char szText[1024];
if( GetLanguagefile( "option", "close", "Close", szText, 1024 ) ){
SetMenuText( szText );
}

Any ideas how I could safely return a const char* or char *?

Many thanks.

Simon
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

"Simon" <sp********@schoolsofafrica.com> wrote in message
news:2h************@uni-berlin.de...
Hi,

I think this is slightly OT, (I am not certain that Macros are part of the
standard), but I am hopping that someone could help.
Macros are part of the standard.

I am trying to use a language file in my system.
the format of the file would be something like

[option]
open=Open
close=Close
...
and so on.

Now I want to create some macros that I could use all over the place in all the functions that would output text to the screen.

something like

SetMenuText( LANG_CLOSE ); or DisplayMessage( LANG_HELLOWORLD ); and so one ...

where the macro would represent...

#define LANG_CLOSE GetLanguagefile( "option", "close",
"Close" );
#define LANG_HELLOWORLD GetLanguagefile( "option", "hellow", "Hello
world..." );
No reason for macros, inline functions would be better.

but my problem is the return value, I would like to return a const char* or even a char* but I don't think it is possible to return them directly.
I could have a global char *, char * g_szReturn = NULL; but is it really
safe to use it that way?
Not really, its obscure and therefore dangerous.

I need to return the function otherwise the macro are not very useful really because I would have to add 2 or three lines of code every time.
Like

char szText[1024];
if( GetLanguagefile( "option", "close", "Close", szText, 1024 ) ){
SetMenuText( szText );
}

Any ideas how I could safely return a const char* or char *?


Don't, return a string instead.

#include <string>

inline std::string LANG_CLOSE()
{
return GetLanguagefile( "option", "close", "Close" );
}

SetMenuText(LANG_CLOSE().c_str());

john
Jul 22 '05 #2

P: n/a
Thanks for your reply,

Macros are part of the standard.

ah, thanks.
Don't, return a string instead.

#include <string>

inline std::string LANG_CLOSE()
{
return GetLanguagefile( "option", "close", "Close" );
}

SetMenuText(LANG_CLOSE().c_str());
But could i not go a step further and still use my Macro?

#define LANG_CLOSE GetLanguagefile( "option", "close", "Close" ).c_str()


john


Simon.
Jul 22 '05 #3

P: n/a

"Simon" <sp********@schoolsofafrica.com> wrote in message
news:2h************@uni-berlin.de...
Thanks for your reply,

Macros are part of the standard.


ah, thanks.
Don't, return a string instead.

#include <string>

inline std::string LANG_CLOSE()
{
return GetLanguagefile( "option", "close", "Close" );
}

SetMenuText(LANG_CLOSE().c_str());


But could i not go a step further and still use my Macro?

#define LANG_CLOSE GetLanguagefile( "option", "close", "Close" ).c_str()


Only if GetLanguageFile returns a std::string. My inline function would work
whether GetLanguageFile returns char*, const char* or std::string.

But why use a macro? They have no advantage in this case.

john
Jul 22 '05 #4

P: n/a

But could i not go a step further and still use my Macro?

#define LANG_CLOSE GetLanguagefile( "option", "close", "Close" ).c_str()

Only if GetLanguageFile returns a std::string. My inline function would

work whether GetLanguageFile returns char*, const char* or std::string.
I am not sure I follow, GetLanguagefile(...) will return a string in both
cases.

But why use a macro? They have no advantage in this case.
Just curious really. If I could cut out the '.c_str()' it would be a bonus.

john


Simon
Jul 22 '05 #5

P: n/a

"Sims" <si*********@hotmail.com> wrote in message
news:2h************@uni-berlin.de...

But could i not go a step further and still use my Macro?

#define LANG_CLOSE GetLanguagefile( "option", "close", "Close" ).c_str()
Only if GetLanguageFile returns a std::string. My inline function would

work
whether GetLanguageFile returns char*, const char* or std::string.


I am not sure I follow, GetLanguagefile(...) will return a string in both
cases.


A std::string can be implicitly created from a char* or const char*. This
would happen with my inline function if GetLanguageFile happened to return a
char* or const char*. I just mentioned this because I wasn't sure if
GetLanguageFile was a function you are writing or not. If you are writing it
then you should make it return a std::string.

But why use a macro? They have no advantage in this case.


Just curious really. If I could cut out the '.c_str()' it would be a

bonus.


Fair enough.

john
Jul 22 '05 #6

P: n/a
> >

A std::string can be implicitly created from a char* or const char*. This
would happen with my inline function if GetLanguageFile happened to return a char* or const char*. I just mentioned this because I wasn't sure if
GetLanguageFile was a function you are writing or not. If you are writing it then you should make it return a std::string.
Yes it is my function. So i guess i will make it return std:string

But why use a macro? They have no advantage in this case.


Just curious really. If I could cut out the '.c_str()' it would be a

bonus.


Fair enough.

john

Thanks for your help.

Simon
Jul 22 '05 #7

P: n/a
My Suggestion:

struct Language
{
char* pOpen;
char* pClose;
char* pRead;
char* pWrite;
};
const Language English = {

"Open",
"Close",
"Read",
"Write" };

const Language Irish = {

"Oscail",
"Dún",
"Léigh",
"Scríobh" };


const Language* pCurrentLanguage = &English; //Default to English

void SetLanguage(const Language* const NewLanguage)
{
pCurrentLanguage = NewLanguage;
}

int main(void)
{
SetLanguage(&English);

cout << pCurrentLanguage.pOpen;

SetLanguage(&Irish);

cout << pCurrentLanguage.pOpen;
}

You may consider setting "SetLanguage" to take an enum instead of a pointer,
whatever tickles your fancy. I myself like this way!
Hope that helps.
-JKop
Jul 22 '05 #8

P: n/a
And once again I have missed the point!!
You can have language file like this:
char* pLoadedLanguage = "Open\0Close\0Read\0Write";
Language MakeLanguageStruct(const char* pLoadedLanguage)
{
Language NewLanguage;

NewLanguage.pOpen = pLoadedLanguage;
while (pLoadLanguage +=1) {}

pLoadLanguage +=1;

NewLanguage.pClose = pLoadedLanguage;

while (pLoadLanguage +=1) {}

pLoadLanguage +=1;

NewLanguage.pRead = pLoadedLanguage;

while (pLoadLanguage +=1) {}

pLoadLanguage +=1;

Language.pWrite = pLoadedLanguage;
return NewLanguage;
}

Or something to that effect!!

-JKop
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.