More flexible parameter passing in C++

John Hunter jdhunter at nitace.bsd.uchicago.edu
Tue Jan 1 13:14:55 EST 2002


>>>>> "Hung" == Hung Jung Lu <hungjunglu at yahoo.com> writes:


    Hung> Is there some object-oriented way of making parameter
    Hung> passing more flexible in C++? I mean, the goal is avoid
    Hung> modifying the header declaration of methods... especially in
    Hung> situations where you have subclasses and sub-subclasses.

What if you passed a struct such as Params, which has all the params
as fields?  Then you could pass each method the Params object. If you
need to add a new parameter, for a certain method, you would only need
to change the Param struct.  All of the calling semantics would be
unchanged.

Or you could create the new param class by subclassing it.

struct ParamsBase {
        string name;
        size_t N;
        float dt;
};


struct MyParams : ParamsBase {
        Foo foo;
};

//will accept either a ParamsBase* or a MyParams*
void some_method( ParamsBase* pars);  




More information about the Python-list mailing list