Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #12735

Re: Confused about casts

From "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Newsgroups comp.lang.c++
Subject Re: Confused about casts
Date 2011-12-21 20:32 +0100
Organization A noiseless patient Spider
Message-ID <jctc93$q2e$1@dont-email.me> (permalink)
References <jcre8g$ecq$1@speranza.aioe.org> <jcrg8s$2o8$1@dont-email.me> <jcta4v$kfn$1@speranza.aioe.org>

Show all headers | View raw


On 21.12.2011 19:56, Paulo da Silva wrote:
> Em 21-12-2011 02:28, Alf P. Steinbach escreveu:
>> On 21.12.2011 02:54, Paulo da Silva wrote:
>>>
>>> using namespace std;
>>>
>>> #include<iostream>
>>> #include<fstream>
>>>
>>> int main(int argc,char **argv)
>>> {    ifstream *in_p;
>>>      int val;
>>>      if (argc==2)
>>>      {    in_p=new ifstream(argv[1]);
>>>          // Test in_p->good()
>>>      }
>>>      else
>>>          in_p=static_cast<ifstream*>(&cin);
>>>      *in_p>>   val;
>>>      cout<<   "val="<<   val<<   endl;
>>>      if (in_p!=&cin)
>>>          delete in_p;
>>>      return 0;
>>> }
>>>
>>> In the line "in_p=static_cast<ifstream*>(&cin);" both static and
>>> reinterpret casts seem to work. Which is (more) correct?
>>
>> They are both technically allowed, and both are *wrong*.
>>
>> static_cast is allowed because ifstream is derived from istream so that
>> the opposite direction would convert implicitly.
>>
>> reinterpret_cast is allowed because just about any data pointers can be
>> reinterpret_cast'ed, although for cases outside the conversions listed
>> here and there in the standard the result (and whether it's allowed)
>> depends on the compiler and compiler options.
>>
>> You're doing a so called *downcast*, to a more derived class.
>>
>> That's only proper if the referred to object is of the derived class (or
>> more derived).
>>
>> Here's what you should do, given the apparent goals of your code:
>>
>>
>> <code>
>> using namespace std;
>>
>> #include<iostream>
>> #include<fstream>
>>
>> void do_things( istream&  in )
>> {
>>      int val;
>>
>>      in>>  val;
>>      cout<<  "val="<<  val<<  endl;
>> }
>>
>> int main( int argc,char *argv[] )
>> {
>>      if (argc==2)
>>      {
>>          ifstream f( argv[1] );
>>          // Test in_p->good()
>>          do_things( f );
>>      }
>>      else
>>      {
>>          do_things( cin );
>>      }
>> }
>> </code>
> Thanks!
> But using the same logic, may be a little change fixes the problem.
> Isn't this the same?
>
> <code>
> using namespace std;
>
> #include<iostream>
> #include<fstream>
>
> int main(int argc,char **argv)
> {	istream *in_p;
> 	int val;
> 	if (argc==2)
> 	{	in_p=new ifstream(argv[1]);
> 		// Test in_p->good()
> 	}
> 	else
> 		in_p=&cin;
> 	*in_p>>  val;
> 	cout<<  "val="<<  val<<  endl;
> 	if (in_p!=&cin)
> 		delete in_p;
> 	return 0;
> }
> </code>


Well, it's not quite the same:

   * It uses dynamic allocation, which is generally slow.

   * It's in general not exception safe.

   * At least to my eyes, it's much less clear.

The problem is that this code addresses many concerns within the same 
little stretch of code.

Instead, try to separate concerns.


Cheers & hth.

- Alf

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Confused about casts Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> - 2011-12-21 01:54 +0000
  Re: Confused about casts "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-12-21 03:28 +0100
    Re: Confused about casts Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> - 2011-12-21 18:56 +0000
      Re: Confused about casts Leigh Johnston <leigh@i42.co.uk> - 2011-12-21 19:10 +0000
      Re: Confused about casts "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-12-21 20:32 +0100
    Re: Confused about casts Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> - 2011-12-21 19:32 +0000
    Re: Confused about casts Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> - 2011-12-21 19:34 +0000
  Re: Confused about casts (sorry) Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> - 2011-12-21 19:45 +0000

csiph-web