dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: constructor function

  1. #1
    Join Date
    Jul 2007
    Posts
    249

    constructor function

    Hi,
    I have a class like

    class Test{

    public:
    Test* fun()
    {
    return new Test; // I also can write that return new Test()
    }
    };



    what is the difference between both the return statements.

    Is there any thing called constructor function in C++

    Can someone explain please
    Thanks

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: constructor function

    Quote Originally Posted by Rajesh1978
    Hi,
    I have a class like

    class Test{

    public:
    Test* fun()
    {
    return new Test; // I also can write that return new Test()
    }
    };



    what is the difference between both the return statements.

    Is there any thing called constructor function in C++

    Can someone explain please
    Thanks
    They do the same thing it seems. Omitting the parenthesis just means it implicitly gets default constructed.

    AFAIK, the only time you can call a constructor explicitly is via an initialiser list:
    Code:
    #include <iostream>
    
    class Test
    {
    public:
      Test()
      {
        std::cout << "Base constructor." << std::endl;
      }
    };
    
    class Der_Test : public Test
    {
    public:
      Der_Test() : Test()
      {
        std::cout << "Derived constructor." << std::endl;
      }
    };
    
    int main()
    {
      Der_Test test;
      
      return 0;
    }
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  3. #3
    Join Date
    May 2008
    Posts
    96

    Re: constructor function

    People tend towards the first because it can be used to declare variables
    Code:
    Test testing123;
    whereas the latter looks like a function prototype to the compiler
    Code:
    Test testing123();
    --> error: function "Test testing123()" declared but not defined.

  4. #4
    Join Date
    May 2007
    Posts
    811

    Re: constructor function

    Actually there is a difference, look here, specially 10.2 section.

  5. #5
    Join Date
    Jul 2007
    Posts
    249

    Re: constructor function

    I went through the link provided

    But found nothing extra

    there you can find

    suppose you have a class called List then

    void f()
    {
    List x; // Local object named x (of class List)
    ...
    }

    and
    void g()
    {
    List x(); // Function named x (that returns a List)
    ...
    }

    which is different in my case

    Please see my first post

    I clearly mension that during creation of a new object.

    By the way what is the tag to use to get a well formated code in this forum
    is this <code>.......</code>
    thanks

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: constructor function

    Quote Originally Posted by Rajesh1978
    By the way what is the tag to use to get a well formated code in this forum
    is this <code>.......</code>
    thanks
    Change "<" and ">" to "[" and "]" and you've got it.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #7
    Join Date
    May 2008
    Posts
    96

    Re: constructor function

    @STLdude
    Actually there is a difference, look here, specially 10.2 section.
    How is that any different than what I said?

    @Rajesh
    You did not clearly mention anything about function prototypes in your first post. You only asked about the difference with and without () when using operator new. To which you have been given a complete answer -- they are the same.
    Last edited by Duoas; July 2nd, 2008 at 05:49 AM.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: constructor function

    You only asked about the difference with and without () when using operator new. To which you have been given a complete answer -- they are the same.
    From my reading of the C++ Standard, I'd say that they are technically different since with the parentheses it is value initialisation and without it is default initialisation, but they effectively do the same thing in this case.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    May 2007
    Posts
    811

    Re: constructor function

    Quote Originally Posted by Duoas
    @STLdude

    How is that any different than what I said?

    @Rajesh
    You did not clearly mention anything about function prototypes in your first post. You only asked about the difference with and without () when using operator new. To which you have been given a complete answer -- they are the same.
    You said
    People tend towards the first because it can be used to declare variables
    which is not correct. If you don't get section 10.2, then I wont' be able to explain any better.
    Just to reiterate again:
    Code:
    List x;     // Local object named x (of class List)
    List x();   // Function named x (that returns a List)

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: constructor function

    Quote Originally Posted by STLDude
    which is not correct. If you don't get section 10.2, then I wont' be able to explain any better.
    STLDude, you and Duoas are saying the same thing where it concerns syntax. Duoas' statement is misleading because of the "people tend towards" (when actually it is a matter of choosing the appropriate syntax for the desired construct), but I am very sure that Duoas understands the difference as mentioned in section 10.2 of the FAQ that you linked to.

    However, section 10.2 of that FAQ does not answer the question that Rajesh1978 is asking. Rajesh1978 is asking about the difference between a new expression with the new initialiser omitted and one for which the new initialiser is an empty pair of parentheses. The answer, according to the 2003 edition of the C++ standard, is that the former results in default initialisation if the type is a non-POD class type, or the creation of an object with indeterminate value otherwise. The latter results in value initialisation.

    Now, from Rajesh1978's example, Test is POD class, therefore default initialisation results in zero initialisation. Since there is no user declared constructor, value initialisation results in value initialisation of non-static data members and base class components. However, as there are no member variables, there is nothing to initialise anyway, so the effect must be the same either way.
    Last edited by laserlight; July 2nd, 2008 at 11:03 AM. Reason: Oops, Test is a POD class.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    Join Date
    Jul 2007
    Posts
    249

    Re: constructor function

    However, section 10.2 of that FAQ does not answer the question that Rajesh1978 is asking. Rajesh1978 is asking about the difference between a new expression with the new initialiser omitted and one for which the new initialiser is an empty pair of parentheses. The answer, according to the 2003 edition of the C++ standard, is that the former results in default initialisation if the type is a non-POD class type, or the creation of an object with indeterminate value otherwise. The latter results in value initialisation.
    laserlight
    You caught the exact point. This is the thing I was actually looking for. But it was driven in some other way.

    Now I got the solution

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)