Click to See Complete Forum and Search --> : Which is better in C++?


dunno
11-15-2000, 07:12 PM
To declare your classes like this:



class thing {
private:
int x;
public:
void changeX(int);
};

void thing::changeX(int t) {
...
}


or like this:


class thing {
private:
int x;
public:
void changeX(int t) {
...
}
};



[This message has been edited by dunno (edited 15 November 2000).]

binaryDigit
11-15-2000, 09:08 PM
i always like the first one better. it's just easier to read IMHO.

of course if the function is only one line or really short it saves time by doing it the second way.

i don't know if there is some kind of standard for the situation.



------------------
http://home.earthlink.net/~pebice/philLinux.html (http://home.earthlink.net/~pebice)

Strike
11-15-2000, 09:16 PM
I do the first one. Usually I'll put the method declarations in a .cpp file and the class definition in a separate header file. What stinks is that template classes don't allow this for some reason. I have to stick the methods in the same file as the template class definition. This may have been fixed, though.

dunno
11-15-2000, 09:24 PM
Header files in C++:
#include <iostream.h> is better as:
#include <iostream>
Is that true with header files i create?

#include "filename.h" or #include "filename"

Energon
11-15-2000, 09:32 PM
If you put your class functions into your .h file like in the second one, that tells the compiler to attempt to make them inline (afaik)... so you'd want to put your dinky short one liners like that, and your longer ones like the first one... it's better styling IMHO...

#include "filename" says "include 'filename'
#include "filename.h" says "include 'filename.h'

#include <filename> says "include this standard header (I think)"
#include <filename.h> says "include 'filename.h' from my standard include directory"

someone correct me if I'm wrong...
Basically you want your standard C++ library headers (iostream, string, etc) to be w/o the .h and your headers in your include directory (/usr/include) to be with a .h... your personal include files are whatever you want them to be (.givemeafsckingbreak for all the compiler cares), but it has to be the filename or it won't work (so if it has a .h, then you have to put a .h)... and in quotes rather than the gt and lt symbols so the compiler knows "they're in the current directory, not the include directory"

but someone correct me if I'm wrong...

[This message has been edited by Energon (edited 15 November 2000).]

dunno
11-15-2000, 11:11 PM
I gotcha, thanks

Stuka
11-16-2000, 01:48 PM
One other thing about the location of function bodies - if they are in the class definition, as Energon mentioned, they are assumed to be inline (if that's possible). If you want inline functions, but want to hide what they do (for whatever reason), you can use the inline keyword in your function definition and declaration. In my experience (as little as that is! http://www.linuxnewbie.org/ubb/smile.gif) however, the inline functions you use will be pretty obvious, such as getting or setting the state of a private variable, so what's the sense in hiding them?

Glaurung
11-16-2000, 03:43 PM
#include <header> includes the file 'header' and dumps it's contents into the std namespace. #include <header.h> includes the file 'header.h' but (by convention) dumps it's contents into the global namespace.

If you check it out, eg with iostream and iostream.h, iostream is just "namespace std { #include <iostream.h> }"