i have following 2 class header files (a.h , b.h), function inside each:
class { public: double f(double); };
class b { public: double g(double); };
here corresponding cpp files:
include "a.h"
double a::f(double variable1) { // stuff on variable 1 }
include "b.h"
include "a.h"
double b::g(double variable2) { // stuff on variable2 using f }
i use function f (from a) inside function g (in b). how do this?
a few options:
make functions
static
. can if functions don't use non-static members of class. can calla::f(variable2)
withinb::g
have
b
inherita
. can accessa::f(variable2)
b::g
have instance of
a
member ofb
. accessa::f(variable2)
instance.pass instance of
a
functionb::g
. access function via instance.
(1) works if classes serve little more acting way of bundling related functions together. (although prefer use namespace
such cases.)
(2) works if inheritance structure logical. don't if isn't.
(3) common thing do.
Comments
Post a Comment