0

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.)

9
  • #include "bar.hpp" and then const int foo = bar();? Commented Oct 24, 2013 at 17:18
  • You mean const int foo = bar();? Or am I missing something? Commented Oct 24, 2013 at 17:18
  • Why not a static const member? Global/namespace variables can be used, but I prefer static const cus you don't have to worry about extern. Commented Oct 24, 2013 at 17:24
  • @mike-seymour Not really. When doing like this I get compilation error: error: initializer element is not constant Commented Oct 24, 2013 at 17:26
  • @jrok the same as above Commented Oct 24, 2013 at 17:29

2 Answers 2

1

Write

extern const int foo;

in foo.hpp and

const int foo = bar();

in foo.cpp.

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

Comments

1

Of course there is:

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

as you can see here it works just fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.