Click here to Skip to main content
16,016,669 members
Please Sign up or sign in to vote.
4.67/5 (3 votes)
See more:
Hello people!

When I need implement my class object initialization I can do it in several stages. I can initialize in constructors body:

C++
...
anyClassConstructor::anyClassConstructor()
{
     anyMemberVariable = anyValue;
}
...


Either in initialization stage:

C++
...
anyClassConstructor::anyClassConstructor():anyMemberVariable(anyValue)
{
}
...


But I have seen, that after implement constructor initialization in initialization stage, there was semicolon in the end.

C++
...
anyClassConstructor::anyClassConstructor():anyMemberVariable(anyValue)
{
}; // why there is semicolon?
...

As I know, semicolon is need after class declaration, member method declaration (prototypes) or to end line. But there is semicolon like after class declaration, and there is after constructor implement. Why? Is it any specific thing? After my test it's work with semicolon and without it, no errors, no warnings, no bugs? Any effect?

Any ideas? Thanks...
Posted
Updated 14-Jan-12 1:43am
v5
Comments
Satheesh1546 14-Jan-12 10:53am    
I think there is no need of that semicolon there..
MNMR 14-Jan-12 12:45pm    
I think too. And I think, that is no important where to place semicolon. Important is that to write where is need, and anywhere else don't effect. I just saw example like my one in the bottom with semicolon and I thought that is a bug, but when I tested, everything worked. So this is a question...
Sergey Alexandrovich Kryukov 14-Jan-12 13:24pm    
Wrong.
--SA
Sergey Alexandrovich Kryukov 14-Jan-12 15:49pm    
Why contaminate the site with all those guessing? It it too difficult to address to the standard?
I answered this particular question, please see if you are still interested.
--SA
MNMR 14-Jan-12 16:09pm    
Thanks :) You really helped to me!!! Btw your links are very useful!

1 solution

The semicolon after the function definition is not needed. So why your compiler tolerates it?
What you see here was not allowed in C++03, but C++11 introduced so called empty declaration, please see:
http://en.wikipedia.org/wiki/C%2B%2B0x[^],
http://www.open-std.org/jtc1/sc22/wg21/[^],
http://en.cppreference.com/[^].

So, what you observe in your case is interpreted as the empty declaration. Apparently, using this semicolon makes your code just a bit less portable.

Please see: https://developer.mozilla.org/en/C++_Portability_Guide#Don%27t_put_extra_top-level_semi-colons_in_code[^].

However, you should not assume an unwanted semicolon cannot spoil the code. It can. There is a number of cases where it is not allowed, quite apparently. For your own good, write only the elements of the code which have some purpose and only those you perfectly understand.

Best wishes,
—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900