I am able to typecast reference of vector<void*>
to reference of vector<Foo*>
whereas I am unable to typecast vector<void*> to vector<Foo*>
. I am getting the error C2440: 'reinterpret_cast' : cannot convert from 'std::vector<_Ty>' to 'std::vector<_Ty>' why?
And I am able to typecast void* to Foo* without getting compiler error.
void* g =&foo;
reportFooVector2(reinterpret_cast<Foo*>(g));
Below is my entire code.
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
struct Foo
{
string s;
int i;
};
void reportFooVector( vector <Foo*> * pvf )
{
}
void reportFooVector1( vector <Foo*> pvf )
{
}
void reportFooVector2( Foo *pvf )
{
}
int main()
{
struct Foo foo = {"foo", 5};
struct Foo goo = {"goo", 10};
void* g =&foo;
reportFooVector2(reinterpret_cast<Foo*>(g));
vector <void *> vf;
vf.push_back(&foo);
vf.push_back(&goo);
reportFooVector1( reinterpret_cast< vector < Foo * > >(vf));
reportFooVector( reinterpret_cast< vector < Foo * > * >(&vf));
}
In the above program I am getting the compiler error C2440: 'reinterpret_cast' : cannot convert from 'std::vector<_Ty>' to 'std::vector<_Ty>' when calling the line reportFooVector1( reinterpret_cast< vector < Foo * > >(vf));
Could you please anyone tell the reason for it?