By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,877 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,877 IT Pros & Developers. It's quick & easy.

Namespace Scope functions and the static keyword in CLI

P: n/a
Hi,

I have an interesting issue, well, it's not really an issue, but I'd
like to understand the mechanics of what's going on. I have a file,
in CLI, which has a class declared, and a static fuction. Because the
class is declared globally, I declare it as static.

Is a function with a namespace scope declared as static by default?

So, this worked fine when I was accessing the static function inside
the cpp file paired with this header. Keep in mind that it is
declared static explicitly.

But when I ty to access this static function from another file, aside
from the cpp file the class is implemented in, I get an compile error
C2129 function declared but not defined. But when declare it as
static inline, or remove the static keyword it works fine.

Can someone explain why this is?

May 21 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
>But when I ty to access this static function from another file, aside
>from the cpp file the class is implemented in, I get an compile error
when applied to a global function, the static keyword limits the scope
to the current source module, so an error in that circumstance is to
be expected.
>But when declare it as
static inline, or remove the static keyword it works fine.
If you define it inline (in the header file), both source modules will
have a copy of the code.

Dave
May 21 '07 #2

P: n/a

"David Lowndes" <Da****@example.invalidwrote in message
news:ku********************************@4ax.com...
But when I ty to access this static function from another file, aside
from the cpp file the class is implemented in, I get an compile error

when applied to a global function, the static keyword limits the scope
to the current source module, so an error in that circumstance is to
be expected.
And because this behavior is inconsistent with other uses of static, it's
now recommended to use the anonymous namespace for that instead.
>
>>But when declare it as
static inline, or remove the static keyword it works fine.
Without the static keyword, you should have gotten a multiple definition
error.
>
If you define it inline (in the header file), both source modules will
have a copy of the code.
Inline functions should be defined in the header file, non-inline function
should have a prototype in the header, but the definition should always be
in an implementation file. This is based on your use of the inline keyword
(or defining inside the type declaration), not the compiler's ultimate
decision whether to inline or not.
>
Dave

May 22 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.