Some questions about inline function
-
Hello all, I have some questions about inline function, if I have something like this
class myClass { protected: int i; public: inline Set_I(int val) {i = val;}; } int main() { myClass c; c.Set_I(10); }
I want to know what will actually happen. Since Set_I() is an inline function, the compiler will replcae the definition of the function in where the function has been invoked. However, in this case the inline is actually accessing the protected member of the class, so what will happen?? Thanks! Nachi -
Hello all, I have some questions about inline function, if I have something like this
class myClass { protected: int i; public: inline Set_I(int val) {i = val;}; } int main() { myClass c; c.Set_I(10); }
I want to know what will actually happen. Since Set_I() is an inline function, the compiler will replcae the definition of the function in where the function has been invoked. However, in this case the inline is actually accessing the protected member of the class, so what will happen?? Thanks! Nachinachilau wrote: However, in this case the inline is actually accessing the protected member of the class, so what will happen?? Exactly the same thing. The
protected
keyword tells the compiler that thei
variable can be accessed by members ofmyClass
or its derivatives. This condition is still true. Whether or not the compiler inlines the member functions makes no difference at all. It is still the same function accessing the same variable. Hope this helps,Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Hello all, I have some questions about inline function, if I have something like this
class myClass { protected: int i; public: inline Set_I(int val) {i = val;}; } int main() { myClass c; c.Set_I(10); }
I want to know what will actually happen. Since Set_I() is an inline function, the compiler will replcae the definition of the function in where the function has been invoked. However, in this case the inline is actually accessing the protected member of the class, so what will happen?? Thanks! NachiFor your information. 6.3: The keyword inline[^] Maxwell Chen
-
Hello all, I have some questions about inline function, if I have something like this
class myClass { protected: int i; public: inline Set_I(int val) {i = val;}; } int main() { myClass c; c.Set_I(10); }
I want to know what will actually happen. Since Set_I() is an inline function, the compiler will replcae the definition of the function in where the function has been invoked. However, in this case the inline is actually accessing the protected member of the class, so what will happen?? Thanks! Nachinachilau wrote: compiler will replcae the definition of the function in where the function has been invoked. No. inline is a suggestion to the compiler, it can choose to ignore it. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
-
Hello all, I have some questions about inline function, if I have something like this
class myClass { protected: int i; public: inline Set_I(int val) {i = val;}; } int main() { myClass c; c.Set_I(10); }
I want to know what will actually happen. Since Set_I() is an inline function, the compiler will replcae the definition of the function in where the function has been invoked. However, in this case the inline is actually accessing the protected member of the class, so what will happen?? Thanks! Nachiinline is an important keyword, but it must be used as: 1. in h file, but not inside class scope(if so, it is ignored) 2. not in cpp file (if so, it is ignored) example is //MyClass.h class MyClass { public int Get(); }; inline int MyClass::Get(){return 1;} advantage of inline function is 1.(at least books say) it is faster than normal function. 2.in case of you need to put your code on head file (e.g. craete library) includeh10
-
inline is an important keyword, but it must be used as: 1. in h file, but not inside class scope(if so, it is ignored) 2. not in cpp file (if so, it is ignored) example is //MyClass.h class MyClass { public int Get(); }; inline int MyClass::Get(){return 1;} advantage of inline function is 1.(at least books say) it is faster than normal function. 2.in case of you need to put your code on head file (e.g. craete library) includeh10
includeh10 wrote: public int Get(); }; inline int MyClass::Get(){return 1;} 2.in case of you need to put your code on head file (e.g. craete library) In fact, the compiler is trying to inline any function defined in the *.h-file. Regardless of
inline
or not. If you want to suggest to inline a function with a body in the *.cpp-file, you addinline
before the declaration in the header file. In any case, you are only giving a suggestion, you can not force the compiler to do the inlining.
"We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.
-
includeh10 wrote: public int Get(); }; inline int MyClass::Get(){return 1;} 2.in case of you need to put your code on head file (e.g. craete library) In fact, the compiler is trying to inline any function defined in the *.h-file. Regardless of
inline
or not. If you want to suggest to inline a function with a body in the *.cpp-file, you addinline
before the declaration in the header file. In any case, you are only giving a suggestion, you can not force the compiler to do the inlining.
"We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.
i think u never use inline before. code above can not be compiled if no keyword inline. includeh10