What is the equivalent of "return null" in cpp?

This is a discussion on What is the equivalent of "return null" in cpp? within the C++ Programming forums, part of the General Programming Boards category; Hi! I'm trying to learn c++ coming from Java. Could someone explain to me the way this works in c++? ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    101

    Question What is the equivalent of "return null" in cpp?

    Hi!

    I'm trying to learn c++ coming from Java.
    Could someone explain to me the way this works in c++?

    I have a function returning an object and if I can't construct the object I want to do what in Java would be return null.

    Here is the minimal working example that I would like to get to work.
    Code:
    #include <string>
    using namespace std;
    
    string readObjects(string line){
        return 0;
    }
    
    int main (int argc, char * const argv[]) {
        readObjects(s);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,255
    You can return an empty string to indicate an error. A better choice would probably be to throw an exception though.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you want to return null, you have to do what Java does and return a pointer.
    But that's not usually the best idea. Just do what bithub suggested.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ equivalent for C#'s "sealed override"?
    By m37h0d in forum C++ Programming
    Replies: 56
    Last Post: 12-19-2009, 12:20 AM
  2. creating a "to the power" equivalent in c
    By Irony in forum C Programming
    Replies: 16
    Last Post: 10-06-2009, 11:46 AM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM
  5. Equivalent of Java's "super" keyword
    By JasonLikesJava in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2002, 11:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21