returning value from constructors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kavya

    returning value from constructors

    I was reading a book Test Your C++ Skills by Yashwant Kanetkar. There
    was a question in it

    Ques: Why constructors do not have return values?
    Ans :Constructors are called whenever an object is created. And there
    can never exist a situation where we want to return a value at the time
    of creation of an object.

    I don't understand why author says that.
    What if I want to check whether the object is successfully created. We
    could have checked that by returning some value and finding it.

  • Jacek Dziedzic

    #2
    Re: returning value from constructors

    Kavya wrote:
    What if I want to check whether the object is successfully created. We
    could have checked that by returning some value and finding it.
    That's what you use exceptions for.

    - J.

    Comment

    • Alf P. Steinbach

      #3
      Re: returning value from constructors

      * Kavya:
      I was reading a book Test Your C++ Skills by Yashwant Kanetkar. There
      was a question in it
      >
      Ques: Why constructors do not have return values?
      Ans :Constructors are called whenever an object is created. And there
      can never exist a situation where we want to return a value at the time
      of creation of an object.
      >
      I don't understand why author says that.
      Most probably because of a lack of imagination & experience.

      What if I want to check whether the object is successfully created. We
      could have checked that by returning some value and finding it.
      Yes, but C++ is designed -- intentionally and/or perhaps just as a
      consequence of Doing The Right Thing -- for using exceptions for this.
      When a constructor fails, let it throw an exception. That way there
      will never be any uninitialized, unusable objects around.

      If you will, read the last sentence again.

      The real reason why C++ constructors don't have return values, other
      than the object created, is that that would force a much less convenient
      and much less safe, not to mention much less efficient, syntax for
      calling constructors. Consider:

      std::cout << std::string( 40, ' ' ) << std::endl;

      What if this std::string constructor returned a bool, say? Instead of
      the single line above you'd have to do something like

      {
      std::string spaces; // Not initialized in this hypothetical C++.
      if( !create( spaces, 40, ' ' ) )
      {
      std::runtime_er ror ex;
      if( !create( ex, "Bah!" ) ) { std::terminate( ); }
      throw ex;
      }
      else
      {
      try
      {
      std::cout << spaces << std::endl;
      destroy( spaces );
      }
      catch( std::exception& )
      {
      destroy( spaces );
      throw;
      }
      }
      }

      Argh!



      --
      A: Because it messes up the order in which people normally read text.
      Q: Why is it such a bad thing?
      A: Top-posting.
      Q: What is the most annoying thing on usenet and in e-mail?

      Comment

      • Frederick Gotham

        #4
        Re: returning value from constructors

        Kavya:
        What if I want to check whether the object is successfully created. We
        could have checked that by returning some value and finding it.
        Two Strategies:

        (1) If construction fails, throw an exception.

        class MyClass {
        public:

        MyClass(int const i)
        {
        if (42==i) throw -1;
        }
        };

        (2) Check the state after construction:

        MyClass obj;

        if ( !obj.IsOpen() ) return -1;

        --

        Frederick Gotham

        Comment

        • Daniel T.

          #5
          Re: returning value from constructors

          "Kavya" <Lerner84@gmail .comwrote:
          I was reading a book Test Your C++ Skills by Yashwant Kanetkar. There
          was a question in it
          >
          Ques: Why constructors do not have return values?
          Ans :Constructors are called whenever an object is created. And there
          can never exist a situation where we want to return a value at the time
          of creation of an object.
          >
          I don't understand why author says that.
          What if I want to check whether the object is successfully created. We
          could have checked that by returning some value and finding it.
          If a constructor returned a bool (true for success and false for failure
          to create say,) then what would the following line of code mean?

          MyClass myObject;

          After the line, does 'myObject' exist or not?

          --
          To send me email, put "sheltie" in the subject.

          Comment

          • Jim Langston

            #6
            Re: returning value from constructors

            "Kavya" <Lerner84@gmail .comwrote in message
            news:1162112088 .670231.316860@ f16g2000cwb.goo glegroups.com.. .
            >I was reading a book Test Your C++ Skills by Yashwant Kanetkar. There
            was a question in it
            >
            Ques: Why constructors do not have return values?
            Ans :Constructors are called whenever an object is created. And there
            can never exist a situation where we want to return a value at the time
            of creation of an object.
            >
            I don't understand why author says that.
            What if I want to check whether the object is successfully created. We
            could have checked that by returning some value and finding it.
            Okay, say you have a constructor that returns some val. So what does this
            do?

            MyClass MyInstance; // default constructor
            MyClass = MyClass( 10 );

            A constructor can be thought of as returning a value, the class itself.
            Anythign else and how could you do code like that?


            Comment

            • Victor Bazarov

              #7
              Re: returning value from constructors

              Jim Langston wrote:
              [..]
              A constructor can be thought of as returning a value, the class
              itself. [..]
              I think it's better to say that it returns *an instance* of the
              class rather than "the class".

              V
              --
              Please remove capital 'A's when replying by e-mail
              I do not respond to top-posted replies, please don't ask


              Comment

              Working...