calling a function two ways
-
Hey guys, I don't see the difference in using two different ways of calling a function. in the function send, you are creating a pair of set by calling the function creatNVPSet(); In the function update, you are creating a pair of set by first creating a pointer to a Ball object and then using the pointer pBall to call upon the member function createNVPSet(); What's the point of doing that? Can both of the function just create the set by using createNVPSet(); instead of using pBall->creatNVPSet(); thanks in advance :) PairSet* CreateNVPSet() { ............ ............ return set; } void send (int x, int y) { PairSet* pNvpSet = createNVPSet(); ................ ............. } void update (int a, int b) { Ball *pBall; PairSet* pNvpSet = pBall->createNVPSet(); ........... ........ }
-
Hey guys, I don't see the difference in using two different ways of calling a function. in the function send, you are creating a pair of set by calling the function creatNVPSet(); In the function update, you are creating a pair of set by first creating a pointer to a Ball object and then using the pointer pBall to call upon the member function createNVPSet(); What's the point of doing that? Can both of the function just create the set by using createNVPSet(); instead of using pBall->creatNVPSet(); thanks in advance :) PairSet* CreateNVPSet() { ............ ............ return set; } void send (int x, int y) { PairSet* pNvpSet = createNVPSet(); ................ ............. } void update (int a, int b) { Ball *pBall; PairSet* pNvpSet = pBall->createNVPSet(); ........... ........ }
it's hard to say with the lack of details. but this is wrong :
Ball *pBall;
PairSet* pNvpSet = pBall->createNVPSet();you need to create a
Ball
object before callingcreateNVPSet
on the object, it might simply be a simple omission in your exampel. Also, you can have more than one method that create the same object, it depends on the design; in your example, does thesend
function a free function or is it a member of a class ? same thing forupdate
? maybe in the context of theupdate
function there is no way to create aPairSet
except thrue aBall
object.
Maximilien Lincourt Your Head A Splode - Strong Bad
-
Hey guys, I don't see the difference in using two different ways of calling a function. in the function send, you are creating a pair of set by calling the function creatNVPSet(); In the function update, you are creating a pair of set by first creating a pointer to a Ball object and then using the pointer pBall to call upon the member function createNVPSet(); What's the point of doing that? Can both of the function just create the set by using createNVPSet(); instead of using pBall->creatNVPSet(); thanks in advance :) PairSet* CreateNVPSet() { ............ ............ return set; } void send (int x, int y) { PairSet* pNvpSet = createNVPSet(); ................ ............. } void update (int a, int b) { Ball *pBall; PairSet* pNvpSet = pBall->createNVPSet(); ........... ........ }
It isn't calling the same function (at least not directly anyway).
Jay03 wrote:
PairSet* CreateNVPSet() { ............ ............ return set; }
This function appears to be in the global namespace and is not a member of any class.
Jay03 wrote:
pBall->createNVPSet();
This function is a member function of the class Ball. While it may end up calling the global method within that function, it is not the same function (and may not call the previous one at all ... depending on the implementation).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
It isn't calling the same function (at least not directly anyway).
Jay03 wrote:
PairSet* CreateNVPSet() { ............ ............ return set; }
This function appears to be in the global namespace and is not a member of any class.
Jay03 wrote:
pBall->createNVPSet();
This function is a member function of the class Ball. While it may end up calling the global method within that function, it is not the same function (and may not call the previous one at all ... depending on the implementation).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
it's hard to say with the lack of details. but this is wrong :
Ball *pBall;
PairSet* pNvpSet = pBall->createNVPSet();you need to create a
Ball
object before callingcreateNVPSet
on the object, it might simply be a simple omission in your exampel. Also, you can have more than one method that create the same object, it depends on the design; in your example, does thesend
function a free function or is it a member of a class ? same thing forupdate
? maybe in the context of theupdate
function there is no way to create aPairSet
except thrue aBall
object.
Maximilien Lincourt Your Head A Splode - Strong Bad
-
sorry im missing some detail. The function createNVPSet is a member of class Ball. PairSet* Ball::CreateNVPSet() { ............ ............ return set; }
-
You haven't given enough information to tell, then. Are send and update both member functions as well? Show the whole class and the relevant methods' implementations and it will be easier to answer your question.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
You haven't given enough information to tell, then. Are send and update both member functions as well? Show the whole class and the relevant methods' implementations and it will be easier to answer your question.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
RTI::AttributeHandleValuePairSet* Ball::createNVPSet() { RTI::AttributeHandleValuePairSet* pBallAttributes = NULL; //! Make sure the RTI Ambassador is set. if ( ms_rtiAmb ) // if ms_rtiAmb!=NULL { pBallAttributes = RTI::AttributeSetFactory::create( 4 ); pBallAttributes->add( Ball::ms_ballLocationXId, (char*)&this->m_currentState.x, sizeof(double) ); pBallAttributes->add( Ball::ms_ballLocationYId, (char*)&this->m_currentState.y, sizeof(double) ); pBallAttributes->add( Ball::ms_ballVelocityXId, (char*)&this->m_currentState.vx, sizeof(double) ); pBallAttributes->add( Ball::ms_ballVelocityYId, (char*)&this->m_currentState.vy, sizeof(double) ); } //! pClockAttributes is allocated on the heap and must be //! deallocated by the calling function return pBallAttributes; } void Ball::sendUpdate() { if ( m_nextStateCalculated == RTI::RTI_TRUE ) { m_currentState = m_nextState; m_nextStateCalculated = RTI::RTI_FALSE; try { /*! * In order to send the values of our attributes, we must * construct an AttributeHandleValuePairSet (AHVPS) which * is a set comprised of attribute handles, values, and * the size of the values. */ RTI::AttributeHandleValuePairSet* pNvpSet = createNVPSet(); /*! * Send the AHVPS to the federation. * * this call returns an event retraction handle but we * don't support event retraction so no need to store it. */ (void) ms_rtiAmb->updateAttributeValues( getInstanceId(), *pNvpSet, NULL ); //! Must free the memory pNvpSet->empty(); delete pNvpSet; } catch ( RTI::Exception& e ) { cerr << "BALL: Error:" << &e << endl; } } } void Ball::update( RTI::InteractionClassHandle theInteraction, const RTI::ParameterHandleValuePairSet& theParameters ) { if (ms_rtiAmb) // if ms_rtiAmb!=NULL { if ( theInteraction == Ball::ms_clockTickId ) { RTI::ParameterHandle paramHandle; RTI::ULong
-
RTI::AttributeHandleValuePairSet* Ball::createNVPSet() { RTI::AttributeHandleValuePairSet* pBallAttributes = NULL; //! Make sure the RTI Ambassador is set. if ( ms_rtiAmb ) // if ms_rtiAmb!=NULL { pBallAttributes = RTI::AttributeSetFactory::create( 4 ); pBallAttributes->add( Ball::ms_ballLocationXId, (char*)&this->m_currentState.x, sizeof(double) ); pBallAttributes->add( Ball::ms_ballLocationYId, (char*)&this->m_currentState.y, sizeof(double) ); pBallAttributes->add( Ball::ms_ballVelocityXId, (char*)&this->m_currentState.vx, sizeof(double) ); pBallAttributes->add( Ball::ms_ballVelocityYId, (char*)&this->m_currentState.vy, sizeof(double) ); } //! pClockAttributes is allocated on the heap and must be //! deallocated by the calling function return pBallAttributes; } void Ball::sendUpdate() { if ( m_nextStateCalculated == RTI::RTI_TRUE ) { m_currentState = m_nextState; m_nextStateCalculated = RTI::RTI_FALSE; try { /*! * In order to send the values of our attributes, we must * construct an AttributeHandleValuePairSet (AHVPS) which * is a set comprised of attribute handles, values, and * the size of the values. */ RTI::AttributeHandleValuePairSet* pNvpSet = createNVPSet(); /*! * Send the AHVPS to the federation. * * this call returns an event retraction handle but we * don't support event retraction so no need to store it. */ (void) ms_rtiAmb->updateAttributeValues( getInstanceId(), *pNvpSet, NULL ); //! Must free the memory pNvpSet->empty(); delete pNvpSet; } catch ( RTI::Exception& e ) { cerr << "BALL: Error:" << &e << endl; } } } void Ball::update( RTI::InteractionClassHandle theInteraction, const RTI::ParameterHandleValuePairSet& theParameters ) { if (ms_rtiAmb) // if ms_rtiAmb!=NULL { if ( theInteraction == Ball::ms_clockTickId ) { RTI::ParameterHandle paramHandle; RTI::ULong
As Zac mentioned, show only the relevant code. All that extraneous code just gets in the way. Formatting your code with <pre> tags goes a long way, too.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
RTI::AttributeHandleValuePairSet* Ball::createNVPSet() { RTI::AttributeHandleValuePairSet* pBallAttributes = NULL; //! Make sure the RTI Ambassador is set. if ( ms_rtiAmb ) // if ms_rtiAmb!=NULL { pBallAttributes = RTI::AttributeSetFactory::create( 4 ); pBallAttributes->add( Ball::ms_ballLocationXId, (char*)&this->m_currentState.x, sizeof(double) ); pBallAttributes->add( Ball::ms_ballLocationYId, (char*)&this->m_currentState.y, sizeof(double) ); pBallAttributes->add( Ball::ms_ballVelocityXId, (char*)&this->m_currentState.vx, sizeof(double) ); pBallAttributes->add( Ball::ms_ballVelocityYId, (char*)&this->m_currentState.vy, sizeof(double) ); } //! pClockAttributes is allocated on the heap and must be //! deallocated by the calling function return pBallAttributes; } void Ball::sendUpdate() { if ( m_nextStateCalculated == RTI::RTI_TRUE ) { m_currentState = m_nextState; m_nextStateCalculated = RTI::RTI_FALSE; try { /*! * In order to send the values of our attributes, we must * construct an AttributeHandleValuePairSet (AHVPS) which * is a set comprised of attribute handles, values, and * the size of the values. */ RTI::AttributeHandleValuePairSet* pNvpSet = createNVPSet(); /*! * Send the AHVPS to the federation. * * this call returns an event retraction handle but we * don't support event retraction so no need to store it. */ (void) ms_rtiAmb->updateAttributeValues( getInstanceId(), *pNvpSet, NULL ); //! Must free the memory pNvpSet->empty(); delete pNvpSet; } catch ( RTI::Exception& e ) { cerr << "BALL: Error:" << &e << endl; } } } void Ball::update( RTI::InteractionClassHandle theInteraction, const RTI::ParameterHandleValuePairSet& theParameters ) { if (ms_rtiAmb) // if ms_rtiAmb!=NULL { if ( theInteraction == Ball::ms_clockTickId ) { RTI::ParameterHandle paramHandle; RTI::ULong
The call in the sendUpdate method is operating on only the current Ball object. The call in the update method is operating on a Ball object that was contained in the array it is iterating through.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
As Zac mentioned, show only the relevant code. All that extraneous code just gets in the way. Formatting your code with <pre> tags goes a long way, too.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
RTI::AttributeHandleValuePairSet* Ball::createNVPSet()
{
RTI::AttributeHandleValuePairSet* pBallAttributes = NULL;//! Make sure the RTI Ambassador is set.
if ( ms_rtiAmb ) // if ms_rtiAmb!=NULL
{
pBallAttributes = RTI::AttributeSetFactory::create( 4 );pBallAttributes->add( Ball::ms_ballLocationXId,
(char*)&this->m_currentState.x,
sizeof(double) );
pBallAttributes->add( Ball::ms_ballLocationYId,
(char*)&this->m_currentState.y,
sizeof(double) );
pBallAttributes->add( Ball::ms_ballVelocityXId,
(char*)&this->m_currentState.vx,
sizeof(double) );
pBallAttributes->add( Ball::ms_ballVelocityYId,
(char*)&this->m_currentState.vy,
sizeof(double) );
}//! pClockAttributes is allocated on the heap and must be
//! deallocated by the calling function
return pBallAttributes;
}void Ball::sendUpdate()
{
if ( m_nextStateCalculated == RTI::RTI_TRUE )
{
m_currentState = m_nextState;
m_nextStateCalculated = RTI::RTI_FALSE;try
{
/*!
* In order to send the values of our attributes, we must
* construct an AttributeHandleValuePairSet (AHVPS) which
* is a set comprised of attribute handles, values, and
* the size of the values.
*/
RTI::AttributeHandleValuePairSet* pNvpSet = createNVPSet();
/*!
* Send the AHVPS to the federation.
*
* this call returns an event retraction handle but we
* don't support event retraction so no need to store it.
*/
(void) ms_rtiAmb->updateAttributeValues( getInstanceId(),
*pNvpSet,
NULL );
//! Must free the memory
pNvpSet->empty();
delete pNvpSet;
}
catch ( RTI::Exception& e )
{
cerr << "BALL: Error:" << &e << endl;
}
}
}void Ball::update(
RTI::InteractionClassHandle theInteraction,
const RTI::ParameterHandleValuePairSet& theParameters )
{
if (ms_rtiAmb) // if ms_rtiAmb!=NULL
{
if ( theInteraction == Ball::ms_clockTickId )
{
RTI::ParameterHandle paramHandle;
RTI::ULong valueLength;//! We need to iterate through the AttributeHandleValuePairSet
//! to extract each AttributeHandleValuePair. Based on the type
//! specified ( the value returned by getHandle() ) we need to
//! extract the data frlom the buffer that is returned by
//! getValue().
for ( unsigned int i = 0; i < theParameters.size(); i++ )
{
paramHandle = theParameters.getHandle( i );
if ( paramHandle == Ball::ms_clockTickIntervalId )
{
double timeInterval = 1 / 60.0;
theParameters.getValue( i, (char*)&timeInterval, valueLength );//! Update all clock objects
for ( unsigned int j = 0; j < ms_numInstances; j++ )
{
( *( (Ball*)(ms_instances[ -
As Zac mentioned, show only the relevant code. All that extraneous code just gets in the way. Formatting your code with <pre> tags goes a long way, too.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
RTI::AttributeHandleValuePairSet* Ball::createNVPSet()
{
RTI::AttributeHandleValuePairSet* pBallAttributes = NULL;//! Make sure the RTI Ambassador is set.
if ( ms_rtiAmb ) // if ms_rtiAmb!=NULL
{
pBallAttributes = RTI::AttributeSetFactory::create( 4 );pBallAttributes->add( Ball::ms_ballLocationXId,
(char*)&this->m_currentState.x,
sizeof(double) );
pBallAttributes->add( Ball::ms_ballLocationYId,
(char*)&this->m_currentState.y,
sizeof(double) );
pBallAttributes->add( Ball::ms_ballVelocityXId,
(char*)&this->m_currentState.vx,
sizeof(double) );
pBallAttributes->add( Ball::ms_ballVelocityYId,
(char*)&this->m_currentState.vy,
sizeof(double) );
}//! pClockAttributes is allocated on the heap and must be
//! deallocated by the calling function
return pBallAttributes;
}void Ball::sendUpdate()
{
if ( m_nextStateCalculated == RTI::RTI_TRUE )
{
m_currentState = m_nextState;
m_nextStateCalculated = RTI::RTI_FALSE;try
{
/*!
* In order to send the values of our attributes, we must
* construct an AttributeHandleValuePairSet (AHVPS) which
* is a set comprised of attribute handles, values, and
* the size of the values.
*/
RTI::AttributeHandleValuePairSet* pNvpSet = createNVPSet();
/*!
* Send the AHVPS to the federation.
*
* this call returns an event retraction handle but we
* don't support event retraction so no need to store it.
*/
(void) ms_rtiAmb->updateAttributeValues( getInstanceId(),
*pNvpSet,
NULL );
//! Must free the memory
pNvpSet->empty();
delete pNvpSet;
}
catch ( RTI::Exception& e )
{
cerr << "BALL: Error:" << &e << endl;
}
}
}void Ball::update(
RTI::InteractionClassHandle theInteraction,
const RTI::ParameterHandleValuePairSet& theParameters )
{
if (ms_rtiAmb) // if ms_rtiAmb!=NULL
{
if ( theInteraction == Ball::ms_clockTickId )
{
RTI::ParameterHandle paramHandle;
RTI::ULong valueLength;//! We need to iterate through the AttributeHandleValuePairSet
//! to extract each AttributeHandleValuePair. Based on the type
//! specified ( the value returned by getHandle() ) we need to
//! extract the data frlom the buffer that is returned by
//! getValue().
for ( unsigned int i = 0; i < theParameters.size(); i++ )
{
paramHandle = theParameters.getHandle( i );
if ( paramHandle == Ball::ms_clockTickIntervalId )
{
double timeInterval = 1 / 60.0;
theParameters.getValue( i, (char*)&timeInterval, valueLength );//! Update all clock objects
for ( unsigned int j = 0; j < ms_numInstances; j++ )
{
( *( (Ball*)(ms_instances[