Skip to content

Commit e03bac8

Browse files
author
wintel2014
committed
The index is not right when BaseTypePtr vs DerviceTypePtr
1 parent 4629939 commit e03bac8

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

C++/STL/vector_index/index.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <vector>
2+
#include <iostream>
3+
4+
struct ITask {
5+
long mI;
6+
};
7+
8+
struct Task:public ITask
9+
{
10+
long mT;
11+
long mN;
12+
};
13+
14+
int main()
15+
{
16+
std::vector<long> letters {0,0,1,1, 3,2,3,4,5,6,7,8};
17+
18+
long* pLong = &letters[4];
19+
auto index = pLong-letters.data();
20+
std::cout<<index<<std::endl;
21+
22+
23+
Task t1, t2, t3, t4;
24+
std::vector<Task> T;
25+
T.push_back(t1);
26+
T.push_back(t2);
27+
T.push_back(t3);
28+
T.push_back(t4);
29+
30+
ITask* pTask = &T[2];
31+
std::cout<<pTask-static_cast<ITask*>(T.data())<<std::endl;
32+
std::cout<<static_cast<Task*>(pTask)-(T.data())<<std::endl;
33+
34+
}

0 commit comments

Comments
 (0)