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