Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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.)
#include "bar.hpp"
const int foo = bar();
static const
Write
extern const int foo;
in foo.hpp and
in foo.cpp.
Add a comment
Of course there is:
// foo.hpp const int foo = bar();
as you can see here it works just fine.
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
#include "bar.hpp"and thenconst int foo = bar();?const int foo = bar();? Or am I missing something?static constmember? Global/namespace variables can be used, but I preferstatic constcus you don't have to worry about extern.