473,378 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Theoretical wondering about scope in CPP-files

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änligen Kerstin Viltersten
(The Cool Giraffe)
Apr 18 '07 #1
2 1584
The Cool Giraffe wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 18 '07 #2
The Cool Giraffe wrote:
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
Apr 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Zeng Dinghao | last post by:
int main() { for (int i=0; i<10; i++) { cout << "dfdfd" << endl; } for (int i=0; i<10; i++) { cout << "dfdfd" << endl; } } when compiling it in Visual C++, i got an error.
6
by: shaun | last post by:
If I put (define) const variables at the top of a .cpp file but do not declare them in the .h file, are they only visible within that .cpp file? e.g. const int a={1,2,3,4}; in a cpp file...
2
by: Bit byte | last post by:
What's wrong with this: //file1.h Class A { public: A(){i=1;} ~A(){;} void doThis(void){;} void doThat(void){;}
4
by: red floyd | last post by:
What is the linkage of a namespace scope typedef? Is it internal or external? 3.5 is unclear on this. In other words, is the following program, consisting of translation units a.cpp and...
4
by: twelvetone | last post by:
Can anyone explain why this file won't compile? The errors I'm getting in VS.Net 2003 are below. ============================ c:\sri\sw\scs\SCS\src\TaskPlans\main.cpp(24): error C3861: 'str':...
6
by: Florian Lindner | last post by:
Hello, I have these class declarations class CBauteil { private: int m_iNumber; int m_iWeight; public: int getNumber(); void setNumber(int);
1
by: chunghorng_lung | last post by:
Hi All, I have a question on the scope of variables for threads. The program has a main thread which creates a few worker threads. The main thread can access another class stored in another...
2
by: Tim H | last post by:
help?!? #include <boost/shared_ptr.hpp> class pp_dirent {}; class pp_scope: public pp_dirent {}; int main() { boost::shared_ptr<pp_direntp1(new pp_scope()); boost::shared_ptr<pp_scopep2 =...
1
by: George | last post by:
Hi All, With gcc (3 or 4) the member template definition out of class scope below compiles fine. But with Sun's CC 5.9, it fails with: "main.cpp", line 7: Error: Could not find a match for...
2
by: jr.freester | last post by:
I have a written a simple class called Matrix. It allows me to operate on a 2D array if it were a matrix. I have not really written a class in c++ since the single course I took in college. In...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.