Indirection operator and write access

P

Paradigm

Hi

Here is the scenario:

There is a pointer variable 'x'.

For what type of 'x' would the following show a write access on 'x'

std::cout<<*x<<'\n'; //showing write access on x [what's its type?]

I could not think of any possible situation apart from overloaded '*' operator having write operation inside the definition itself.

Hope my question is clear.
Thanks
 
V

Victor Bazarov

Here is the scenario:

There is a pointer variable 'x'.

For what type of 'x' would the following show a write access on 'x'

std::cout<<*x<<'\n'; //showing write access on x [what's its type?]

I could not think of any possible situation apart from overloaded '*'
operator having write operation inside the definition itself.

You can't overload the operator* for pointers. That's a built-in one.
Once you dereference *x, you get a reference to the object. The pointer
variable does not have any play in dealing with that, so it seems that
the write access to 'x' itself has nothing to do with '*x'.

V
 
A

Alain Ketterlin

Paradigm said:
There is a pointer variable 'x'.

For what type of 'x' would the following show a write access on 'x'

std::cout<<*x<<'\n'; //showing write access on x [what's its type?]

What does "showing write access on x" mean?
I could not think of any possible situation apart from overloaded '*'
operator having write operation inside the definition itself.

You can't overload *. I can't see how x could me modified here, but note
that anything can happen inside the version of operator<<(ostream&,...)
called at that point. Here is an example:

class X { ... };
class Y {
public:
Y(const X & x) { ... }
};
ostream & operator<<(ostream & os, const Y & y) {...}

If x is of type X*, your line of code may well call the version of op<<
on Y, which in turn can do anything. This is not a write access to x,
but still execution of an arbitrary amount of code.

-- Alain.
 
J

Jamie

Do you know what the "word" 'paradigm' means?

(Deal with it, I top-posted, on purpose).
 

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,054
Latest member
TrimKetoBoost

Latest Threads

Top