returning a const char*

Discussion in 'C++' started by Simon, May 24, 2004.

  1. Simon

    Simon Guest

    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
    Simon, May 24, 2004
    #1
    1. Advertising

  2. "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
    John Harrison, May 24, 2004
    #2
    1. Advertising

  3. Simon

    Simon Guest

    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.
    Simon, May 24, 2004
    #3
  4. "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
    John Harrison, May 24, 2004
    #4
  5. Simon

    Sims Guest


    > >
    > > 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
    Sims, May 24, 2004
    #5
  6. "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
    John Harrison, May 24, 2004
    #6
  7. Simon

    Sims Guest

    > >
    >
    > 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
    Sims, May 24, 2004
    #7
  8. Simon

    JKop Guest

    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
    JKop, May 24, 2004
    #8
  9. Simon

    JKop Guest

    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
    JKop, May 24, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Matthews
    Replies:
    5
    Views:
    2,372
    tom_usenet
    Aug 2, 2004
  2. Santa
    Replies:
    1
    Views:
    1,061
    Mark A. Odell
    Jul 17, 2003
  3. Replies:
    24
    Views:
    814
    Netocrat
    Oct 30, 2005
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,012
    Ian Collins
    May 9, 2006
  5. Javier
    Replies:
    2
    Views:
    547
    James Kanze
    Sep 4, 2007
Loading...

Share This Page