CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2009
    Posts
    2

    Question Why Compiler modifies constructor to const method?

    Can any one help me in understanding why compiler implicitly coverts the constructors of any class to const method.

    class motor
    {
    motor()
    {

    }
    ...
    ...
    };

    compiler modifes the constructor motor() to,

    class motor
    {
    motor() CONST
    {

    }

    }


    Kindly reply what is the advantage compiler provides to user by doing so.
    Thanks in advance.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Why Compiler modifies constructor to const method?

    Quote Originally Posted by nagesh_534 View Post
    Can any one help me in understanding why compiler implicitly coverts the constructors of any class to const method.

    class motor
    {
    motor()
    {

    }
    ...
    ...
    };

    compiler modifes the constructor motor() to,

    class motor
    {
    motor() CONST
    {

    }

    }


    Kindly reply what is the advantage compiler provides to user by doing so.
    Thanks in advance.
    Are you sure you mean compiler, and not editor?

    It doesn't really mean anything for a constructor to be const, so I don't know what to make of it.

  3. #3
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Why Compiler modifies constructor to const method?

    Also, upper-case CONST is not a C++ keyword.
    My hobby projects:
    www.rclsoftware.org.uk

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Why Compiler modifies constructor to const method?

    Quote Originally Posted by nagesh_534 View Post
    Kindly reply what is the advantage compiler provides to user by doing so.
    A compiler doesn't modify any source code. A compiler's job is to create intermediate (object) code from the source code.

    Anyway, there is no such keyword as "CONST".

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Nov 2009
    Posts
    2

    Re: Why Compiler modifies constructor to const method?

    I deliberately made const as CAPTICAL, so that it catch eyes very quickly.Nothing other than that.

    Hi Paul Makenzie, thanks for your reply.
    I agree compiler never inserts any additional code into the source code directely. This is basic. But it inserts some code intelegently into the intermediate code which is benificial for either compiler itself to do it's job smoothly or for the user who forget to provide trival things.

    If you know the ans for my posted question, kindly share it to me. Thanks in advance.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Why Compiler modifies constructor to const method?

    Quote Originally Posted by nagesh_534 View Post
    I agree compiler never inserts any additional code into the source code directely. This is basic. But it inserts some code intelegently into the intermediate code which is benificial for either compiler itself to do it's job smoothly or for the user who forget to provide trival things.
    Are you just making this up because you think this is what's happening? Where is your proof of this? What compiler are you referring to?

    A const function in C++ means that the object cannot change the value of any of its members. What sense does it make to have a constructor const, when constructors are responsible for initializing members, and anything else to set members to a certain value?

    Regards,

    Paul McKenzie

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured