473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Cast vector<foo*> to vector<const foo*>?

Some function requires a vector<const foo*> argument.
How can I cast a vector<foo*> to vector<const foo*>?

Thanks!

Joseph

Jan 26 '06 #1
8 4802
Joseph Turian wrote:
Some function requires a vector<const foo*> argument.
How can I cast a vector<foo*> to vector<const foo*>?

You can't; they are completely different types.

--
Ian Collins.
Jan 26 '06 #2

Ian Collins wrote:
How can I cast a vector<foo*> to vector<const foo*>?

You can't; they are completely different types.


Care to edify me why?
At first blush, they seem quite similar.

Joseph

Jan 26 '06 #3
Joseph Turian wrote:
Ian Collins wrote:
How can I cast a vector<foo*> to vector<const foo*>?


You can't; they are completely different types.

Care to edify me why?
At first blush, they seem quite similar.

An instantiation of a class template is a unique class. Don't be fooled
by the template type, it my look similar to you, but it's apples and
oranges to the compiler. foo* and const foo* are different types.

--
Ian Collins.
Jan 26 '06 #4
"Joseph Turian" <tu****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com
Ian Collins wrote:
How can I cast a vector<foo*> to vector<const foo*>?

You can't; they are completely different types.


Care to edify me why?
At first blush, they seem quite similar.

Joseph


To add to what Ian has said,

foo * and const foo * are different types but are nevertheless similar
enough that you can use const_cast to cast between them. Where two types are
used as template arguments, however, the similarity of the types is
irrevant. Consider:

template<class T>
class Test
{
T t;
};

template<>
class Test<const int>
{
char array[1000];
};

Thus Test<const int> contains an array of chars, whereas Test<int> contains
a single int.

#include <iostream>
using namespace std;

int main()
{
Test<int> t1;
Test<const int> t2;
cout << sizeof(t1) << endl; // gives 4
cout << sizeof(t2) << endl; // gives 1000
return 0;
}

In reality vector<foo *> and vector<const foo *> may hardly differ at
all --- in particular, they may have the same size. However, the possibility
of explicit template specialization means that they could differ
spectacularly, hence the reluctance of the compiler to allow conversion.
--
John Carson
Jan 26 '06 #5
Joseph Turian wrote:
Some function requires a vector<const foo*> argument.
How can I cast a vector<foo*> to vector<const foo*>?


As said, you can't cast.

You'll have to copy each element.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Jan 26 '06 #6

Ben Pope wrote:
Joseph Turian wrote:
Some function requires a vector<const foo*> argument.
How can I cast a vector<foo*> to vector<const foo*>?


As said, you can't cast.

You'll have to copy each element.


You have to copy each element if you want a vector< const Foo * >

However if all you want to do is pass a reference to your vector and
ensure that none of the Foo objects are modified, you can use an
adapter,

template < typename T >
vector_const_adapter
{
typedef std::vector< T * > vec_type;

const vec_type * itsVecRef;

public:

typedef typename vec_type::size_type size_type;

vector_const_adapter() : itsVecRef( 0 ) {}

vector_const_adapter( const vec_type & vecRef )
: itsVecRef( &vecRef )
{
}
const T* operator[] ( size_type idx ) const
{
return (*itsVecRef)[ idx ];
}

bool empty() const;
size_type size() const;
const T* const * begin() const;
const T* const * end() const;
};

and I'll leave it up to you to implement the functions empty() size(),
begin() and end()

Now all you have to do is pass around vector_const_adapter<Foo>. Note I
used a member pointer so that vector_const_adapter can have a default
constructor and be assignable. If you don't want these features use a
member reference.

Jan 26 '06 #7
"Joseph Turian" <tu****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Ian Collins wrote:
> How can I cast a vector<foo*> to vector<const foo*>? You can't; they are completely different types.

Care to edify me why?
At first blush, they seem quite similar.


Here is one reason:

void append(vector<const foo*>& v, const foo* p)
{
v.push_back(p);
}

const foo f;
const foo* p = &f;
vector<foo*> v;
append(v, p);

By casting v to vector<const foo*>&, you have managed to put a const foo*
value into a vector<foo*>. Now, you can execute

v.last()->munge();

where munge is a member of class foo that modifies its object. By doing so,
you have modified a const foo object.

Jan 26 '06 #8

Andrew Koenig wrote:
Care to edify me why?
At first blush, they seem quite similar.


Here is one reason:

void append(vector<const foo*>& v, const foo* p)
{
v.push_back(p);
}

const foo f;
const foo* p = &f;
vector<foo*> v;
append(v, p);

By casting v to vector<const foo*>&, you have managed to put a const foo*
value into a vector<foo*>. Now, you can execute

v.last()->munge();

where munge is a member of class foo that modifies its object. By doing so,
you have modified a const foo object.


Something you couldn't do with my adapter as you cannot modify the
vector either thus you can't push anything onto it.

Jan 27 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Newsgroup - Ann | last post by:
Hi: I saw the following codes from the FAQ about the contiguous storage of vector. I am just wondering how does the implementation of the <vector> guarantee the contiguous? What if a vector v...
3
by: klaas | last post by:
the following code gives rise to the beneath error message, only when a matrix object is instantiated as matrix<bool>, not with matrix<float>: /*returns a reference to the object at position...
8
by: Christian Stigen Larsen | last post by:
Consider the following: class parent { public: virtual void print() { printf("Parent\n"); } }; class child : public parent {
1
by: Dennis | last post by:
Hi I'm trying to implement a vector of vectors where find can be used to find a vector<double> in the vectors of vectors, that is hard to understand i guess. What I mean is that I got a vector...
1
by: Alex Vinokur | last post by:
------ foo.cpp ------ #include <vector> using namespace std; int main() { const vector<int> v1 (10); const vector<bool> v2 (10); &v1;
16
by: Vince | last post by:
Hi, I have replaced my BYTE* by a vector<BYTE> and before I used to do : void CCardRecord::GetRecData(int nOffset, int nDataSize, CString& csValue) { BYTE *pTmp = NULL; pTmp = new BYTE;...
18
by: lchian | last post by:
Hi, I have a vector of class Foo. When I do a push_back(), I expect stl to call the default constructor I wrote for Foo. But instead, stl makes up its own default that is initialized with...
2
by: eb | last post by:
I have this working code : foo.h /* nothing */ foo.cpp ... std::vector<std::vector<T * my_T(board_size,board_size) ; for (i=0; i<board_size; i++)
12
by: eiji.anonremail | last post by:
Hi all, I'm facing some uncertainty with const template arguments. Maybe someone could explain the general strategy. #include <vector> int main(int arc, char** argv) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.