2

As inline function will replace the actual call from code, what is the use of calling inline function as const.

Inline void adddata() const {...}
Swapnil
  • 1,424
  • 2
  • 19
  • 30
  • 1
    [`inline`](https://stackoverflow.com/questions/145838/benefits-of-inline-functions-in-c) and [`const`](https://stackoverflow.com/questions/751681/meaning-of-const-last-in-a-c-method-declaration) are two orthogonal concepts, they have nothing to do with each other – Cory Kramer Aug 13 '15 at 15:17
  • You have specified the function as free standing (not a member function). If that was your intention, the code isn't valid - a const function must be a member function. – nos Aug 13 '15 at 15:19
  • But if you define the the function as mention above what happens internally? – Swapnil Aug 13 '15 at 15:19
  • 4
    @swapnil Are you morally opposed to reading? What is missing from the two links I provided you? – Cory Kramer Aug 13 '15 at 15:20
  • 3
    It's difficult to imagine how to design a const function that adds data. The only way to do it is to have write to a member pointer so that you can get around the `const` by avoid modifying any member, which is a ugly design. – user3528438 Aug 13 '15 at 15:21
  • `inline` doesn't mean that much, just that you're supposed to have the function definition in every translation unit where it's called while `const` means that it's only callable as a member of a `const` object (and `this` pointer will be const) – skyking Aug 13 '15 at 15:25
  • 3
    @user3528438 _"The only way to do it ..."_ There's `mutable` also. – πάντα ῥεῖ Aug 13 '15 at 15:28
  • @πάνταῥεῖ And... `const_cast`... but we wouldn't want to talk about that one. `inline` means that you suggest the compiler should inline the function--it doesn't even mean the compiler will actually do it. – caps Aug 13 '15 at 17:00

1 Answers1

9

An inline function is one which can be defined in each translation unit, and must be defined separately in each translation unit where it is called. It is also a completely non binding suggestion to the compiler that you think the function should be inline. The compiler is free to actually inline or not inline any of your functions, whether or not they are declared inline.

const means that the object the function is a method of will tend not to be visibly modified by the function call. There are exceptions to this, it is always possible to modify if you try hard enough, but in general const is a promise to the caller that you won't.

Using them together means nothing additional to their individual meanings. They are essentially unrelated.

Ethan Fine
  • 519
  • 3
  • 8