Note that there are some explanatory texts on larger screens.

plurals
  1. POstatic behaviour->objective C
    primarykey
    data
    text
    <p>I have this <code>static</code> declaration of <code>m_pData=1099656</code> in a <code>class</code>.</p> <p>In <code>main</code> <code>NSLog(@"Testing:%d",m_pData);</code></p> <p>Its printing me <code>0</code>. </p> <p>How to get the same value of <code>m_pData</code> in the class in the main function too.</p> <p>I cant use <code>obj.m_pData</code> or <code>obj-&gt;m_pData</code> to acces the variable.</p> <p>EDITED:</p> <pre><code>test2.m ---------- #import &lt;Foundation/Foundation.h&gt; #import "data_derived.h" int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; data* dat = [data alloc]; requestSession* session = [requestSession alloc]; [session init]; [dat TxCreateImage:RM_REQUEST_SESSION]; NSLog(@"Testing:%d",m_pData); //the static variable is not printing the value its holding.Its printing Zero.If printed the same variable inside the class it gives some numbers. [dat dataBuffer:&amp;m_pData withLen:&amp;m_uDataSize]; //here the actual values of static values are not passed.when printed both of them contains zero values. [pool drain]; return 0; } data.h -------- #import &lt;Foundation/Foundation.h&gt; #import "remote.h" static int m_nMessageId; //Message ID static int m_uSessionId; //Session ID static int m_chSequenceChar; //Sequence ID static int* m_pData; //Integer buffer to carry data static int m_uDataSize; //Datasize @interface data : NSObject { @public } - (id)initWithID:(int) uMessageId withData:(id)pData withSize:(size_t) uDataSize; + (void)initialize; - (void)dealloc; - (id) dataBuffer:(int**)m_Data withLen:(int**)uLen; - (BOOL) TxCreateImage:(int)messageId; @end data.m --------- #import "data.h" #define ENCODED_MSG_DATA_OFFSET 8 @implementation data + (void)initialize { m_uSessionId = 0; m_chSequenceChar= 0; // Initialize values from derived class m_nMessageId = 0; m_pData = 0; m_uDataSize = 0; } - (id) initWithID:(int) uMessageId withData:(id)pData withSize:(size_t) uDataSize { if(self=[super init]) { // Initialize the member variables m_uSessionId = 0xFF; m_chSequenceChar= 10; // Initialize values from derived class m_nMessageId = uMessageId; m_pData = (int*)pData; m_uDataSize = (int)uDataSize; } NSLog(@"Data size:%d",uDataSize); NSLog(@"m_pData:%d",m_pData); NSLog(@"pData:%d",pData); return self; } - (id) dataBuffer:(int**)m_Data withLen:(int**)uLen { if ( m_uDataSize &lt;= RMH_MAX_ENCODED_LENGTH ) { int abBigEndian[RMH_MESSAGE_MAX_SIZE]; memcpy(abBigEndian,m_Data,m_uDataSize); NSLog(@"m_Data:%d",*m_Data); NSLog(@"abBigEndian:%d",abBigEndian); uLen += ENCODED_CRC_BYTE_LEN + 1; NSLog(@"%d",*uLen); } NSLog(@"END!"); return self; } - (BOOL) TxCreateImage:(int)messageId { char pData[4096]; sprintf(pData,"%x %d %d %d %x",ASCII_STX,m_uSessionId,m_chSequenceChar,m_nMessageId,ASCII_ETX); //uLen = ENCODED_MSG_DATA_OFFSET; NSLog(@"%s",pData); return YES; } - (void)dealloc { [super dealloc]; } @end data_derived.h --------------------- #import &lt;Foundation/Foundation.h&gt; #import "data.h" #define DECLARE_RS232_NEWMSG(ClassID)\ enum \ { \ ID = ClassID \ }; \ @interface requestSession : data { @public DECLARE_RS232_NEWMSG(RM_REQUEST_SESSION); struct RMH_REQUEST_SESSION_MSG st; } -(id)init; -(void)dealloc; @end data_derived.m --------------------- #import "data_derived.h" @implementation requestSession - (id)init { size_t asize = sizeof(st); st.uDeviceID = RS232_PROTOCOL_DEVICE_ID; st.uProtocolVersion = RS232_VERSION; memset(st.uReserved,0x00,sizeof(st.uReserved)); NSLog(@"Address of the structure:%d",&amp;st); self=[super initWithID:ID withData:(id)&amp;st withSize:asize]; if (self) { } return self; } - (void)dealloc { [super dealloc]; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload