Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. What should be returned by a method that fails to construct the object it has to return ?..
Forum Updated to NodeBB v4.3 + New Features

What should be returned by a method that fails to construct the object it has to return ?..

Scheduled Pinned Locked Moved Solved C++ Gurus
6 Posts 5 Posters 864 Views 3 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by
    #1

    Hello,
    i have a method that should read a JSON file, construct QList of Tool* and return it ,
    i wonder what should i return if the json file is not found. This must be a basic (silly ?) question sry

    QList<Tool*> ToolLoader::loadTools(QString name){
      QFile jsonFile(name);
      if(jsonFile.open(QFile::ReadOnly)){
        // construct a list of Tools and return it
      }else{
        // what to return if file can not be opened ?
      }
    }
    

    Thank you

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      It can throw, it can take a second parameter bool *bOk, it can return an empty list, ... it depends on what you want and define.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      ODБOïO 1 Reply Last reply
      4
      • Christian EhrlicherC Christian Ehrlicher

        It can throw, it can take a second parameter bool *bOk, it can return an empty list, ... it depends on what you want and define.

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #3

        hi @Christian-Ehrlicher thank you for replying
        currently i am returning an empty list, then the caller of the method has to check the list, but i tought it was bad to return anything at all in this case.
        I would like to not return anything
        I will read about exceptions.
        Thank you.

        fcarneyF 1 Reply Last reply
        0
        • ODБOïO ODБOï

          hi @Christian-Ehrlicher thank you for replying
          currently i am returning an empty list, then the caller of the method has to check the list, but i tought it was bad to return anything at all in this case.
          I would like to not return anything
          I will read about exceptions.
          Thank you.

          fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #4

          @LeLev If you have a return type that is an object you have to return that object. If its a pointer you can return nullptr. If its a list you have to return a list (empty if its empty).

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          5
          • A Offline
            A Offline
            Asperamanca
            wrote on last edited by Asperamanca
            #5

            In such cases, I have started using std::optional or a custom sum type which can either return a value or error information, basically a nice wrapper around

            template<class T>
            std::variant<T,MyErrorClass>
            

            The latter has the advantage that you can provide information why no value was returned, and (if you consistently use this pattern) you can pass the "value-or-error" sum type through multiple levels of calls without having to put an "if" statement checking for errors after each call.

            1 Reply Last reply
            4
            • Kent-DorfmanK Offline
              Kent-DorfmanK Offline
              Kent-Dorfman
              wrote on last edited by
              #6

              I always throw on a failed constructor so that the object pointer is left alone and nothing becomes "partially initialized". Being that you are wrapping the object construction in an additional layer, you could throw to your method and record status as a return code. I prefer the following standard for method prototypes where possible.

              status = method(outparam&, inparam, inparam=value);

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved