tags:

views:

57

answers:

2

I have a object, namely "server" which loses it existence when the control of program is out of scope.

So in general for any such objects, and memory of objects, when a function is called from within a scope, is the object lost?

like

void main void
{
     if this and that
     { //scope
         do this
         call_func();
     }
 }//main ends


void call_func()
{
    working
    "utilise objects created in parent."
    return;
}

call_func will not be able to see whats created by the parent function? yes ? or no?

+1  A: 

As a general rule, the scope lasts between the curly brackets. So anything in call_func has its own scope, completely separate to the parent function.

When control returns to the parent function, the objects that were in scope prior to calling the function are still in scope to anything within the brackets.

The child method only has access to objects which are in the parent class. Objects which are defined in the parent method are only available if they are specifically passed (as parameters) to the child.

class testClass
{
    private int classLevelInt;

    private void Main()
    {
        int methodLevelInt;

        if (someTest)
        {
             int bracketLevelInt;

             // classLevelInt, methodLevelInt and bracketLevelInt all in scope
             ChildMethod();
             // classLevelInt, methodLevelInt and bracketLevelInt all in scope
        }

        // only classLevelInt and methodLevelInt are still in scope
    }

    private void ChildMethod()
    {
        // This method can see classLevelInt only
        // If access to other ints is required they must be passed to the method
    }
}
PaulG
Does ur answer mean that the call_func will not be able to see whats created by the parent function?
if its already mentioned in ur answer i cannot understand ..
@user. The child method can only see objects in the parent *class* (not method), along with any objects that are specifically passed to the child method
PaulG
+1  A: 

call_func will not be able to see whats created by the parent function? yes ? or no?

Imagine this scenario:

void ParentMethod1()
{
 object something1;
 call_func();
}

void ParentMethod2()
{
 object somethingdifferent1;
 call_func();
}

void call_func()
{
  object scopeObject;
}

What objects do you think should be available to call_func?

What if call_func is public and called from a completely different assembly?

Basically a function (method) can only access any variables created within, passed to it as parameters, or available as class level variables.

ck