This is a discussion on Doubt with Scope within the C and C++ forums, part of the Programming Talk category; I have my C++ program as below: int main() { sample s1; test t1 = s1.example(); cout << "t1....
|
|||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I have my C++ program as below:
int main() { sample s1; test t1 = s1.example(); cout << "t1.a = "<< t1.a << endl; cout << "t1.b = "<< t1.b << endl; }; struct test { int a; int b; }; struct sample { test example() { test t; t.a = 100; t.b = 200; return t; } }; I have a doubt in the above C++ program. I want to know the scope of availability of the object test that is created inside the function example(). Will it get lost once the method gets completed? Kindly clarify my doubt. |
|
|||
|
yes t gets created when the control reaches the function
and destroyed when control exits from the function.but the value is safely returned to the main.returning a locally created refrence variable is unsafe. in this example u must declare struct test and struct samplebefore main in order to tell the main function that u have created test and sample somwhere in the program |
![]() |
| Thread Tools | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| which has future scope in J2EE or Mainframes(cobol)? | chilledice01 | COBOL | 5 | 06-24-2007 10:01 AM |
| Doubt on Functioning of Query | caradoc | Oracle Database | 1 | 05-18-2007 04:00 PM |
| wot exactly is da scope of CCNA????? | anand.karan | CISCO Certification | 5 | 05-27-2006 06:55 AM |
| Scope of CCNA | mtajim | CISCO Certification | 4 | 04-29-2006 07:25 AM |
| Scope international, Chennai Freshers Jobs | techguru | Freshers Jobs | 0 | 06-08-2005 07:01 AM |