Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #79397
| From | Paavo Helde <myfirstname@osa.pri.ee> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Overloading the [] operator for read/write... |
| Date | 2021-05-02 22:15 +0300 |
| Organization | A noiseless patient Spider |
| Message-ID | <s6mtnu$umn$1@dont-email.me> (permalink) |
| References | <s67oai$cm6$1@dont-email.me> <s68qva$sls$1@dont-email.me> <s6hrjh$gup$1@dont-email.me> |
01.05.2021 00:08 TDH1978 kirjutas:
> On 2021-04-27 11:02:01 +0000, Paavo Helde said:
>
>> You need to add a streaming operator for your proxy. Something like:
>>
>> std::ostream& operator<<(std::ostream& os, const Proxy& proxy) {
>> os << proxy.get_value();
>> return os;
>> }
>
> Thank you; I'm sure that would work, but I would have expected
> operator<<() to prefer the 'const':
>
> X operator[](const Y& y) const
>
> over the 'non-const':
>
> MyDBProxy_& operator[](const Y& y)
>
Which operator<<()? I your example there was none, and there is no default.
I guess you mean the line:
> cout << mydb[y] << endl; // compiler error; tries to print proxy
object and not 'x'.
Here, operator[] is called before passing the result to any operator<<,
existing or not. IOW, the meaning of 'mydb[y]' does not change depending
on the expression where it appears in.
Whether a const or non-const overload is preferred, is determined simply
by if 'mydb' is const or non-const. You can always cast to const if
that's what you need:
const MyDB& cmydb = mydb;
cout << cmydb[y] << endl
> Do you know why the compiler is choosing one over the other?
>
> What if, instead of operator<<(), I had a function:
>
> void foo(const X& x);
>
> and I pass it:
>
> foo(mydb[y]);
>
> Would the compiler complain because it thinks mydb[y] returns the proxy
> and not x?
Why don't you try out and see it by yourself what happens? Such
experiments are a must if you want to understand how a language works.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Overloading the [] operator for read/write... TDH1978 <thedeerhunter1978@movie.uni> - 2021-04-26 21:10 -0400
Re: Overloading the [] operator for read/write... Paavo Helde <myfirstname@osa.pri.ee> - 2021-04-27 14:02 +0300
Re: Overloading the [] operator for read/write... TDH1978 <thedeerhunter1978@movie.uni> - 2021-04-30 17:08 -0400
Re: Overloading the [] operator for read/write... Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-02 22:15 +0300
Re: Overloading the [] operator for read/write... Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-03 09:27 +0200
Re: Overloading the [] operator for read/write... "daniel...@gmail.com" <danielaparker@gmail.com> - 2021-05-03 07:57 -0700
Re: Overloading the [] operator for read/write... Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-03 19:13 +0300
Re: Overloading the [] operator for read/write... "daniel...@gmail.com" <danielaparker@gmail.com> - 2021-05-03 11:33 -0700
Re: Overloading the [] operator for read/write... David Brown <david.brown@hesbynett.no> - 2021-05-04 18:34 +0200
csiph-web