Two questions about lifetime and scope.

J

Jason Heyes

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

Gernot Frisch

Jason Heyes said:
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?
 
A

Alf P. Steinbach

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

Gernot Frisch

Alf P. Steinbach said:
* 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
 
J

Jason Heyes

Alf P. Steinbach said:
* 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.
 
R

Rolf Magnus

Gernot said:
Hm... I don't see any way now.





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

Now, you need a Bar to 'foo'.





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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top