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. How to return NULL when return type is user defined

How to return NULL when return type is user defined

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiontutorial
8 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
    Anonymous
    wrote on last edited by
    #1

    Please read this code and the question asked at the end. class Point { private: int x, y; public: Point(); Point(int x, int y); //constructor int getX(); // getter int getY(); // and void setX(int x); // setters void setY(int y); // functions bool operator==(Point p) const; //overloaded == operator ~Point(); // destructor }; class Box { private: Point points[40]; public: Point findPoint(Point p); //what should be the prototype of this function }; Point Box::findPoint(Point p) { Point pTemp; for(int i=0; i<40; ++i) { pTemp = points[i]; if( pTemp == p ) return pTemp; } return; // what to return here ????????????? } Look at the findPoint function. The responsibility of this function is to search in arrary for the Point given and if function finds this Point, return it, otherwise if function does not find the Point given it should return NULL. But I am unable to do it. I can achieve this by changing the prototype of function like this Point *findPoint(Point p); butt I dont want to do this because it will break the conecpts of OOP and give access to private data memeber. The problem is if I return by value then I cant return NULL, so in user class or function, I can not know whether the Point is found or not. and if I return the pointer, then I give direct access to private data member. Anybody help please !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    N N 2 Replies Last reply
    0
    • A Anonymous

      Please read this code and the question asked at the end. class Point { private: int x, y; public: Point(); Point(int x, int y); //constructor int getX(); // getter int getY(); // and void setX(int x); // setters void setY(int y); // functions bool operator==(Point p) const; //overloaded == operator ~Point(); // destructor }; class Box { private: Point points[40]; public: Point findPoint(Point p); //what should be the prototype of this function }; Point Box::findPoint(Point p) { Point pTemp; for(int i=0; i<40; ++i) { pTemp = points[i]; if( pTemp == p ) return pTemp; } return; // what to return here ????????????? } Look at the findPoint function. The responsibility of this function is to search in arrary for the Point given and if function finds this Point, return it, otherwise if function does not find the Point given it should return NULL. But I am unable to do it. I can achieve this by changing the prototype of function like this Point *findPoint(Point p); butt I dont want to do this because it will break the conecpts of OOP and give access to private data memeber. The problem is if I return by value then I cant return NULL, so in user class or function, I can not know whether the Point is found or not. and if I return the pointer, then I give direct access to private data member. Anybody help please !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

      N Offline
      N Offline
      nfactorial
      wrote on last edited by
      #2

      Why are you returning a 'Point' anyway? :) If it's found, they've already supplied the point in the first place, so the return result doesn't give them anything new except another point that matches the one they supplied. Maybe a 'bool HasPoint( Point point )' method may suit your needs, or you could return an integer index into the array (and -1 if it doesn't exist)? If you really do want to return a point for some reason then how about: class Box { private: Point points[40]; public: bool FindPoint( Point search, Point &result ); }; n!

      H 1 Reply Last reply
      0
      • A Anonymous

        Please read this code and the question asked at the end. class Point { private: int x, y; public: Point(); Point(int x, int y); //constructor int getX(); // getter int getY(); // and void setX(int x); // setters void setY(int y); // functions bool operator==(Point p) const; //overloaded == operator ~Point(); // destructor }; class Box { private: Point points[40]; public: Point findPoint(Point p); //what should be the prototype of this function }; Point Box::findPoint(Point p) { Point pTemp; for(int i=0; i<40; ++i) { pTemp = points[i]; if( pTemp == p ) return pTemp; } return; // what to return here ????????????? } Look at the findPoint function. The responsibility of this function is to search in arrary for the Point given and if function finds this Point, return it, otherwise if function does not find the Point given it should return NULL. But I am unable to do it. I can achieve this by changing the prototype of function like this Point *findPoint(Point p); butt I dont want to do this because it will break the conecpts of OOP and give access to private data memeber. The problem is if I return by value then I cant return NULL, so in user class or function, I can not know whether the Point is found or not. and if I return the pointer, then I give direct access to private data member. Anybody help please !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        N Offline
        N Offline
        Neville Franks
        wrote on last edited by
        #3

        You can do several things. First I would change it to: bool findPoint( Point& ptFound, Point p ); Another option is to keep your original code and set the returned Point to a value that would never occur in practice. eg. -1,-1. Then you could write: IsPointValid( ptFound = findPoint( point ) ) You can and should also make the function const. You can also return a const ptr, which doesn't break your OOP rule. Solution 3. Use const wherever possible. There you - pick 1. Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com

        1 Reply Last reply
        0
        • N nfactorial

          Why are you returning a 'Point' anyway? :) If it's found, they've already supplied the point in the first place, so the return result doesn't give them anything new except another point that matches the one they supplied. Maybe a 'bool HasPoint( Point point )' method may suit your needs, or you could return an integer index into the array (and -1 if it doesn't exist)? If you really do want to return a point for some reason then how about: class Box { private: Point points[40]; public: bool FindPoint( Point search, Point &result ); }; n!

          H Offline
          H Offline
          Hashim Saleem
          wrote on last edited by
          #4

          Thanks for the reply and reminding me my mistake. Now consider the situation. what if findPoint function now has this prototype Point Box::findPoint(int x) { Point pTemp; for(int i=0; i<40; ++i) { pTemp = points[i]; if(pTemp.getX() == x) return pTemp; } return; // what to return here. } The purpose of the findPoint function is to find a Point which has its x data member equal to x provided. So, what to return if the if condition not satisfied.

          N 1 Reply Last reply
          0
          • H Hashim Saleem

            Thanks for the reply and reminding me my mistake. Now consider the situation. what if findPoint function now has this prototype Point Box::findPoint(int x) { Point pTemp; for(int i=0; i<40; ++i) { pTemp = points[i]; if(pTemp.getX() == x) return pTemp; } return; // what to return here. } The purpose of the findPoint function is to find a Point which has its x data member equal to x provided. So, what to return if the if condition not satisfied.

            N Offline
            N Offline
            nfactorial
            wrote on last edited by
            #5

            Ahh ok, then I'd pass in a reference to be filled in and return a bool. bool Box::FindPoint( int x, Point &result ) const { for ( int i = 0; i < 40; ++i ) { if ( points[i].GetX() == x ) { result = points[i]; return true; } } return false; } n!

            H 1 Reply Last reply
            0
            • N nfactorial

              Ahh ok, then I'd pass in a reference to be filled in and return a bool. bool Box::FindPoint( int x, Point &result ) const { for ( int i = 0; i < 40; ++i ) { if ( points[i].GetX() == x ) { result = points[i]; return true; } } return false; } n!

              H Offline
              H Offline
              Hashim Saleem
              wrote on last edited by
              #6

              Can we not use Java style here like this this code fragment is of JAVA language Point findPoint(int x); so, in this case we can return null (cause null is a keyword in java) and also user defined types are reference types in java. My question now is isnt there any alternative in C++ to this JAVA style other than provided by you???

              N 1 Reply Last reply
              0
              • H Hashim Saleem

                Can we not use Java style here like this this code fragment is of JAVA language Point findPoint(int x); so, in this case we can return null (cause null is a keyword in java) and also user defined types are reference types in java. My question now is isnt there any alternative in C++ to this JAVA style other than provided by you???

                N Offline
                N Offline
                nfactorial
                wrote on last edited by
                #7

                All objects in Java are reference types, you're not returning by reference you're returning by value here. C++'s class model is different to Java, the closest built in primitive to a Java reference (which may be null, as you said) is a pointer (C++ references may not legally be null), which you said you'd like to avoid. The alternatives I can think of at the moment are: Throw an exception, of course, but that's a bit heavy handed for a failed search :) Return an integer index (with -1 meaning not found) and provide an index operator. Write a RefResult templated wrapper and return one of those, but that doesn't give you anything more than you'd get returning a pointer (except more code). My previous suggestion (returning bool). I can only say that C++ isn't Java and if you prefer the Java style then use Java? :) n!

                H 1 Reply Last reply
                0
                • N nfactorial

                  All objects in Java are reference types, you're not returning by reference you're returning by value here. C++'s class model is different to Java, the closest built in primitive to a Java reference (which may be null, as you said) is a pointer (C++ references may not legally be null), which you said you'd like to avoid. The alternatives I can think of at the moment are: Throw an exception, of course, but that's a bit heavy handed for a failed search :) Return an integer index (with -1 meaning not found) and provide an index operator. Write a RefResult templated wrapper and return one of those, but that doesn't give you anything more than you'd get returning a pointer (except more code). My previous suggestion (returning bool). I can only say that C++ isn't Java and if you prefer the Java style then use Java? :) n!

                  H Offline
                  H Offline
                  Hashim Saleem
                  wrote on last edited by
                  #8

                  Thank you very much "n!" I have decided to use your earliest suggestion (return bool). Your replies were really very much informative to me. I was really confusing Java and C++ memory management. This confusion is lowered very much by your replies. 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