PDA

View Full Version : Simplify Function



Atomic_Sheep
31st August 2013, 12:06
I'm trying to write a function with objects:


void System::SetBetaParameter()
{
if(conditions)
{
cBetaOne.m_bState = ENGAGED;
}
else if(alternate conditions)
{
cBetaOne.m_bState = DISENGAGED;
}
}

My case is simple with just two conditions and two objects, so theoretically I can just write two if loops testing for type of object being tested, but this is very cumbersome and I'm more interested in doing it by passing by reference or as a pointer.

Question that I have is, should I do the following:

System.h


#include "beta.h"

class System
{
Beta cBetaOne, cBetaTwo;
Beta *cBetaOne, *cBetaTwo;
}


Simply create the objects and then use their pointers after setting those pointers to correspond to those objects in the constructor or should I dynamically allocate those objects using new in the constructor and go from there?

Santosh Reddy
31st August 2013, 12:42
Youe question is not clear to me.

Let me see, is your question about sould you create objects dynamically? Use dynamic allocation in situations where you don't know how many objects need to be created if at all they need to be created. If you know upfront that object has to be created and how many of those have to be created then go with static objects (global/stack based)

wysota
2nd September 2013, 21:53
What are the conditions?