Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Certain situations in my code, i end up invoking the function only if that function is defined, or else i should not. How can i achieve this ?

like:
if (function 'sum' exists ) then invoke sum ()

May be the other way around to ask this question is: How to determine if function is defined at runtime and if so, then invoke.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
417 views
Welcome To Ask or Share your Answers For Others

1 Answer

When you declare 'sum' you could declare it like:

#define SUM_EXISTS
int sum(std::vector<int>& addMeUp) {
    ...
}

Then when you come to use it you could go:

#ifdef SUM_EXISTS
int result = sum(x);
...
#endif

I'm guessing you're coming from a scripting language where things are all done at runtime. The main thing to remember with C++ is the two phases:

  • Compile time
    • Preprocessor runs
    • template code is turned into real source code
    • source code is turned in machine code
  • runtime
    • the machine code is run

So all the #define and things like that happen at compile time.

....

If you really wanted to do it all at runtime .. you might be interested in using some of the component architecture products out there.

Or maybe a plugin kind of architecture is what you're after.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...