Saturday, 15 June 2013

methods - Difference between }; and } in C++ -



methods - Difference between }; and } in C++ -

brand new c++.

woking on project assignment, , in illustration code have found methods ending }; instead of typical (expected) }

for example:

circbuffer::circbuffer() { cout<<"constructor called\n"; cout<<"buffer has " << buffersize << "elements\n"; (int = 0; i<= buffersize -1; i++) { buffer[i] = 0; } readin = writein = 0; setdelay(0); }; // <=== here

i can't find info why done online.

thanks, lewis

that trailing ; in namespace scope constitutes empty declaration. have in above code seen compiler as

circbuffer::circbuffer() { ... } // <- `circbuffer::circbuffer` definition ends here ; // <- empty declaration declares nil

i.e. method definition not end }; compiler's point of view. ends }, , ; treated separately , independently.

empty declaration illegal in original version of c++ , in c++03, legalized in c++11. code quoted above hence invalid in c++98 , c++03, legal in c++11. however, c++98 compilers supported empty declarations non-standard extension.

note above applies out-of-class function definitions (as in example). in-class fellow member function definitions trailing ; has been legal (and optional)

class c { c() { ... }; // <- ';' not required, legal in c++98 };

(in case optional ; part of fellow member definition, meaning definition indeed end in }; , not introduce empty declaration.)

when see in actual code, bad habit, maybe based on confusion between in-class , out-of-class definition contexts.

c++ methods

No comments:

Post a Comment