473,385 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Two questions about lifetime and scope.

Consider the function with this prototype:

void foo(Bar bar);

1. Can a program call foo and allow only one addressable Bar object to exist
at a time?
Now consider the following program:

class Bar
{
int value;
public:
Bar() : value(0) { }
void change(int new_value) { value = new_value; }
};

void foo(Bar bar) { }

int main()
{
Bar bar;
bar.change(1);
foo(bar);
return 0;
}

2. Can this program be modified without change to foo and Bar so that:

i) main calls foo with the same value of bar, and
ii) only one addressable Bar object exists at a time.
Please support your answers with examples. Any help is appreciated.

Jul 22 '05 #1
6 1255

"Jason Heyes" <ja********@optusnet.com.au> schrieb im Newsbeitrag
news:41***********************@news.optusnet.com.a u...
Consider the function with this prototype:

void foo(Bar bar);

1. Can a program call foo and allow only one addressable Bar object
to exist
at a time?
Hm... I don't see any way now.
2. Can this program be modified without change to foo and Bar so
that:

i) main calls foo with the same value of bar, and

class Bar
{
public:
void foo() {this->foo_something();}
}

Now, you need a Bar to 'foo'.

ii) only one addressable Bar object exists at a time.


int IsOneBarThere=0;
class Bar
{
Bar() {if (IsOneBarThere++) assert(0);}
}

What exaclty are you trying to do?


Jul 22 '05 #2
* Jason Heyes:
[homework questions]


HOMEWORK is not answered here.

Previously you posted the same two questions with a third
question,

3. What is the practical significance, if any, of the previous two
questions in relation to data sharing and copy-on-write?

which says this is HOMEWORK.

Go away.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #3

"Alf P. Steinbach" <al***@start.no> schrieb im Newsbeitrag
news:41*****************@news.individual.net...
* Jason Heyes:
[homework questions]


HOMEWORK is not answered here.

Previously you posted the same two questions with a third
question,

3. What is the practical significance, if any, of the previous two
questions in relation to data sharing and copy-on-write?

which says this is HOMEWORK.


Oh. I've not seen this, but my solution uses a global variable. I'd
like to see the face of your professor when you show this... :)
-Gernot
Jul 22 '05 #4
"Alf P. Steinbach" <al***@start.no> wrote in message
news:41*****************@news.individual.net...
* Jason Heyes:
[homework questions]


HOMEWORK is not answered here.

Previously you posted the same two questions with a third
question,

3. What is the practical significance, if any, of the previous two
questions in relation to data sharing and copy-on-write?

which says this is HOMEWORK.

Go away.


Stop saying all my posts are homework. You've made everyone paranoid.
Jul 22 '05 #5
Gernot Frisch wrote:

"Jason Heyes" <ja********@optusnet.com.au> schrieb im Newsbeitrag
news:41***********************@news.optusnet.com.a u...
Consider the function with this prototype:

void foo(Bar bar);

1. Can a program call foo and allow only one addressable Bar object
to exist
at a time?


Hm... I don't see any way now.
2. Can this program be modified without change to foo and Bar so
that:

i) main calls foo with the same value of bar, and

class Bar
{
public:
void foo() {this->foo_something();}
}

Now, you need a Bar to 'foo'.

ii) only one addressable Bar object exists at a time.


int IsOneBarThere=0;
class Bar
{
Bar() {if (IsOneBarThere++) assert(0);}
}

What exaclty are you trying to do?


That code does not follow the requirement of not changing foo and Bar.

Jul 22 '05 #6
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:cn************@news.t-online.com...
Gernot Frisch wrote:

"Jason Heyes" <ja********@optusnet.com.au> schrieb im Newsbeitrag
news:41***********************@news.optusnet.com.a u...
Consider the function with this prototype:

void foo(Bar bar);

1. Can a program call foo and allow only one addressable Bar object
to exist
at a time?


Hm... I don't see any way now.
2. Can this program be modified without change to foo and Bar so
that:

i) main calls foo with the same value of bar, and

class Bar
{
public:
void foo() {this->foo_something();}
}

Now, you need a Bar to 'foo'.

ii) only one addressable Bar object exists at a time.


int IsOneBarThere=0;
class Bar
{
Bar() {if (IsOneBarThere++) assert(0);}
}

What exaclty are you trying to do?


That code does not follow the requirement of not changing foo and Bar.


This is correct.
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Martin Ziebart | last post by:
Hi ! The following PHP-Code: >> $conn=pg_connect ($conn_string); >> $query=pg_exec ($conn, $sql_statement); // it's pg_query for PHP >4.2.0 >> pg_close ($conn);
11
by: Jason Heyes | last post by:
Consider the function with this prototype: void foo(Bar bar); 1. Can a program call foo and allow only one addressable Bar object to exist at a time? Now consider the following program:
15
by: copx | last post by:
Q1: If an array is declared static in file A is it still valid to access it from file B? I mean if a function form file A which returns a pointer to a position inside of the array is called from...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
14
by: Frederick Gotham | last post by:
There is a common misconception, (one which I myself also held at one point), that a const reference can "extend the lifetime of a temporary". Examples such as the following are given: Snippet...
23
by: TefJlives | last post by:
Hi all, I'm learning a bit about C, and I have a few questions. I'm not trying to insult C or anything with these questions, they're just honestly things I don't get. It seems like pointers...
9
by: David | last post by:
With a non-server app there is one instance of the program running and one user 'using' it at a time. With this scenario I'm pretty comfortable with variable scope and lifetime. With a server app...
16
by: Haskell Prelude | last post by:
Hello Friends - Can anyone answer these C questions? 1. What is the effect of making an internal (local) variable static? 2. What is the effect of making an external (non-local) variable...
5
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.