A way to include code 'directly' into a class

May 26, 2011 at 6:33pm
I want to have some functions ready to be 'injected' into any class.
What strategy are the best? Macros ? Another ? 'inline' ?

Thanks
May 26, 2011 at 6:50pm
To be honest... you aren't really being clear what exactly you are trying to achieve.
May 26, 2011 at 7:10pm
Just use function pointers.
For example, you could include a map<string, funcptr> combo to register functions and then call them by name.
May 26, 2011 at 7:14pm
Rather than function pointers, if he wanted replacable functions I'd use function objects... (http://www.oodesign.com/strategy-pattern.html )
May 26, 2011 at 7:32pm
hmmmm
Maybe I didnt explain well ....

Imagine I have 3 functions
FunctionA, FunctionB, and FunctionC.
Ok, this functions are stored into a h.file (I would not want to use header and implem)
And... I dont want to create any class for this functions

I want to do something like when you use the string library. Just you have to inlcude <string> and later write std::the_string_functions.

Maybe it is a stupid question...., but I'd want to write my functions in a way they be 'inserted' into the classes which use them . (inline ?).
Thanks.
May 26, 2011 at 7:34pm
The std:: syntax has nothing to do with classes. It's a namespace.

I don't see what you could get out of writing a function then "referencing" it or something in a class. Just put the function in the class. If you need to use the same code multiple times for different classes, it probably shouldn't be in any one of those classes unless they are related somehow.
May 26, 2011 at 7:37pm
Uh... you can inline a function... but that's an optimization that could potentially improve runtime speed... I am not really sure if that's what you're after.
May 26, 2011 at 7:57pm
Maybe he's on about writing a .dll file? So that he can write functions he uses often and 'include' them in other projects?
May 26, 2011 at 11:16pm
All that making a function inline means is that every time their is a call to the function the compiler replaces the function call with the actual body of the function (and changes up the code a bit so that there aren't errors). As for your question, perhaps I'm wrong here but are you trying to create friend functions so that you have a global function that you can as you put it "insert" into any class when you want too? If so you just have to define the function globally and then in the class declaration write something like
 
friend void myFunc(int param1, double param2);

Last edited on May 26, 2011 at 11:17pm
Topic archived. No new replies allowed.