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. Returning a refrence from a function

Returning a refrence from a function

Scheduled Pinned Locked Moved C / C++ / MFC
databasehelpquestion
7 Posts 4 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.
  • A Offline
    A Offline
    Abhishek Narula
    wrote on last edited by
    #1

    Hi there , I have a class lets say CMyClass, it contains a member variable of type CArray of lets say int. Now I have a public function of this Class GetValue(int nIndex) now this function returns the Reference of int which is actually a reference to a particular index of my CArray type member variable. Now here is the problem .. My GetValue function performs some checks before returning the Refrence . now in case the checks fails I dont want to return the reference .. in that case WHAT DO I RETUNR FROM MY CODE. does any body understand what I am trying to ask ? Abhishek Narula "Learn to appreciate others ... World would appreciate you"

    J M 2 Replies Last reply
    0
    • A Abhishek Narula

      Hi there , I have a class lets say CMyClass, it contains a member variable of type CArray of lets say int. Now I have a public function of this Class GetValue(int nIndex) now this function returns the Reference of int which is actually a reference to a particular index of my CArray type member variable. Now here is the problem .. My GetValue function performs some checks before returning the Refrence . now in case the checks fails I dont want to return the reference .. in that case WHAT DO I RETUNR FROM MY CODE. does any body understand what I am trying to ask ? Abhishek Narula "Learn to appreciate others ... World would appreciate you"

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      Throw an exception. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      A 1 Reply Last reply
      0
      • A Abhishek Narula

        Hi there , I have a class lets say CMyClass, it contains a member variable of type CArray of lets say int. Now I have a public function of this Class GetValue(int nIndex) now this function returns the Reference of int which is actually a reference to a particular index of my CArray type member variable. Now here is the problem .. My GetValue function performs some checks before returning the Refrence . now in case the checks fails I dont want to return the reference .. in that case WHAT DO I RETUNR FROM MY CODE. does any body understand what I am trying to ask ? Abhishek Narula "Learn to appreciate others ... World would appreciate you"

        M Offline
        M Offline
        markkuk
        wrote on last edited by
        #3

        Throw an exception. There's no reference equivalent of a NULL pointer, so you must either return a valid reference or throw.

        A 1 Reply Last reply
        0
        • J Joaquin M Lopez Munoz

          Throw an exception. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          A Offline
          A Offline
          Abhishek Narula
          wrote on last edited by
          #4

          Hmm that seems a better idea .. please throw some light on thoring exceptions also .. Abhishek Narula "Learn to appreciate others ... World would appreciate you"

          J A 2 Replies Last reply
          0
          • M markkuk

            Throw an exception. There's no reference equivalent of a NULL pointer, so you must either return a valid reference or throw.

            A Offline
            A Offline
            Abhishek Narula
            wrote on last edited by
            #5

            Wonderfull Idea !! thanks .. please tell me more about throwing exceptions also .. Abhishek Narula "Learn to appreciate others ... World would appreciate you"

            1 Reply Last reply
            0
            • A Abhishek Narula

              Hmm that seems a better idea .. please throw some light on thoring exceptions also .. Abhishek Narula "Learn to appreciate others ... World would appreciate you"

              J Offline
              J Offline
              Joaquin M Lopez Munoz
              wrote on last edited by
              #6

              Essentially, you just throw with throw, which interrupts the normal flow of execution and throws an object that can be catched by some point up the call stack with catch. Just read your docs for throw and catch, and/or search some tutorial on the net, sure there're many out there. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

              1 Reply Last reply
              0
              • A Abhishek Narula

                Hmm that seems a better idea .. please throw some light on thoring exceptions also .. Abhishek Narula "Learn to appreciate others ... World would appreciate you"

                A Offline
                A Offline
                Alexandru Savescu
                wrote on last edited by
                #7

                It goes like this: 1. Create a class CMyException. Here is an example:

                class CMyException
                {
                CString strError;
                public:
                CMyException (CString& a) : strError (a) {}
                void HandleError ()
                {
                AfxMessageBox (strError);
                }
                }

                2. Do this in that function:

                if (index_is_out_of_bounds)
                throw CMyException ("Index out of bounds"); // throw the exception

                3. Catch the exception like this:

                try
                {
                // call the function that may throw the exception
                }
                catch (CMyException& e)
                {
                // caught an exception and now handle it
                e.HandleError ();
                return;
                }

                You can also check MSDN for details or download a good C++ book. "Thinking in C++" from www.bruceeckel.com Best regards, Alexandru Savescu

                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