Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #13555
| From | john <gh14tq5@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: static_cast and std::vector |
| Date | 2012-01-23 12:47 -0500 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <jfk6fm$du4$1@speranza.aioe.org> (permalink) |
| References | <jfbrpt$vd$1@speranza.aioe.org> <953898973348828453.949041troplin-bluewin.ch@news.aioe.org> |
>> #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]); }
>>
>> };
>
> Why don't you just write:
> std::vector<Derived0*> m_vd0;
> std::vector<Derived1*> m_vd1;
> and use those without any casting?
>
> This would be the most obvious solution and you are losing nothing.
We have looked into this, but we also really need functionality like
const std::vector<Base*>* GetBase(int which)
{ return &m_vb[which]; }
So, changing as you suggested would preclude this. Would it be possible
to replace the UB casts
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]; }
with some type of specialized iterator?
Thanks.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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