I am somewhat familiar with the older version of Borland C++ but;
In the UDPPacketClass example, there is the following comment and code:
// Note that the UDPPacket instance below is enclosed by '{'
// braces to handle the scope of the data buffers used by
// the C++ class instance.
{
UDPPacket pkt;
pkt.SetSourcePort( portnum );
pkt.SetDestinationPort( portnum );
pkt.AddData( buffer );
pkt.AddDataByte( 0 );
pkt.Send( ipaddr );
}
In this example the class definition for UDPPacket is not available.
What do the brackets do?
If the brackets weren't there, what would happen.
Is this a KR standard, or particular to NNDK?
Is there a document that explains C++ within the NNDK domain, or do I just go out and buy any old C++ for dummies book?
JK
C++ Scope
- Chris Ruff
- Posts: 222
- Joined: Thu Apr 24, 2008 4:09 pm
- Location: topsail island, nc
- Contact:
Re: C++ Scope
Post subject: C++ Scope
>I am somewhat familiar with the older version of Borland C++ but;
>
>In the UDPPacketClass example, there is the following comment and code:
>// Note that the UDPPacket instance below is enclosed by '{'
>// braces to handle the scope of the data buffers used by
>// the C++ class instance.
>{
>UDPPacket pkt;
>pkt.SetSourcePort( portnum );
>pkt.SetDestinationPort( portnum );
>pkt.AddData( buffer );
>pkt.AddDataByte( 0 );
>pkt.Send( ipaddr );
>}
>In this example the class definition for UDPPacket is not available.
>What do the brackets do?
the brackets specify a scope for any variables that are instantiated inside. When the code leaves the last bracket the generated variables go "out-of-scope" and are overwritten or deleted.
>If the brackets weren't there, what would happen.
If compiled in C++, the variable would remain on the stack until a larger unit outside went out of scope, like the function the code is in. The code would still work.
>Is this a KR standard, or particular to NNDK?
It is either a simple programming style in C++, or in the case of 'C' absolutely necessary because in C all variables must be declared at the top of a function or in the module space (globals). And unless the code here is at the top of a function it would cause a compiler error without the brackets. In C you can declare variables wherever you are if you declare a namespace with { ... }
>Is there a document that explains C++ within the NNDK domain, or do I just go out and buy any old C++ for >dummies book?
"Thinking in C++" by Bruce Eckel. Great book!
Chris
>I am somewhat familiar with the older version of Borland C++ but;
>
>In the UDPPacketClass example, there is the following comment and code:
>// Note that the UDPPacket instance below is enclosed by '{'
>// braces to handle the scope of the data buffers used by
>// the C++ class instance.
>{
>UDPPacket pkt;
>pkt.SetSourcePort( portnum );
>pkt.SetDestinationPort( portnum );
>pkt.AddData( buffer );
>pkt.AddDataByte( 0 );
>pkt.Send( ipaddr );
>}
>In this example the class definition for UDPPacket is not available.
>What do the brackets do?
the brackets specify a scope for any variables that are instantiated inside. When the code leaves the last bracket the generated variables go "out-of-scope" and are overwritten or deleted.
>If the brackets weren't there, what would happen.
If compiled in C++, the variable would remain on the stack until a larger unit outside went out of scope, like the function the code is in. The code would still work.
>Is this a KR standard, or particular to NNDK?
It is either a simple programming style in C++, or in the case of 'C' absolutely necessary because in C all variables must be declared at the top of a function or in the module space (globals). And unless the code here is at the top of a function it would cause a compiler error without the brackets. In C you can declare variables wherever you are if you declare a namespace with { ... }
>Is there a document that explains C++ within the NNDK domain, or do I just go out and buy any old C++ for >dummies book?
"Thinking in C++" by Bruce Eckel. Great book!
Chris
Real Programmers don't comment their code. If it was hard to write, it should be hard to understand
Re: C++ Scope
Thanks for taking the time to reply and the book recommendation.
I guess I am still somewhat confused.
I had always thought that a variable instantiated inside a function was "In scope" while you were in that function.
UDPPacket pkt is instantiated inside main() and all the following statement after UDPPacket pkt are also within main().
Is the goal here to force UDPPacket pkt out of scope to allow the stack more room?
I am going to take a quantum leap in logic and "assume"
that this could be significant.
In a multi tasking environment other tasks might need stack resources; is this a way having more control over freeing those resources?
I guess I am still somewhat confused.
I had always thought that a variable instantiated inside a function was "In scope" while you were in that function.
UDPPacket pkt is instantiated inside main() and all the following statement after UDPPacket pkt are also within main().
Is the goal here to force UDPPacket pkt out of scope to allow the stack more room?
I am going to take a quantum leap in logic and "assume"

In a multi tasking environment other tasks might need stack resources; is this a way having more control over freeing those resources?
Re: C++ Scope
I think the point Chris was making is that this is a C++ thing. You would be correct for C.