user2643716
user2643716

Reputation: 31

Initializing const value in c++ header with value returned by function

Is there any way to define conastant foo in c++ header "foo.hpp"

const int foo;

and initialize it with value returned by function bar defined in "bar.hpp"

int bar();

? (Either in foo.hpp or in foo.cpp.)

Upvotes: 0

Views: 107

Answers (2)

Oswald
Oswald

Reputation: 31685

Write

extern const int foo;

in foo.hpp and

const int foo = bar();

in foo.cpp.

Upvotes: 1

Shoe
Shoe

Reputation: 76280

Of course there is:

// foo.hpp
const int foo = bar();

as you can see here it works just fine.

Upvotes: 1

Related Questions