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


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

Re: static_cast and std::vector

From john <gh14tq5@yahoo.com>
Newsgroups comp.lang.c++
Subject Re: static_cast and std::vector
Date 2012-01-20 09:58 -0500
Organization Aioe.org NNTP Server
Message-ID <jfbveg$ae0$1@speranza.aioe.org> (permalink)
References <jfbrpt$vd$1@speranza.aioe.org> <IIidnQOEtaTr7oTSnZ2dnUVZ8rydnZ2d@giganews.com>

Show all headers | View raw


On 1/20/2012 9:00 AM, Leigh Johnston wrote:
> On 20/01/2012 13:56, john wrote:
>> Hi,
>>
>> I am attempting to replace lots of C-style casts with static_cast's in
>> our code base. Most of these are simple, but there are a few that I'm
>> not sure what to do.
>>
>> Basically, the problem is shown in the code below. GetD0/GetD1 are the
>> original C-style casts that work fine. I would like to change these to
>> C++ style casts, but the GetDD0/GetDD1 functions do not compile.
>>
>> Is there a way to do this? Are there any potential problems I should be
>> aware of?
>>
>> Thanks.
>>
>> #include <vector>
>>
>> class Base {};
>> class Derived0 : public Base {};
>> class Derived1 : public Base {};
>>
>> class Y
>> {
>> std::vector<Base*> m_vb[2];
>>
>> public:
>>
>> void Add(Derived0* d0) { m_vb[0].push_back(d0); }
>> void Add(Derived1* d1) { m_vb[1].push_back(d1); }
>>
>> const std::vector<Derived0*>* GetD0()
>> { return (const std::vector<Derived0*>*) &m_vb[0]; }
>> const std::vector<Derived1*>* GetD1()
>> { return (const std::vector<Derived1*>*) &m_vb[1]; }
>>
>> const std::vector<Derived0*>* GetDD0()
>> { return static_cast<const std::vector<Derived0*>*>(&m_vb[0]); }
>> const std::vector<Derived1*>* GetDD1()
>> { return static_cast<const std::vector<Derived1*>*>(&m_vb[1]); }
>>
>> };
>
> Both the C-style cast and the static_cast are wrong; you cannot cast a
> std::vector<base*> to a std::vector<derived*>.
>
> Instead have these getter functions return a std::vector<base*> and
> downcast (using static_cast or dynamic_cast) the vector's elements
> instead elsewhere in your code.
>
> /Leigh


I do not see why the C-style cast is wrong.  It works flawlessly across 
a wide range of compilers and systems.  I can understand the problem 
with C++ static_cast, but I was hoping there was a way around it.  If 
there is not a valid C++ way to do this, I'll leave it as is.


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


Thread

static_cast and std::vector john <gh14tq5@yahoo.com> - 2012-01-20 08:56 -0500
  Re: static_cast and std::vector Leigh Johnston <leigh@i42.co.uk> - 2012-01-20 14:00 +0000
    Re: static_cast and std::vector john <gh14tq5@yahoo.com> - 2012-01-20 09:58 -0500
      Re: static_cast and std::vector Leigh Johnston <leigh@i42.co.uk> - 2012-01-20 15:02 +0000
        Re: static_cast and std::vector john <gh14tq5@yahoo.com> - 2012-01-20 10:28 -0500
      Re: static_cast and std::vector "Fred Zwarts \(KVI\)" <F.Zwarts@KVI.nl> - 2012-01-20 16:48 +0100
        Re: static_cast and std::vector Leigh Johnston <leigh@i42.co.uk> - 2012-01-20 16:02 +0000
          Re: static_cast and std::vector Joshua Maurice <joshuamaurice@gmail.com> - 2012-01-20 14:30 -0800
      Re: static_cast and std::vector Juha Nieminen <nospam@thanks.invalid> - 2012-01-20 19:04 +0000
  Re: static_cast and std::vector Tobias Müller <troplin@bluewin.ch> - 2012-01-21 08:56 +0000
    Re: static_cast and std::vector john <gh14tq5@yahoo.com> - 2012-01-23 12:47 -0500
      Re: static_cast and std::vector Ian Collins <ian-news@hotmail.com> - 2012-01-24 09:22 +1300
  Re: static_cast and std::vector Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2012-01-21 23:02 -0500

csiph-web