Exforsys

Online Training

Doubt with Scope

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....


Go Back   Exforsys > Programming Talk > C and C++

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-08-2007, 04:17 PM
Senior Member
 
Join Date: Apr 2006
Posts: 122
rachelle is on a distinguished road
Exclamation Doubt with Scope

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-08-2007, 06:56 PM
Junior Member
 
Join Date: Jun 2007
Posts: 3
tarungosain20 is on a distinguished road
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-10-2007, 04:17 PM
Senior Member
 
Join Date: Apr 2006
Posts: 125
ashlee is on a distinguished road
Yes as in Java in C++ also the scope gets lost as you have asked. Can you guess what happens if you make &t at the end instead of t. Try out or guess and tell me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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 On


Similar Threads

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


All times are GMT -4. The time now is 11:51 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.