constructor with/without semicolon

Discussion in 'C++' started by thomas, Feb 17, 2008.

  1. thomas

    thomas Guest

    for the following code, line L can also be written as
    A(int i):data(i){}; // L1
    A(int i):data(i){} // L2

    As I know, L2 works because it's a function;
    but how can L1 work? It seems to be a declaration.

    ---code---
    #include<iostream>
    using namespace std;

    class A{
    int data;
    public:
    A(int i):data(i){} // L
    void B(){
    cout<<data<<endl;
    }
    };

    int main(){
    A(3).B();
    }

    ---code--
    thomas, Feb 17, 2008
    #1
    1. Advertising

  2. thomas

    Ian Collins Guest

    thomas wrote:
    > for the following code, line L can also be written as
    > A(int i):data(i){}; // L1
    > A(int i):data(i){} // L2
    >
    > As I know, L2 works because it's a function;
    > but how can L1 work? It seems to be a declaration.
    >

    No, the semicolon is simply superfluous.

    --
    Ian Collins.
    Ian Collins, Feb 17, 2008
    #2
    1. Advertising

  3. thomas schrieb:
    > for the following code, line L can also be written as
    > A(int i):data(i){}; // L1
    > A(int i):data(i){} // L2
    >
    > As I know, L2 works because it's a function;
    > but how can L1 work? It seems to be a declaration.


    A constuctor/destructor is a function too. A Function without a return
    value of course. And like outside a class definition a function
    implementation requires no semicolon. Compare it to:

    A::A(int i)
    : data(i)
    { // empty
    }
    ^ Here is usually no semicolon too.


    Marcel
    Marcel Müller, Feb 17, 2008
    #3
  4. thomas

    James Kanze Guest

    On Feb 17, 8:57 am, Marcel Müller <>
    wrote:
    > thomas schrieb:


    > > for the following code, line L can also be written as
    > > A(int i):data(i){}; // L1
    > > A(int i):data(i){} // L2


    > > As I know, L2 works because it's a function;
    > > but how can L1 work? It seems to be a declaration.


    > A constuctor/destructor is a function too. A Function without a return
    > value of course. And like outside a class definition a function
    > implementation requires no semicolon. Compare it to:


    > A::A(int i)
    > : data(i)
    > { // empty}


    > ^ Here is usually no semicolon too.


    With a slight difference: a semicolon after a function
    definition outside of the class is illegal. One after a
    function definition inside the class is explicitly permitted.
    (I think most compilers allow one after the function definition
    outside the class as well, as an extension. Logically, it
    creates an empty statement, and the only reasons for forbidding
    empty statements at namespace scope are historical.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Feb 17, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ankursinha
    Replies:
    33
    Views:
    22,287
    Keith Thompson
    Feb 25, 2004
  2. subnet

    Programs without semicolon

    subnet, May 11, 2004, in forum: C Programming
    Replies:
    18
    Views:
    2,934
    avsharath
    Jun 1, 2007
  3. Replies:
    3
    Views:
    351
    Joe Greer
    Oct 10, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,197
  5. Peter Michaux

    Cookies: semicolon vs. semicolon-space

    Peter Michaux, Dec 10, 2006, in forum: Javascript
    Replies:
    3
    Views:
    210
    Michael Winter
    Dec 11, 2006
Loading...

Share This Page