Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > returning a const char*

Reply
Thread Tools

returning a const char*

 
 
Simon
Guest
Posts: n/a
 
      05-24-2004
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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-24-2004

"Simon" <> wrote in message
news:...
> 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


 
Reply With Quote
 
 
 
 
Simon
Guest
Posts: n/a
 
      05-24-2004
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.


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-24-2004

"Simon" <> wrote in message
news:...
> 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


 
Reply With Quote
 
Sims
Guest
Posts: n/a
 
      05-24-2004

> >
> > 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


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-24-2004

"Sims" <> wrote in message
news:...
>
> > >
> > > 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


 
Reply With Quote
 
Sims
Guest
Posts: n/a
 
      05-24-2004
> >
>
> 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


 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      05-24-2004
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
 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      05-24-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Const/non-const pointer returning method Jens Thoms Toerring C++ 11 05-26-2010 04:51 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57