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


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


Please support your answers with examples. Any help is appreciated.
 
J

Jason Heyes

Alf P. Steinbach said:
* Jason Heyes:
[homework questions]

Off-topic; see the FAQ on HOMEWORK QUESTIONS.

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

How do I modify my post so that the forum doesn't interpret it as homework?
 
A

Alf P. Steinbach

* Jason Heyes:
[quoting signature]

Do not quote signatures; corrected; see FAQs on how to post.


* Jason Heyes:
* Alf P. Steinbach:
* Jason Heyes:
[homework questions]

Off-topic; see the FAQ on HOMEWORK QUESTIONS.

How do I modify my post so that the forum doesn't interpret it as homework?

You can't.

The question

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

is too strong evidence.
 
K

Karl Heinz Buchegger

Jason said:
Alf P. Steinbach said:
* Jason Heyes:
[homework questions]

Off-topic; see the FAQ on HOMEWORK QUESTIONS.

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

How do I modify my post so that the forum doesn't interpret it as homework?


Eg. by telling us your point of view of what the answers could be and why.
 
K

Karl Heinz Buchegger

Jason said:
Alf P. Steinbach said:
* Jason Heyes:
[homework questions]

Off-topic; see the FAQ on HOMEWORK QUESTIONS.

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

How do I modify my post so that the forum doesn't interpret it as homework?


Homework per se is not the problem.
But what we don't do: Answer homework question when we see no sign in the posting
that the OP has made an attempt at solving the puzzle on his/her own.

Just posting the assignment doesn't qualify as 'making an attempt'
 
J

Jeff Flinn

Alf P. Steinbach said:
* Jason Heyes:
[quoting signature]

Do not quote signatures; corrected; see FAQs on how to post.


* Jason Heyes:
* Alf P. Steinbach:
* Jason Heyes:
[homework questions]

Off-topic; see the FAQ on HOMEWORK QUESTIONS.

How do I modify my post so that the forum doesn't interpret it as
homework?

You can't.

The question

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

is too strong evidence.

I was just thinking. This guy seems to have cropped up just as another
poster with a similar personality disappeared by the name of JKop.

Jeff F
 
J

Jason Heyes

Karl Heinz Buchegger said:
Jason said:
Alf P. Steinbach said:
* Jason Heyes:
[homework questions]

Off-topic; see the FAQ on HOMEWORK QUESTIONS.

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

How do I modify my post so that the forum doesn't interpret it as
homework?


Homework per se is not the problem.
But what we don't do: Answer homework question when we see no sign in the
posting
that the OP has made an attempt at solving the puzzle on his/her own.

Just posting the assignment doesn't qualify as 'making an attempt'

I don't see the point of making hopeless attempts at questions I need help
with.
 
J

Jason Heyes

Karl Heinz Buchegger said:
Jason said:
Alf P. Steinbach said:
* Jason Heyes:
[homework questions]

Off-topic; see the FAQ on HOMEWORK QUESTIONS.

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

How do I modify my post so that the forum doesn't interpret it as
homework?


Eg. by telling us your point of view of what the answers could be and why.

I'd rather not speculate on what the answers to my own questions could be.
Thus the point of writing the question in the first place; that is, so you
guys can help me out!
 
K

Karl Heinz Buchegger

Jason said:
I'd rather not speculate on what the answers to my own questions could be.
Thus the point of writing the question in the first place; that is, so you
guys can help me out!

Tell us your speculation.
If it's correct, we let you know.
If it's not correct, we have something to work with.

In any case: The group is perfectly willing to discuss your thoughts.
What is correct on them, what is wrong on them.
But for this you need to tell us your thoughts.
 
B

Branimir Maksimovic

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?

What do you think?
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.

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

In order to answer your questions, I must know
what is addressable object and what is not?
Give just one example.
That would be a good start.

Greetings, Bane.
 
J

Jason Heyes

Branimir Maksimovic said:
What do you think?


What do you think?


In order to answer your questions, I must know
what is addressable object and what is not?
Give just one example.
That would be a good start.

An l-value is an addressable object. An r-value is a non-addressable object.
Example:

Bar bar = Bar();

The object 'bar' is an l-value and so is addressable. The object Bar() is an
r-value and so is non-addressable.
 
B

Branimir Maksimovic

Jason Heyes said:
An l-value is an addressable object. An r-value is a non-addressable object.
Example:

Bar bar = Bar();

The object 'bar' is an l-value and so is addressable. The object Bar() is an
r-value and so is non-addressable.

Great. You've just answered first two questions. Call foo with rvalue
object; c++ accept this syntax: Bar().member_function();

In order to answer question three we must know what is data sharing
and copy on write.

Greetings, Bane.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top