inline keyword

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???
Last edited on
what does inline keyword do in classes?????
You mean inside them, like
1
2
3
4
class Foo
{
inline void x(){};
}
? Then nothing.

How can we create a program that makes text file but name is chosen by user
1
2
3
4
5
std::string name;
std::cout << "Enter file name: ";
std::cin >> name;
std::ofstream output(name);
output << "I am the first line of new file!";
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
1
2
3
4
5
6
7
class Foo
{
public:
  inline void x(float a){var = a;};
private:
  float var;
};

then code like this
1
2
3
4
5
int main()
{
   Foo d;
   d.x(1.0f);
}

is compiled as if you made var public and wrote
1
2
3
4
5
int main()
{
   Foo d;
   d.var = 1.0f;
}

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.
If class methods are defined inside of class inline keyword will not do anything. Because methods defined inside class body are inline by definition. So inline keyword is redundancy here.
Last edited on
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
class Foo
{
public:
  inline void x(float a);
private:
  float var;
};

void Foo::x(float a)
{
  var = a;
}
closed account (o1vk4iN6)
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.
Last edited on
Topic archived. No new replies allowed.