Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > constructor with/without semicolon

Reply
Thread Tools

constructor with/without semicolon

 
 
thomas
Guest
Posts: n/a
 
      02-17-2008
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--
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-17-2008
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.
 
Reply With Quote
 
 
 
 
Marcel Müller
Guest
Posts: n/a
 
      02-17-2008
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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-17-2008
On Feb 17, 8:57 am, Marcel Müller <(E-Mail Removed)>
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:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Cookies: semicolon vs. semicolon-space Peter Michaux Javascript 3 12-11-2006 04:43 PM
Problem returning identity from SQL Server when string contains semicolon Dan ASP .Net 3 11-16-2005 07:31 PM
"Hello World" without semicolon with a difference ankursinha C Programming 33 02-25-2004 09:27 AM
orgin of semicolon usage Russ C Programming 9 10-26-2003 02:44 AM



Advertisments