4

I have a class exposing a static function in myclass.hpp

class MyClass { public: static std::string dosome(); }; 

Well, in myclass.cpp what should I write: this:

std::string MyClass::dosome() { ... } 

or this:

static std::string MyClass::dosome() { ... } 

I guess I should not repeat the static keyword... is it correct?

2
  • Don't repeat the static keyword but I'd be interested to find out why Commented Jan 24, 2011 at 11:12
  • @MattSmith: See my answer below. Commented Jan 24, 2011 at 11:12

3 Answers 3

10

C++ compiler will not allow this:

static std::string MyClass::dosome() { ... } 

since having static in a function definition means something completely different - static linkage (meaning the function can only be called from the same translation unit).

Having static in a member function declaration is enough.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah yeah you're right... it's like c... if it encounters a static function it means that it can only be called from the same... file or better, as you pointed out, translation unit. Thank you for your exaustive answer.
5

Do not repeat the static keyword. To do so will result in an error.

Comments

1

Yes. The static keyword should not be used when defining a function body outside the class definition.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.