Sign in to follow this  
KulSeran

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 this post


Link to post
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 this post


Link to post
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 5
Test(); //prints out 0
Test(40); // prints out 40

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by MetaCipher
3. 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 this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by MetaCipher
3. 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)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this