Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Indirection operator and write access (http://www.velocityreviews.com/forums/t946565-indirection-operator-and-write-access.html)

Paradigm 06-02-2012 05:22 PM

Indirection operator and write access
 
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

Victor Bazarov 06-03-2012 01:31 PM

Re: Indirection operator and write access
 
On 6/2/2012 1:22 PM, Paradigm wrote:
> 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
--
I do not respond to top-posted replies, please don't ask

Alain Ketterlin 06-03-2012 02:27 PM

Re: Indirection operator and write access
 
Paradigm <prasoonsaurav.nit@gmail.com> writes:

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

Jamie 07-15-2012 07:10 AM

Re: Indirection operator and write access
 
Do you know what the "word" 'paradigm' means?

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

"Paradigm" <prasoonsaurav.nit@gmail.com> wrote in message
news:5657706f-2eff-4782-95c8-b086d750dee7@googlegroups.com...
> 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





All times are GMT. The time now is 06:27 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.