tags:

views:

164

answers:

5

The following code compiles and runs perfectly,

#include <iostream>

class sam {
    public:
        void func1();
        int func2();
};

int main() {
    sam s;
}

Should it not produce a error for the lack of class member definition?

+4  A: 

Yes it is perfectly valid to not define a class member function if it is not used. That is also true for non-member functions. A definition is required for virtual functions, though. But pure virtuals can omit definitions if they aren't used.

"Used", btw, does not include refering to the function within sizeof. In other words, this is still valid:

sizeof (s.func2()); // still not used!
Johannes Schaub - litb
I'll +1 once I get me votes back. :)
GMan
@GMan ohh thanks!
Johannes Schaub - litb
@GMan: Did you misplace them? `:)` `<goes off to find some votes lying around>`
sbi
@sbi, how do you get the code highlighting here in comments?
Gollum
Though, its usually not valid for virtual member functions.
nos
@sbi: Haha. And hm, I just noticed if I run out of votes that I can no longer even up-vote comments. :| @Gollum: On SO, you can mark inline code by surrounding it with backticks: \`code\` results in `code`. I escaped the former backticks with a backslash.
GMan
@GMan: Yeah I know this well enough, I was just joking. (Sometimes I find I have used up all my votes for the day when I come back from my lunch break. `:(` A really bad day is when I have hit the rep cap by that time, too. Then there's nothing to do but working until the evening. `<sheepish_grin>`)
sbi
Oh, and I didn't know about the backslash escaping. Thanks for sharing that...
sbi
Gollum
@GMan ohh! i learn something new about this SO engine everyday. Looks like SO needs a standard spec
Johannes Schaub - litb
@Gollum actually there was a SO developer meeting. But it was too far away from FF/M germany for me to join -.-
Johannes Schaub - litb
@Gollum: I'd come if you pay for the flight. `:-x`
sbi
@Johannes: Say, you didn't know you can run out of votes? Reeeaaallllyy? Didn't you do your duties eagerly enough?!
sbi
Oh, and I noticed a bug: "@sbi)" doesn't showing up in my responses, while "@sbi:" does.
sbi
@sbi ohh nono i meant about teh backslashes! xD
Johannes Schaub - litb
@Johannes: Thanks to a recently learned feature of SO I now know that it took you more than 15mins to find this excuse. `:)`
sbi
+8  A: 

If you don't call the member functions, they don't have to be defined. Even if you call them, the compiler won't complain since they could be defined in some other compilation unit. Only the linker will complain. Not defining functions is accepted and common to force an error for undesired behavior (e.g. for preventing copying).

Philipp
The last sentence should note that it does this be forcing an error for undesired behavior.
James Curran
@James Curran: Thanks, added.
Philipp
+2  A: 

You can define it elsewhere (like another file)

void Sam::func1()
{
    // do stuff here
}
Michael J
A: 

You did define declared it. You just didn't add implementation provided a function prototype. The linker may display a warning if you do not have a function definition (for instance, Borland C++ Builder 5 does not).

0A0D
The terminology is wrong, here. He *declared* them, but certainly didn't *define* them (which would be giving them an implementation).
GMan
that's called declaration.
Gollum
See this: [What is the difference between a definition and a declaration?](http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration/1410632#1410632)
sbi
The terminology is correct now (though strictly speaking there's no such thing as a prototype.) But now you're just back to his question. :) *Why* is it legal to only declare functions, and never define them?
GMan
@GMan: These aren't prototypes? http://opencbp.sourceforge.net/en_US.ISO8859-1/books/opencbook/func.prototypes.html
0A0D
declaration can only help the code compile, it's linking phase when the **linker** looks for actual definition for that declaration(prototype), and it does so only if it encounters some code piece using that function. You don't get charged for what you do not use.
Gollum
@0A0D: I mean since we were talking technical terminology, that "prototype" is an unofficial way of saying "declaration without definition". The language isn't technically defined in terms of prototypes, because saying "declaration" is enough.
GMan
@GMan: Sure, but it is still a prototype nonetheless. Just like there is no such thing as an Apple Class but I can create it (there are classes though just as there are declarations without definition) :)
0A0D
+1  A: 

From MSDN Linker Error LNK2019

Dave18
@Dave18, you get link error only if you are **actually** using it in the client code, otherwise you would do just fine.
Gollum
Yes, It is true.
Dave18