Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. calling a function two ways

calling a function two ways

Scheduled Pinned Locked Moved C / C++ / MFC
questionannouncement
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Jay03

    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(); ........... ........ }

    M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #2

    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 calling createNVPSet 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 the send function a free function or is it a member of a class ? same thing for update ? maybe in the context of the update function there is no way to create a PairSet except thrue a Ball object.


    Maximilien Lincourt Your Head A Splode - Strong Bad

    J 1 Reply Last reply
    0
    • J Jay03

      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(); ........... ........ }

      Z Offline
      Z Offline
      Zac Howland
      wrote on last edited by
      #3

      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

      J 1 Reply Last reply
      0
      • Z Zac Howland

        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

        J Offline
        J Offline
        Jay03
        wrote on last edited by
        #4

        sorry im missing some detail. The function createNVPSet is a member of class Ball. PairSet* Ball::CreateNVPSet() { ............ ............ return set; }

        J 1 Reply Last reply
        0
        • M Maximilien

          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 calling createNVPSet 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 the send function a free function or is it a member of a class ? same thing for update ? maybe in the context of the update function there is no way to create a PairSet except thrue a Ball object.


          Maximilien Lincourt Your Head A Splode - Strong Bad

          J Offline
          J Offline
          Jay03
          wrote on last edited by
          #5

          sorry im missing some detail. The function createNVPSet is a member of class Ball. PairSet* Ball::CreateNVPSet() { ............ ............ return set; }

          1 Reply Last reply
          0
          • J Jay03

            sorry im missing some detail. The function createNVPSet is a member of class Ball. PairSet* Ball::CreateNVPSet() { ............ ............ return set; }

            J Offline
            J Offline
            Jay03
            wrote on last edited by
            #6

            Is my initial argument correct now?

            Z 1 Reply Last reply
            0
            • J Jay03

              Is my initial argument correct now?

              Z Offline
              Z Offline
              Zac Howland
              wrote on last edited by
              #7

              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

              J 1 Reply Last reply
              0
              • Z Zac Howland

                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

                J Offline
                J Offline
                Jay03
                wrote on last edited by
                #8

                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

                Z D 2 Replies Last reply
                0
                • J Jay03

                  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

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #9

                  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

                  J 2 Replies Last reply
                  0
                  • J Jay03

                    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

                    Z Offline
                    Z Offline
                    Zac Howland
                    wrote on last edited by
                    #10

                    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

                    1 Reply Last reply
                    0
                    • D David Crow

                      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

                      J Offline
                      J Offline
                      Jay03
                      wrote on last edited by
                      #11

                      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[

                      T 1 Reply Last reply
                      0
                      • D David Crow

                        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

                        J Offline
                        J Offline
                        Jay03
                        wrote on last edited by
                        #12

                        it does actually...... does

                        take away the preprocessor statements

                        1 Reply Last reply
                        0
                        • J Jay03

                          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[

                          T Offline
                          T Offline
                          toxcct
                          wrote on last edited by
                          #13

                          David said "Relevant"


                          TOXCCT >>> GEII power

                          [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups