Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Local function emulation (using struct) and scope of "static"

Reply
Thread Tools

Local function emulation (using struct) and scope of "static"

 
 
A
Guest
Posts: n/a
 
      05-20-2010
Here is an example function:

void MyFunction()
{
struct TLocal
{
static bool Check(int i)
{
return true;
}
};

TLocal::Check(1);
}

This is how I call local function "Check". This function should have scope
only within MyFunction as far as I know and lose scope once MyFunction is
done. Is this true?

If I remove "static" then TLocal::Check(1); call will not work - can anyone
explain why?

Also, is there any other (better in some way or easier in some way) method
to call local functions? Would it make some difference if I used "class"
instead of "struct"?


 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      05-20-2010
On 5/19/2010 8:35 PM, A wrote:
> Here is an example function:
>
> void MyFunction()
> {
> struct TLocal
> {
> static bool Check(int i)
> {
> return true;
> }
> };
>
> TLocal::Check(1);
> }
>
> This is how I call local function "Check". This function should have scope
> only within MyFunction as far as I know and lose scope once MyFunction is
> done. Is this true?
>
> If I remove "static" then TLocal::Check(1); call will not work - can anyone
> explain why?


Because then Check would need an object:

TLocal t;
t.Check(1);

>
> Also, is there any other (better in some way or easier in some way) method
> to call local functions?


There's really no other way to define "local" functions -- C++ does not
have nested/local functions.

> Would it make some difference if I used "class"
> instead of "struct"?


No. Classes and structs are identical, except for default access
specifiers.

 
Reply With Quote
 
 
 
 
Gert-Jan de Vos
Guest
Posts: n/a
 
      05-20-2010
On May 20, 5:35*am, "A" <a...@a.a> wrote:
> Here is an example function:
>
> void MyFunction()
> {
> struct TLocal
> * * * * {
> * * * * static bool Check(int i)
> * * * * * * * * {
> * * * * * * * * return true;
> * * * * * * * * }
> * * * * };
>
> TLocal::Check(1);
>
> }
>
> This is how I call local function "Check". This function should have scope
> only within MyFunction as far as I know and lose scope once MyFunction is
> done. Is this true?


Correct.

> If I remove "static" then TLocal::Check(1); call will not work - can anyone
> explain why?


static allows you to call the Check function without first creating an
instance
(object) of the TLocal class.

> Also, is there any other (better in some way or easier in some way) method
> to call local functions? Would it make some difference if I used "class"
> instead of "struct"?


Use the functor idiom:

void MyFunction()
{
struct Checker
{
bool operator()(int i)
{
return true;
}
} check;

check(1);
}

More or less the same but no static, no TLocal::. The syntax of a
local functor is further reduced by C++0x's lambda's.
 
Reply With Quote
 
thomas
Guest
Posts: n/a
 
      05-20-2010
On May 20, 11:35*am, "A" <a...@a.a> wrote:
> Here is an example function:
>
> void MyFunction()
> {
> struct TLocal
> * * * * {
> * * * * static bool Check(int i)
> * * * * * * * * {
> * * * * * * * * return true;
> * * * * * * * * }
> * * * * };
>
> TLocal::Check(1);
>
> }
>
> This is how I call local function "Check". This function should have scope
> only within MyFunction as far as I know and lose scope once MyFunction is
> done. Is this true?

Yeap..

>
> If I remove "static" then TLocal::Check(1); call will not work - can anyone
> explain why?

Without static you need an object to invoke a method.

>
> Also, is there any other (better in some way or easier in some way) method
> to call local functions? Would it make some difference if I used "class"
> instead of "struct"?


Why do you want "local functions"(your words)?
Maybe the only reanable explanation is avoiding polluting name space.
Then declare a class wrapping your "MyFunction()" interface and make
your local function private.
Or define a new namespace.
Either way is better than your "local function".
 
Reply With Quote
 
A
Guest
Posts: n/a
 
      05-20-2010
> Why do you want "local functions"(your words)?
> Maybe the only reanable explanation is avoiding polluting name space.
> Then declare a class wrapping your "MyFunction()" interface and make
> your local function private.
> Or define a new namespace.
> Either way is better than your "local function".


i need them because in the same function i have a set of actions that are
always identical with one parameter as exception. so i can fit them nicely
in a local function within a function that has only this parameter
different. otherwise i would have to repeat a block of code over and over
which would make a mess.

as i have no use of this block of code anywhere else than in this particular
function it makes sense to use such "local functions".

unless you have a better idea how to place a repeatable block of code that
is used only in that function?


 
Reply With Quote
 
werasm
Guest
Posts: n/a
 
      05-21-2010
On May 20, 1:38*pm, thomas <freshtho...@gmail.com> wrote:

> Why do you want "local functions"(your words)?


Because sometimes the code in the local function is only applicable
(usable) from one function (and makes no sense from other contexts).
I've found that sometimes this prevents (local) code duplication.

Should I then now go and stick it in some unnamed namespace, think out
some name for it and confuse others as to its use, or would "local" be
more encapsulating?

> Maybe the only reanable explanation is avoiding polluting name space.


This would not be the case if we use an unnamed namespace, but our
name would have to be unique, not?

> Then declare a class wrapping your "MyFunction()" interface and make
> your local function private.


I hope you don't suggest that I should pollute my interface, which for
me is worse than polluting some namespace. Anything that need not be
in my interface should not be there. Perhaps a pimpl with access to
the this pointer would suffice as local (but still not local enough).

> Or define a new namespace.
> Either way is better than your "local function".


I for one, don't agree with this in all circumstances. I like Gert-
Jan's idiom and I've often used it.

Regards,

Werner
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
Modify the local scope inside a function Sandra-24 Python 5 03-01-2006 11:10 AM
Problems with emulation of numeric types and coercion rules ziga.seilnacht@gmail.com Python 1 11-02-2005 02:17 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
Diff between Runtime and Emulation Environment of personal java samindla venkateswar rao Java 0 11-27-2004 08:46 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57