Theoretical wondering about scope in CPP-files

T

The Cool Giraffe

I was told to use the following scheme.

// Some.h
class Some
{ bool doSome ();};

// Some.cpp
#include "Some.h"
bool Some::doSome () {return true;}

Why do i need to specify the returned type? The scope
Some:: i get, the name of the method too, parameter
type and number - sure thing. But why, oh why, does one
need to specify that doSome returns a bool when
implementing it in the cpp-file? It's unambiguous, isn't
it? Please advise.
 
V

Victor Bazarov

The said:
I was told to use the following scheme.

// Some.h
class Some
{ bool doSome ();};

// Some.cpp
#include "Some.h"
bool Some::doSome () {return true;}

Why do i need to specify the returned type?

The short answer is: because the Standard requires you to.
The scope
Some:: i get, the name of the method too, parameter
type and number - sure thing. But why, oh why, does one
need to specify that doSome returns a bool when
implementing it in the cpp-file? It's unambiguous, isn't
it? Please advise.

A function definition begins with a declaration followed by
a body. The declaration begins with some specifiers followed
by a return value type. That's just a requirement of the
language. The rule of its syntax. It makes it simple, yet
efficient and reduces errors.

Imagine that there would be no need to specify the return value
type. How the hell should I know what type it returns when
looking at the function definition in the 'cpp' file? I would
have to look it up in the header, right? Extra work. Now I
don't have to, the definition repeats it for me.

What would happen if somebody changed the return value type in
the class definition without ever looking at the implementation
of the function? Suddenly all kinds of problems can arise due
to some conversions or whatever. Now, when the types are not
the same, I get a diagnostic message telling me that, and I
have to go to the definition and change the type there as well.
That's extra protection. That's A GOOD THING(tm).

V
 
Z

Zeppe

The said:
Why do i need to specify the returned type? The scope
Some:: i get, the name of the method too, parameter
type and number - sure thing. But why, oh why, does one
need to specify that doSome returns a bool when
implementing it in the cpp-file? It's unambiguous, isn't
it? Please advise.

In addition to what Victor wrote, please consider that at the beginning
there was the C language, in which there where no classes but only
functions. Any function needs to be defined - that is, have the
declaration and the implementation - at some point in the program, and
this declaration tells everything about the function, because it's the
minimum requirement for a function to be used. Eventually, you can also
spread a little bit of additional definition of the same function,
reporting just the name, return type and argument, to enable other
translation units to use it.

Now, should the member functions in C++ introduce some sort of different
syntax just because in that specific case the function definition
inside of the class definition is mandatory? It would have been rather
stupid, additionally complex, error prone, and so on. Just to save 8
bytes in average? ^^

Regards,

Zeppe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top