what does inline keyword do in classes?????
How can we create a program that makes text file but name is chosen by user.for example IF user wishes he want to create file of shahzaib.txt so program should make file of that name???
inline keyword does the same thing for class methods that it does for global methods which is it instructs the compiler to 'inline' the code instead of generating a 'function call'. In other words it has no effect on what the method does only on how the code is translated into assembly language. Normally each function you write is translated into a function at the assenbly level and then each function call in the source translates into one function call in assembly language. If you ask the compiler to inline a method it will try to replace the call to the function with the body of the function each time it is called. So for example if your class definition was
The advantage of inlining your function is in removing the function call overhead and opening up the way for the compiler to optimise the performance of your program without having to copy your code or modify your class definitions in undesirable ways such as making your private data public.
Well, yes, the way I demonstrated it the inline keyword is redundant but the original question was about the keyword in a class definition in general. You could use it in a non-redundant way in a class by declaring an inline function and defer its definition to later in the file like this:
1 2 3 4 5 6 7 8 9 10 11 12
inlinevoid x(float a);
void Foo::x(float a)
var = a;
If I remember correctly even if you use the inline keyword the compiler still has last say whether the function is actually inlined or not. If you were to place your Foo::x function in a source file and take out the inlined keyword, if you look at the assembly it would probably still be inlined. The only real effect inline has is if you were to say define a function's body in the header you would use inline to avoid linking errors as the function would be compiled in multiple source files and there would be a conflict. Though having the body of the function in the header file does mean you would potentially get an inlined function if you were say using a dynamic linked library where you don't have access to the source file when you compile, where it would otherwise be impossible to inline.