why c++ does not accepts static keyword in function defnition? -
i have program class t has static function.
static void t::func() { m_i=10; cout<<m_i<<endl; }
when try add static in function definition, compiler throws error error: cannot declare member function ‘static void t::func()’ have static linkage. why not accepting static keyword in definition?
the problem keyword static
means different things depending on context.
when declare member function static, in
class t { ... static void func(); ... };
then static
keyword means func
class function, it's not bound specific object.
when define function in source file, like
static void t::func() { ... }
then set function linkage different using static
in declaration inside class. static
when defining function function available in current translation unit, , contradicts declaration function available knows class.
it's not possible make member function (declared static
or not) have static linkage.
if want "hide" member function others, can't called, why not make private
member function? use things the pimpl idiom or not have member function begin with, in case can declare have static
linkage.
Comments
Post a Comment