# syntax question

## Recommended Posts

some odd syntax ive seen but not sure what it does. 1) int foo() = 0; 2) int foo( int a=5 ){ //stuff } 3) class foo { int a; foo a=5:(){ //stuff } // not sure if i even typed this one right... }

##### Share on other sites
1. That's a declaration of a "pure virtual function".
2. That's a declaration of a function with a "default argument".
3. That looks kind of like a (mistyped) example of an "initializer list".

Look up each of those in your C++ book.

##### Share on other sites
the second one you listed (as Sneftel pointed out) is a default argument. anway, basically it gives you an option on weather or not to send that argument, and if you dont, it will be assigned to the default. example:

void Test(int x = 0){   cout << x << endl;}

usage:
Test(5); //prints out 5Test(); //prints out 0Test(40); // prints out 40

##### Share on other sites
1. Forces a virtual function to be derived to be used. Ie. you can't call it in a base class. The function must be declared virtual or it won't compile:

class CBase {
public:
CBase() {
};

virtual void Function() = 0; //Can't call it from a CBase variable
};

class CDeriv {
public:
CDeriv() {
};
void Function() {
cout << "Stuff\n";
}
};

2. That sets a default value
3. You typed it wrong. Anyway, it sets the value of the variable.

class foo {
int a;
foo():a = 5 {
};
};

##### Share on other sites
I see... Thank you very much for all your replies.

I was looking at the Enginuity articles here on GameDev
and these kept popping up in his code, and i wasn't sure what
they did, but now i understand.
Thanks.

##### Share on other sites
Quote:
 Original post by MetaCipher3. You typed it wrong. Anyway, it sets the value of the variable.class foo { int a; foo():a = 5 { };};

You did too.

"foo() : a(5) {};"

The whitespace doesn't matter of course, but you don't use assignment statements in the initializer list - instead it uses that constructor-call-like syntax.

##### Share on other sites
Quote:
Original post by Zahlman
Quote:
 Original post by MetaCipher3. You typed it wrong. Anyway, it sets the value of the variable.class foo { int a; foo():a = 5 { };};

You did too.

"foo() : a(5) {};"

The whitespace doesn't matter of course, but you don't use assignment statements in the initializer list - instead it uses that constructor-call-like syntax.

Whoops, you are right. I was in a rush when I replied to this. You use paranthesis to assign values in the construct not an equal sign. (Like Zahlman has it)

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627701
• Total Posts
2978709

• 21
• 14
• 12
• 10
• 12