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.
Re: Why Compiler modifies constructor to const method?
Quote:
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.
Re: Why Compiler modifies constructor to const method?
Also, upper-case CONST is not a C++ keyword.
Re: Why Compiler modifies constructor to const method?
Quote:
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
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.
Re: Why Compiler modifies constructor to const method?
Quote:
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