c++ - Static inline method no need static member initialization -


there class:

k.h

class k {     static int ii;     static void foo(); }; 

k.cpp

#include "k.h"  void k::foo() {     ii++; } 

during compile following error message:

error lnk2001: unresolved external symbol "private: static int k::ii" (?ii@k@@0ha) 

it's ok. when add inline keyword method, error disappeared:

class k {     static int ii;     inline static void foo(); }; 

this not real-world example, don't know happens in code, may explain me?

this code:

#include <iostream> using namespace std;  struct k {     static int ii;     static void foo(); };  void k::foo() {     ii=0; }  int main() {     // code goes here     return 0; } 

gives linking error, because function k::foo output compiler, , references k::ii.

this code:

#include <iostream> using namespace std;  struct k {     static int ii;     inline static void foo(); };  inline void k::foo() {     ii=0; }  int main() {     // code goes here     return 0; } 

does not give linking error, because function k::foo declared inline , not called anywhere, compiler never produces code it.

if add call k::foo() inside main, or anywhere else, linking error.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -