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


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

static_cast and std::vector

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

Show all headers | View raw


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]); }

};

Back to comp.lang.c++ | Previous | Next — 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