• No results found

1 #include <cstdlib> 2 #include <iostream> 3 4 using namespace std; 5 6 class point 7 { 8 public: 9 point() 10 { 11 x = y = 0; 12 } 13

14 void SetX(int xValue) 15 {

16 x = xValue; 17 }

18 19 int GetX(void) 20 { 21 return (x); 22 } 23

24 void SetY(int yValue) 25 { 26 y = yValue; 27 } 28 29 int GetY(void) 30 { 31 return (y); 32 } 33 34 private: 35 int x, y; 36 }; 37

38 int main(int argc, char *argv[]) 39 { 40 point rightHere; 41 42 rightHere.SetX(10); 43 rightHere.SetY(20); 44

45 cout << "(x,y)=(" << rightHere.GetX(); 46 cout << "," << rightHere.GetY() << ")"; 47 cout << endl; 48 49 rightHere.SetX(20); 50 rightHere.SetY(10); 51

52 cout << "(x,y)=(" << rightHere.GetX(); 53 cout << "," << rightHere.GetY() << ")"; 54 cout << endl; 55 56 system("PAUSE"); 57 return (EXIT_SUCCESS); 58 }

Rather than just prototypes for member functions, the class in Listing 3.5

contains the member functions themselves. All of the code for each function appears in the class. The main() function on lines 3858 demonstrates that your

programs can use inline member functions in exactly the same way they use member functions defined outside of a class.

Note

Member functions whose code appears outside of the class definition are called out-of-line member functions.

You're probably quite logically wondering what, if any, differences exist between inline and out-of-line member functions. There's really only one difference: When you define a member function inline, the compiler

substitutes the code from the inline member function into your program. With out-of-line functions, that doesn't happen. For instance, if you look back at

Listing 3.5, you'll see the statement

rightHere.SetX(10);

on line 42. When this statement gets compiled, the compiler substitutes the code for the SetX() function right into the statement. That is, it puts the

equivalent of

rightHere.x=10;

into the compiled program. Of course, it doesn't change the source code in the .cpp file. It performs the substitution in the object code that it generates. It does this everywhere in every function that calls the inline function. The

program in Listing 3.5 makes one call to the constructor, and two calls each to

SetX() and SetY(). That means it substitutes code from the member functions into

the main() function five times.

Factoid

Dev-C++ puts its object code in .o files. Other compilers, such as Microsoft's Visual C++, put object code into .obj files. Whether the filename ends with .o or .obj doesn't matter. Either way, the file contains object code.

When I was teaching college-level C++ programming classes, students would often ask, "Why not just make all functions inline?"

The first reason is that the compiler won't allow it. If the compiler determines that the function is too long or too complex, it won't compile the member

function as an inline function. It still compiles the function without a problem. However, it converts the member function into an out-of-line function in the compiled code. And it does this without telling you. It is completely up to

whoever writes the compiler whether or not a function can be made inline. You and I can't control it. Writing functions inline is more of a suggestion than a command.

Usually functions remain inline if all they do is set or get values from members in a class. They generally also remain inline if they perform simple

calculations. However, member functions that contain loops or call other functions are not likely to remain inline.

Note

In general, inline member functions should not do much more than get or set the values of member data.

Another reason why it might not be a good idea to make all member functions inline is that inline member functions can make programs very large. Because the C++ compiler performs code substitution with inline member functions, there are fewer function calls in programs so they run faster. However, it also makes them bigger because the code for the inline functions gets inserted repeatedly. Using inline functions means that there are lots of copies of the inline function in the compiled program. With out-of-line functions, the

program jumps to the one and only copy of the member function. That's a tiny bit slower, but it makes the program smaller. So when you're writing

programs, you have to decide which is more importantspeed or size.

The last reason why you might not make all of your member functions inline is that it makes your classes hard to read. Complex classes with lots of member functions become huge when you use inline member functions. Other

programmers tend to find them difficult to deal with. Plenty of programmers disagree with this point of viewyou have to decide for yourself.

Tip

In game programming, the real memory and disk hog tends to be the graphics, images, and sounds your game displays. Even if you use lots of inline member functions, your code is small by

function inline, you shouldn't worry a lot about program size. Speed tends to be the more important factor.

One way to have the advantages of inline member functions without cluttering up your class definitions is to use out-of-line inline functions.

Say what?

Amazingly enough, C++ lets you define out-of-line member functions that are inline. It sounds kooky, but it's actually a nice feature. listing 3.6 illustrates how to create out-of-line inline functions.