By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,858 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

to pass in a unknown data and retrieving it

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

Expand|Select|Wrap|Line Numbers
  1. class Node
  2. {
  3.     public:
  4.     Node(){}
  5.     ~Node(){}
  6. };
  7.  
  8.  
  9. class lol  : public Node
  10. {
  11.     public:
  12.     string strName;
  13. };
  14.  
  15. class face
  16. {
  17.     public:
  18.  
  19.     void vSetLOL( CONST Node &C_lol )
  20.     {
  21.         this->C_lol  = C_lol;
  22.     }
  23.  
  24.     const Node getLoL( void ) const
  25.     {
  26.         return C_lol;
  27.     }
  28.  
  29.     priavte:
  30.  
  31.     Node C_lol ;    
  32. }
  33.  
  34.  
  35. main
  36. {
  37.     lol  C_lol;
  38.     face C_face;
  39.  
  40.     C_lol.strname = "gums";
  41.  
  42.     C_face.vSetLOL( C_lol );
  43.  
  44.     C_lol.strname = " monkey";
  45.  
  46.  
  47.     ////// how do you change the node C_lol back to lol C_lol /////
  48.     C_lol = C_face.getLOL();  /// this is the main problem
  49.  
  50. }
Nov 17 '08 #1
Share this Question
Share on Google+
6 Replies


Ganon11
Expert 2.5K+
P: 3,652
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.
Nov 17 '08 #2

Banfa
Expert Mod 5K+
P: 8,916
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.
Nov 18 '08 #3

P: 53
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.

Expand|Select|Wrap|Line Numbers
  1. //// class called Data
  2. Data::Data()
  3. {
  4.     char *pcPacket = new char;
  5. }
  6.  
  7. void Data::setPacketData( const char* pcPacketData )
  8. {
  9.     *pcPacket = *pcPacketData;
  10. }
  11.  
  12. void Data::getPacketData( const char* pcPacketData )
  13. {
  14.     *pcPacketData = *pcPacket;
  15. }
  16.  
  17. int main
  18. {
  19.      stucture vector3
  20.      {
  21.           int x,y, z;
  22.      };
  23.  
  24.      Data C_Data;
  25.      vector3 vec3;
  26.  
  27.       /// this would work
  28.      C_Data.setPacketData( (char*)&vec3 );    
  29.  
  30.       /// this would not work, the returned data would not change?
  31.       C_Data.getPacketData( (char*)&vec3 );    
  32.  
  33.      return 0;
  34. }
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?
Nov 18 '08 #4

P: 53
oops found the problem, such amateur mistake, im only coping over one byte of data. someone slap me
Nov 18 '08 #5

100+
P: 424
*SLAP*!
Oops, message too short... *SLAP* *SLAP* *SLAP* ;-)
Nov 18 '08 #6

P: 53
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.


Expand|Select|Wrap|Line Numbers
  1. //////////// this would be done on both sides of the server and the client
  2. #define BUFFERSIZE 80
  3.  
  4. struct PacketData
  5. {
  6.     float x,y,z;
  7.  
  8. }S_PacketData;
  9.  
  10. struct Data
  11. {
  12.     char *pcPacket;
  13.     char *cpName;
  14. }S_Data;
  15.  
  16. S_Data.pcPacket = new char[ sizeof(PacketData) ];
  17. S_Data.cpName = NULL;
  18.  
  19. char *NetPacket = new char[ sizeof(PacketData) + BUFFERSIZE ];
  20.  
  21. //////////////////////////////////////////////////////////////////////////////////////
  22.  
  23. //////// then on the client side it would do this to send information ////////////////
  24.  
  25. S_PacketData.x = 5.0f;
  26. S_PacketData.y = 15.0f;
  27. S_PacketData.z = 25.0f;
  28.  
  29. S_Data.cpName = "lols";
  30.  
  31. memcpy( S_Data.pcPacket, &S_PacketData, sizeof(PacketData) );
  32.  
  33. memcpy( NetPacket, &S_Data, sizeof(PacketData) + BUFFERSIZE );
  34.  
  35. send(s, NetPacket, sizeof(PacketData) + BUFFERSIZE,0);
  36.  
  37.  
  38. ///////////// on the server would have this ///////////////////
  39.  
  40. recv (s, NetPacket, sizeof(PacketData) + BUFFERSIZE,0);
  41.  
  42. /////// is this correct so far? /////////////////////////////////
Nov 18 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.