Anda di halaman 1dari 6

DOCUMENTTYPE TypeUnitOrDepartmentHere TypeYourNameHere Extern Keyword: There are 2 similar, but different, uses for extern in C++.

It can be used as 1) a storage class specifier for a variable or 2)to tell the compiler how to link to an external function. 1) If you write a program that uses multiple files of source code, and you have a global variable that you want to use in both files, then you can use the extern keyword. File A: ... int myGlobal; // Define a global variable ... File B: ... extern int myGlobal; // Declare that you are using the same . . . // global variable that was defined in // the other file The extern keyword used in this sense is good for code maintenance because it tells someone reading the code that you are intentionally using a variable that was defined elsewhere. If you find yourself using extern a lot in this sense, I recommend that you re-think your program design to minimize coupling between modules. 2) The second use is a little more complicated to explain well. If you want to link to functions in libraries or object files (I mean object as opposed to source or executable) that were creating using a C compiler, you can use the extern keyword. Say File A is object code that was created using a C compiler and has a function called void myCFunc(); To call that function from your C++ program that you are writing (without recompiling the the source code for myCFunc using the C++ compiler) you would use a function prototype like this: extern "C" void myCFunc(); That will allow the C++ code that you write and compile to link to and use the already written and compiled C code. I have needed this to use already existing libraries of code that were compiled with a C compiler because I didn't have the source code for those libraries. Some C++ compilers may even support linking to different languages than C. If so, the syntax would be: extern "language_specifier" function_prototype TypeDateHere

1 (6)

DOCUMENTTYPE TypeUnitOrDepartmentHere TypeYourNameHere Register Keyword: TypeDateHere

2 (6)

ANSI C does not allow for taking the address of a register object; this restriction does not apply to C++. However, if the address-of operator (&) is used on an object, the compiler must put the object in a location for which an address can be represented. In practice, this means in memory instead of in a register. Static keyword: The use of static inside a function is the simplest. It simply means that once the variable has been initialized, it remains in memory until the end of the program. You can think of it as saying that the variable sticks around, maintaining its value, until the program completely ends. For instance, you can use a static variable to record the number of times a function has been called simply by including the lines static int count =0; and count++; inside the function. Because count is a static variable, the line "static int count = 0;" will only be executed once. Whenever the function is called, count will have the last value assigned to it. You can also use static in this fashion to prevent a variable from being reinitialized inside a loop. For instance, in the following code, number_of_times comes out to be 100, even though the line "static int number_of_times = 0;" is inside the inner loop, where it would apparently be executed every time the program loops. The trick is that the keyword static prevents re-initialization of the variable. One feature of using a static keyword is that it happens to be initialized to zero automatically for you -- but don't rely on this behavior (it makes your intention unclear).

The use of static inside a function is the simplest. It simply means that once the variable has been initialized, it remains in memory until the end of the program. You can think of it as saying that the variable sticks around, maintaining its value, until the program completely ends. For instance, you can use a static variable to record the number of times a function has been called simply by including the lines static int count =0; and count++; inside the function. Because count is a static variable, the line "static int count = 0;" will only be executed once. Whenever the function is called, count will have the last value assigned to it. You can also use static in this fashion to prevent a variable from being reinitialized inside a loop. For instance, in the following code, number_of_times comes out to be 100, even though the line "static int number_of_times = 0;" is inside the inner loop, where it would apparently be executed every time the program loops. The trick is that the keyword static prevents re-initialization of the variable. One feature of using a static keyword is that it happens to be initialized to zero automatically for you -- but don't rely on this behavior (it makes your intention unclear).
for(int x=0; x<10; x++) { for(int y=0; y<10; y++) { static int number_of_times = 0; number_of_times++; } }

You might use a static variable in order to preserve information about the last value a function returned, such as if you wanted to store the maximum value calculated by a function. If you are doing string parsing, you might also store the last token returned by the function in order to be able to fetch it simply by calling the function with an argument indicating it should return the last token.

DOCUMENTTYPE TypeUnitOrDepartmentHere TypeYourNameHere TypeDateHere

3 (6)

The second use of static is inside a class definition. While most variables declared inside a class occur on an instance-by-instance basis (which is to say that for each instance of a class, the variable can have a different value), a static member variable has the same value in any instance of the class and doesn't even require an instance of the class to exist. A helpful way to think about it is to imagine that the static variables of a class contain information essential to the making of new member objects (as though a class definition were, to borrow an analogy from The Java Programming Language, a factory). For instance, if you wanted to number your instances of a class, you could use a static member variable to keep track of the last number used. Importantly, it is good syntax to refer to static member functions through the use of a class name (class_name::x; rather than instance_of_class.x;). Doing so helps to remind the programmer that static member variables do not belong to a single instance of the class and that you don't need to have a single instance of a class to use a static member variable. As you have probably noticed, to access the static member, you use the scope operator, ::, when you refer to it through the name of the class. An important detail to keep in mind when debugging or implementing a program using a static class member is that you cannot initialize the static class member inside of the class. In fact, if you decide to put your code in a header file, you cannot even initialize the static variable inside of the header file; do it in a .cpp file instead. Moreover, you are required to initialize the static class member or it will not be in scope. (The syntax is a bit weird: "type class_name::static_variable = value".) You can also have static member functions of a class. Static member functions are functions that do not require an instance of the class, and are called the same way you access static member variables -- with the class name rather than a variable name. (E.g. a_class::static_function(); rather than an_instance.function();) Static member functions can only operate on static members, as they do not belong to specific instances of a class. Static member functions can be used to modify static member variables to keep track of their values -- for instance, you might use a static member function if you chose to use a counter to give each instance of a class a unique id. For instance, you could use the following code:
class user { private: int id; static int next_id;

public: static int next_user_id() { next_id++; return next_id; } /* More stuff for the class user */ user() { id = user::next_id++; //or, id = user.next_user_id(); } }; int user::next_id = 0;

Notice that you must include the type of the static variable when you set it! The line
user a_user;

would set id to the next id number not assigned to any other user through this process. Note that it would be good style to declare id as a const.

DOCUMENTTYPE TypeUnitOrDepartmentHere TypeYourNameHere TypeDateHere

4 (6)

The last use of static is as a global variable inside a file of code. In this case, the use of static indicates that source code in other files that are part of the project cannot access the variable. Only code inside the single file can see the variable. (It's scope -- or visibility -- is limited to the file.) This technique can be used to simulate object oriented code because it limits visibility of a variable and thus helps avoid naming conflicts. This use of static is a holdover from C.

CLOG and Cerr:

1.

uffered and Unbuffered


Buffered - All output is temporarily stored in a buffer and then dumped to screen in one go. Both cout and clog are buffered. Unbuffered- All output goes immediately to the output device. An example of an unbuffered object is cerr.

The example below demonstrates that cerr is used in the same way as cout. #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { cerr.width(15) ; cerr.right; cerr << "Error" << endl; return 0; }

The keyword inline sends a request to the compiler to make the function inline and not a command. There are few situations where an inline function may not work:

For a function returning values; if a return statement exists. For a function not returning any values; if a loop, switch or goto statement exists. If a function is recursive.

Some common aspects:


Compiler option (debug builds usually don't inline, or oyu can tell your compiler to inline regardless of the delcaration) suitable calling convention (varargs usually functions aren't inlined)

DOCUMENTTYPE TypeUnitOrDepartmentHere TypeYourNameHere


5 (6)

TypeDateHere

suitable for inlining: depends on size of the function, frequency of the funciton, and optimization settings (speed vs. code size). E.g. a huge function may be inlined if it is called just once inline call depth and recursion

The 3rd is probably the core of your question, but that's really "compiler specific heuristics" - you need to check the compiler docs, but usualyl they won't give much guarantees. MSDN has some (limited) information for MSVC. Beyond trivialities (e.g. simple getters and very primitive functions), inlining as such isn't very helpful anymore. The cost of the call instruction has gone down, and branch prediction has greatly improved. The great opportunity for inlining is removing code paths that the compiler knows won't be taken - as an extreme example:
inline int Foo(bool refresh = false) { if (refresh) { // ...extensive code to update m_foo } return m_foo; }

A good compiler would inline Foo(false), but not Foo(true). With Link Time Code Generation, Foo could reside in a .cpp (without a inline declararion), and Foo(false) would still be inlined, so again inline has only marginal effects here. To summarize: There are few scenarios where you should attempt to take manual control of inlining by placing (or omitting) inline statements.

The compiler generates an inline function as an ordinary callable function (out of line) when any of the following is true:

You compile with +d. You compile with -g. The function's address is needed (as with a virtual function). The function contains control structures the compiler can't generate inline. The function is too complex.

According to the response to this post by 'clamage45' the "control structures that the compiler can't generate inline" are:

the function contains forbidden constructs, like loop, switch, or goto

Another list can be found here. As most other answers have specified the heuristics are going to be 100% compiler specific, from what I've read I think to ensure that a function is actually inlined you need to avoid:

DOCUMENTTYPE TypeUnitOrDepartmentHere TypeYourNameHere


6 (6)

TypeDateHere

local static variables loop constructs switch statements try/catch goto recursion and of course too complex (whatever that means)

if your goal is to generate efficient code, a switch statement may offer some advantages. It's also more clearly shows the programmer's intent, in my opinion - which obviously differs from your opinion.