Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CString reference

CString reference

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #1

    How can I declare a CString reference into a generic class ? In fact, I want to declare a CString reference that could store errors, but that CString variable to be part of external class (let's say CMyWinApp class) ... it's ilogical request ? I try to do :

    // CMyClass.h
    CString& m_sErrorExt;

    but I get follow error :

    'm_sErrorExt' : must be initialized in constructor base/member initializer list

    D J B 3 Replies Last reply
    0
    • _ _Flaviu

      How can I declare a CString reference into a generic class ? In fact, I want to declare a CString reference that could store errors, but that CString variable to be part of external class (let's say CMyWinApp class) ... it's ilogical request ? I try to do :

      // CMyClass.h
      CString& m_sErrorExt;

      but I get follow error :

      'm_sErrorExt' : must be initialized in constructor base/member initializer list

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Reference and const member variables must be given a value when initialized or in the constructor. Google for examples of how to resolve error C2758.

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

      1 Reply Last reply
      0
      • _ _Flaviu

        How can I declare a CString reference into a generic class ? In fact, I want to declare a CString reference that could store errors, but that CString variable to be part of external class (let's say CMyWinApp class) ... it's ilogical request ? I try to do :

        // CMyClass.h
        CString& m_sErrorExt;

        but I get follow error :

        'm_sErrorExt' : must be initialized in constructor base/member initializer list

        J Offline
        J Offline
        JackDingler
        wrote on last edited by
        #3

        You need to initialize it in the constructor.

        class CMyClass
        {
        public:
        CString m_DefaultErrorExt;
        CString & m_sErrorExt;

        public:
        CMyClass(void) : sErrorExt(DefaultErrorExt) {}
        CMyClass(CString & ExternError) : sErrorExt(ExternError) {}
        };

        J 1 Reply Last reply
        0
        • J JackDingler

          You need to initialize it in the constructor.

          class CMyClass
          {
          public:
          CString m_DefaultErrorExt;
          CString & m_sErrorExt;

          public:
          CMyClass(void) : sErrorExt(DefaultErrorExt) {}
          CMyClass(CString & ExternError) : sErrorExt(ExternError) {}
          };

          J Offline
          J Offline
          JackDingler
          wrote on last edited by
          #4

          With this said, this can be a dangerous practice. If the object your object is linked to, goes out of scope, your code will crash when it tries to access the reference. I don't use this technique often. In my code, my most common usage is when I'm working with legacy code that has really bad variable names. I'll sometimes create a new variable with a meaningful name, and convert the old one to a reference, so that it's still available to code I don't intend to touch.

          _ 1 Reply Last reply
          0
          • J JackDingler

            With this said, this can be a dangerous practice. If the object your object is linked to, goes out of scope, your code will crash when it tries to access the reference. I don't use this technique often. In my code, my most common usage is when I'm working with legacy code that has really bad variable names. I'll sometimes create a new variable with a meaningful name, and convert the old one to a reference, so that it's still available to code I don't intend to touch.

            _ Offline
            _ Offline
            _Flaviu
            wrote on last edited by
            #5

            In my class, I do something that could throw into exceptions, so, I put an string reference in method paramter, like this :

            BOOL CMyClass::Execute(LPCTSTR lpszParam1, CString& sError);

            so, I give up the first request ... I don't know if is good ideea ... thank you all anyway.

            J L 2 Replies Last reply
            0
            • _ _Flaviu

              How can I declare a CString reference into a generic class ? In fact, I want to declare a CString reference that could store errors, but that CString variable to be part of external class (let's say CMyWinApp class) ... it's ilogical request ? I try to do :

              // CMyClass.h
              CString& m_sErrorExt;

              but I get follow error :

              'm_sErrorExt' : must be initialized in constructor base/member initializer list

              B Offline
              B Offline
              bjorn_ht
              wrote on last edited by
              #6

              The way you describe it, that string sounds like an object property - the last error message - so it's better and simpler to let it be a member of your class and add an accessor function for it...

              class MyClass
              {
              public:
              bool execute()
              {
              try
              {
              // ...
              throw exception("Testing testing");
              return true;
              }
              catch(exception& e)
              {
              m_lastError = e.what();
              return false;
              }
              }
              CString& lastError()
              {
              return m_lastError;
              }
              private:
              CString m_lastError;
              };

              void tryIt()
              {
              MyClass c;
              if( c.execute() )
              cout << "All well" << endl;
              else
              cout << "MyClass.execute failed: " << c.lastError().GetString() << endl;
              }

              This is safer because you know the CString goes out of scope at the same time your class does, and you still have a CString reference available to use from the rest of your code exactly as before.

              _ 1 Reply Last reply
              0
              • _ _Flaviu

                In my class, I do something that could throw into exceptions, so, I put an string reference in method paramter, like this :

                BOOL CMyClass::Execute(LPCTSTR lpszParam1, CString& sError);

                so, I give up the first request ... I don't know if is good ideea ... thank you all anyway.

                J Offline
                J Offline
                JackDingler
                wrote on last edited by
                #7

                I don't understand what you're saying there.

                1 Reply Last reply
                0
                • _ _Flaviu

                  In my class, I do something that could throw into exceptions, so, I put an string reference in method paramter, like this :

                  BOOL CMyClass::Execute(LPCTSTR lpszParam1, CString& sError);

                  so, I give up the first request ... I don't know if is good ideea ... thank you all anyway.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Maybe it would help to understand what a reference[^] is.

                  Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                  1 Reply Last reply
                  0
                  • B bjorn_ht

                    The way you describe it, that string sounds like an object property - the last error message - so it's better and simpler to let it be a member of your class and add an accessor function for it...

                    class MyClass
                    {
                    public:
                    bool execute()
                    {
                    try
                    {
                    // ...
                    throw exception("Testing testing");
                    return true;
                    }
                    catch(exception& e)
                    {
                    m_lastError = e.what();
                    return false;
                    }
                    }
                    CString& lastError()
                    {
                    return m_lastError;
                    }
                    private:
                    CString m_lastError;
                    };

                    void tryIt()
                    {
                    MyClass c;
                    if( c.execute() )
                    cout << "All well" << endl;
                    else
                    cout << "MyClass.execute failed: " << c.lastError().GetString() << endl;
                    }

                    This is safer because you know the CString goes out of scope at the same time your class does, and you still have a CString reference available to use from the rest of your code exactly as before.

                    _ Offline
                    _ Offline
                    _Flaviu
                    wrote on last edited by
                    #9

                    Here is what I'm going to do :

                    CMyClass
                    {
                    BOOL Execute(LPCTSTR lpszSQL,CString& sError);
                    }

                    BOOL CMyClass::Execute(LPCTSTR lpszSQL,CString& sError)
                    {
                    try
                    {
                    throw ....
                    }
                    catch(CException* e)
                    {
                    e->GetErrorMessage(sError.GetBufer(255),255);
                    sError->ReleaseBuffer();
                    e->Delete();
                    }
                    }

                    somewhere, along 'Execute' method, might have an error, and want to have method feedback and error description in one code line :

                    CMyClass obj;
                    CString sError;
                    if(! obj.Execute(_T("SELECT * FROM mytable"),sError))
                    {
                    MessageBox(sError);
                    return;
                    }

                    which in fact, it's the same thing like you describe above ... First time, I was thinking that I could setup an external CString reference to have all posible error in CMyClass, but now I see that is not the best idea ... But, one for another, I learn something here because of you guys, and for that I will kindly thank you.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups