-
November 17th, 2009, 07:37 AM
#1
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.
-
November 17th, 2009, 09:18 AM
#2
Re: Why Compiler modifies constructor to const method?
Originally Posted by nagesh_534
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.
-
November 17th, 2009, 09:36 AM
#3
Re: Why Compiler modifies constructor to const method?
Also, upper-case CONST is not a C++ keyword.
-
November 17th, 2009, 10:06 AM
#4
Re: Why Compiler modifies constructor to const method?
Originally Posted by nagesh_534
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
-
November 17th, 2009, 10:30 PM
#5
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.
-
November 17th, 2009, 10:53 PM
#6
Re: Why Compiler modifies constructor to const method?
Originally Posted by nagesh_534
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|