B
BillyO
I need a general mechanism for passing parameters to an API function I
am designing, call it foo. Foo processes data, the operation used to
process the data and the parameters for that operation are to be
passed to foo. A simple approach involves the use of base and derived
classes:
Class DataProcessor
{
Public:
enum ProcessorTypeEnum{op1, op2, op3}
DataProcessor(ProcessorTypeEnum
ProcType):m_ProcType(ProcType){}
ProcessorTypeEnum m_ProcType;
}
Class DataProcessorOp1
{
Public:
DataProcessorOp1(int Param1):m_Param1(Param1),
DataProcessor(op1){}
Int m_Param1;
}
And foo:
Foo(const DataProcessor& dp)
{
If (dp.m_ProcType==op1)
{
// cast dp to DataProcessorOp1 and continue
}
Else if (....=op2)
{
Etc
}
}
This is extensible since I can add new derived classes which contain
the necessary parameters as class members.
An alternate approach is to make DataProcessor polymorphic and use
RTTI to distinguish between dynamic types in Foo. I think the two are
equivalent in terms of storage, but what are the pros and cons of
both? Or is their a better way entirely?
TIA
am designing, call it foo. Foo processes data, the operation used to
process the data and the parameters for that operation are to be
passed to foo. A simple approach involves the use of base and derived
classes:
Class DataProcessor
{
Public:
enum ProcessorTypeEnum{op1, op2, op3}
DataProcessor(ProcessorTypeEnum
ProcType):m_ProcType(ProcType){}
ProcessorTypeEnum m_ProcType;
}
Class DataProcessorOp1
{
Public:
DataProcessorOp1(int Param1):m_Param1(Param1),
DataProcessor(op1){}
Int m_Param1;
}
And foo:
Foo(const DataProcessor& dp)
{
If (dp.m_ProcType==op1)
{
// cast dp to DataProcessorOp1 and continue
}
Else if (....=op2)
{
Etc
}
}
This is extensible since I can add new derived classes which contain
the necessary parameters as class members.
An alternate approach is to make DataProcessor polymorphic and use
RTTI to distinguish between dynamic types in Foo. I think the two are
equivalent in terms of storage, but what are the pros and cons of
both? Or is their a better way entirely?
TIA