using base class instance in a derived class

K

kaferro

I have a base class that has about ten variables. Later, I have a
derived class that adds five more variables. In the derived class'
setData() function, I pass an instance of the base class, and use it to
set the derived class' base class variables. Is there a problem with
this structure?

//cComponent is the base class

void setData(cComponent tmpComponent, float tmpPosition, char
roll_or_new, int orderNum)
{
//trade date
date=date_today();
name=tmpComponent.returnName();
contract_code=tmpComponent.returnContract_code();
contract_month=tmpComponent.returnContract_month();
position=tmpPosition;
//need to set correctly !!!!
price=tmpComponent.returnPrice();
unit_multiplier=tmpComponent.returnUnitMult();
delivery_year=tmpComponent.returnDeliveryYear();;
symbol=tmpComponent.returnSymbol();
exchange=tmpComponent.returnExchange();


orderId=id_creator(orderNum);

//sets other data
if(position<0)
{ lots = position * -1;
order_b_s="S";
}
else
{ lots = position;
order_b_s="B";
}

if(roll_or_new == 'R' && order_b_s == "B")
{
order_memo="Closing Roll. Broker is instructed to check for
existing positions prior to execution.";
order_type="Cover";
}
else if(roll_or_new == 'R' && order_b_s == "S")
{
order_memo="Closing Roll. Broker is instructed to check for
existing positions prior to execution.";
order_type="Sell";
}
else if(roll_or_new == 'N' && order_b_s == "B")
{
order_memo="Adjusting Trade";
order_type="Buy";
}
else if(roll_or_new == 'N' && order_b_s == "S")
{
order_memo="Adjusting Trade";
order_type="Short";
}
else
{
order_memo="Error";
order_type="Error";
}

}
 
A

amit.limaye

this should work but with this method u will not be able to access the
derived class variables with this tmpComponent handle.
Also try passing a reference instead of the actual object. If u want it
to be read-only pass it as a const.

-SIGTERM
amit
 
G

Gianni Mariani

I have a base class that has about ten variables. Later, I have a
derived class that adds five more variables. In the derived class'
setData() function, I pass an instance of the base class, and use it to
set the derived class' base class variables. Is there a problem with
this structure?

based on what you wrote, I can't see anything "bad". Better would be to
pass by reference and even better would be to remove the need for all
the set operations. But that depends on the design and problem domain
so I can't tell if it would be "better" in this case.

In essance, anywhere where you start picking and choosing various
members to copy, you want to ask yourself, "is this maintainable". If I
need to add another member, does it mean I have to change code in alot
of different places (prone to error), or even worse, code in clients of
this code.

It seems this is nothing specific to C++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top