to pass in a unknown data and retrieving it

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • themadme
    New Member
    • Mar 2007
    • 53

    to pass in a unknown data and retrieving it

    i want to set a class back to its original structure after calling a receive function. code below to explain

    Code:
    class Node
    {
    	public:
    	Node(){}
    	~Node(){}
    };
    
    
    class lol  : public Node
    {
    	public:
    	string strName;
    };
    
    class face
    {
    	public:
    
    	void vSetLOL( CONST Node &C_lol )
    	{
    		this->C_lol  = C_lol;
    	}
    
    	const Node getLoL( void ) const
    	{
    		return C_lol;
    	}
    
    	priavte:
    
    	Node C_lol ;	
    }
    
    
    main
    {
    	lol  C_lol;
    	face C_face;
    
    	C_lol.strname = "gums";
    
    	C_face.vSetLOL( C_lol );
    
    	C_lol.strname = " monkey";
    
    
    	////// how do you change the node C_lol back to lol C_lol /////
    	C_lol = C_face.getLOL();  /// this is the main problem
    
    }
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    You may be able to cast C_face.getLOL() to a C_lol, or have the face class hold a lol object instead of a Node object.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      The bottom line is that you can't. As soon as you copied a class lol to a class Node the value of any data members held in the lol were lost forever.

      You may be able to write a constructor and operator= for lol that will take a Node & as the parameter but you will never be able to reconstruct any data held in lol.

      Comment

      • themadme
        New Member
        • Mar 2007
        • 53

        #4
        Thanks for the replies and sorry about the bad naming in the last example

        would that be the same problem if put a structure into a buffer.

        Code:
        //// class called Data
        Data::Data()
        {
            char *pcPacket = new char;
        }
        
        void Data::setPacketData( const char* pcPacketData )
        {
            *pcPacket = *pcPacketData;
        }
        
        void Data::getPacketData( const char* pcPacketData )
        {
            *pcPacketData = *pcPacket;
        }
        
        int main
        {
             stucture vector3
             {
                  int x,y, z;
             };
        
             Data C_Data;
             vector3 vec3;
        
              /// this would work
             C_Data.setPacketData( (char*)&vec3 );    
        
              /// this would not work, the returned data would not change?
              C_Data.getPacketData( (char*)&vec3 );    
            
             return 0;
        }
        so the returned data would be impossible to change or is there a mystic way? if was integers or floats that was put into the buffer, i know that would work but not with structures

        when i allocate the pcPacket, i should put it into an array, depending on the size of the structure that was sent right?

        Comment

        • themadme
          New Member
          • Mar 2007
          • 53

          #5
          oops found the problem, such amateur mistake, im only coping over one byte of data. someone slap me

          Comment

          • arnaudk
            Contributor
            • Sep 2007
            • 425

            #6
            *SLAP*!
            Oops, message too short... *SLAP* *SLAP* *SLAP* ;-)

            Comment

            • themadme
              New Member
              • Mar 2007
              • 53

              #7
              lol ok, new prob, will actually it not a problem yet.

              I would just some comments back to see what i'm doing is correct.


              Code:
              //////////// this would be done on both sides of the server and the client
              #define BUFFERSIZE 80
              
              struct PacketData
              {
              	float x,y,z;
              
              }S_PacketData;
              
              struct Data
              {
              	char *pcPacket;
              	char *cpName;
              }S_Data;
              
              S_Data.pcPacket = new char[ sizeof(PacketData) ];
              S_Data.cpName = NULL;
              
              char *NetPacket = new char[ sizeof(PacketData) + BUFFERSIZE ];
              
              //////////////////////////////////////////////////////////////////////////////////////
              
              //////// then on the client side it would do this to send information ////////////////
              
              S_PacketData.x = 5.0f;
              S_PacketData.y = 15.0f;
              S_PacketData.z = 25.0f;
              
              S_Data.cpName = "lols";
              
              memcpy( S_Data.pcPacket, &S_PacketData, sizeof(PacketData) );
              
              memcpy( NetPacket, &S_Data, sizeof(PacketData) + BUFFERSIZE );
              
              send(s, NetPacket, sizeof(PacketData) + BUFFERSIZE,0);
              
              
              ///////////// on the server would have this ///////////////////
              
              recv (s, NetPacket, sizeof(PacketData) + BUFFERSIZE,0);
              
              /////// is this correct so far? /////////////////////////////////

              Comment

              Working...