I've got a question that's been bugging me for a month or so...
I've noticed that when you create a class constructor, and use a list of initializers, or when you declare/define a destructor with no "body" within the class, you don't have to put a semicolon at the end of the line.
(I mean, the compiler doesn't throw a warning/error)
EX:
class product
{
int number;
public:
string name;
product(string text, int nmbr) : name(text), number(nmbr) {} // No semicolon.
~product(){} // also no semicolon.
}
Now, my question is, "Is this legal C++? Or just a perk of using Dev-C++?"
I kind of like not having the semicolons there, but if this is the start of a bad practice, I'll start placing them.
The code runs fine either way, I just wondered if the C++ standards mentioned anything about it.
I appreciate any help you can give me.
Thanks,
—Malciah™
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The way I did it above was defining them, and even though the compiler will let me, I should not place a semicolon at the end. (unless you have some code within the curvy brackets, then you place the semicolon inside the closing bracket)
Actually, putting a semicolon after the line, the way you have written it, is illegal. Most compilers will not bother, but stray ';' are evil.
The problem is in your understanding of declaration versus definition. When you declare a function you must use a semicolon. When you define a function you must not use a semicolon. Don't let the fact the defining a function is, by "virtue" of the crappy declaration syntax, also declaring a function--visible from that point on.
Hello all!
I've got a question that's been bugging me for a month or so...
I've noticed that when you create a class constructor, and use a list of initializers, or when you declare/define a destructor with no "body" within the class, you don't have to put a semicolon at the end of the line.
(I mean, the compiler doesn't throw a warning/error)
EX:
class product
{
int number;
public:
string name;
product(string text, int nmbr) : name(text), number(nmbr) {} // No semicolon.
~product(){} // also no semicolon.
}
Now, my question is, "Is this legal C++? Or just a perk of using Dev-C++?"
I kind of like not having the semicolons there, but if this is the start of a bad practice, I'll start placing them.
The code runs fine either way, I just wondered if the C++ standards mentioned anything about it.
I appreciate any help you can give me.
Thanks,
—Malciah™
Ahhh!!!!
I think I understand now.
The way I did it above was defining them, and even though the compiler will let me, I should not place a semicolon at the end. (unless you have some code within the curvy brackets, then you place the semicolon inside the closing bracket)
Correct?
However, here:
I'm declaring them, and the semicolon belongs. Is this right?
Oh, I just realized that I forgot the semicolon that ends the class earlier. I'm worried about the wrong semicolons it seems
:) Sorry about that.
Actually, putting a semicolon after the line, the way you have written it, is illegal. Most compilers will not bother, but stray ';' are evil.
The problem is in your understanding of declaration versus definition. When you declare a function you must use a semicolon. When you define a function you must not use a semicolon. Don't let the fact the defining a function is, by "virtue" of the crappy declaration syntax, also declaring a function--visible from that point on.
[Code]
struct test
{
void a(); // Declaration
void b(){} // Definition
};
void test::a(){} // Definition
[/Code]