h*****n 发帖数: 209 | 1 比如说void *p, 我们可以暂时把它cast成(struct A*)p,然后就可以假定p指向一个A结
构的地址了。但是每次我们这样做都必须加上(struct A*),
指针p本身还是void *的。有没有可能永久性的把p指针变成struct A*类型的呢?
如果C不能,C++是不是可以呢? |
X****r 发帖数: 3557 | 2 C/C++里变量的类型是声明/定义的时候就确定的,无法改变。
你可以在更内层的scope里定义不同类型的同名的变量来达到类似效果,但不推荐这种
做法。
【在 h*****n 的大作中提到】 : 比如说void *p, 我们可以暂时把它cast成(struct A*)p,然后就可以假定p指向一个A结 : 构的地址了。但是每次我们这样做都必须加上(struct A*), : 指针p本身还是void *的。有没有可能永久性的把p指针变成struct A*类型的呢? : 如果C不能,C++是不是可以呢?
|
l*****v 发帖数: 498 | 3 Declare another typed pointer, and let compiler optimize? |
m*******l 发帖数: 12782 | 4 alias
【在 h*****n 的大作中提到】 : 比如说void *p, 我们可以暂时把它cast成(struct A*)p,然后就可以假定p指向一个A结 : 构的地址了。但是每次我们这样做都必须加上(struct A*), : 指针p本身还是void *的。有没有可能永久性的把p指针变成struct A*类型的呢? : 如果C不能,C++是不是可以呢?
|
N********n 发帖数: 8363 | 5
You cannot change type dynamically in C/C++.
【在 h*****n 的大作中提到】 : 比如说void *p, 我们可以暂时把它cast成(struct A*)p,然后就可以假定p指向一个A结 : 构的地址了。但是每次我们这样做都必须加上(struct A*), : 指针p本身还是void *的。有没有可能永久性的把p指针变成struct A*类型的呢? : 如果C不能,C++是不是可以呢?
|
j******t 发帖数: 788 | 6 void *p_v
struct A* p_s
p_s = (struct A*)p_v;
或者还不甘心,
#define (struct A*)p_v p_s;
void *p_v
然后用p_s不就行了?
【在 h*****n 的大作中提到】 : 比如说void *p, 我们可以暂时把它cast成(struct A*)p,然后就可以假定p指向一个A结 : 构的地址了。但是每次我们这样做都必须加上(struct A*), : 指针p本身还是void *的。有没有可能永久性的把p指针变成struct A*类型的呢? : 如果C不能,C++是不是可以呢?
|
h*****n 发帖数: 209 | 7 #define p_s (struct A*)p_v ?
如果只map成一个类型当然这个方法是可以的。
但是如果一个函数需要根据输入参数来动态决定把p_v映射成相应类型就不行了。
void foo(void *p_v, int type)
{
struct A* p_a;
struct B* p_b;
if (type == 1)
{
p_a = (struct A*)p_v,
}
else
{
p_b = (struct B*)p_v,
}
// 但是后面的代码还是要根据type来决定用p_a还是p_b
}
【在 j******t 的大作中提到】 : void *p_v : struct A* p_s : p_s = (struct A*)p_v; : 或者还不甘心, : #define (struct A*)p_v p_s; : void *p_v : 然后用p_s不就行了?
|
l*********s 发帖数: 5409 | 8 template man
【在 h*****n 的大作中提到】 : #define p_s (struct A*)p_v ? : 如果只map成一个类型当然这个方法是可以的。 : 但是如果一个函数需要根据输入参数来动态决定把p_v映射成相应类型就不行了。 : void foo(void *p_v, int type) : { : struct A* p_a; : struct B* p_b; : if (type == 1) : { : p_a = (struct A*)p_v,
|
h*****n 发帖数: 209 | 9 C, not C++
【在 l*********s 的大作中提到】 : template man
|
l*********s 发帖数: 5409 | 10 macro man
【在 h*****n 的大作中提到】 : C, not C++
|
|
|
j******t 发帖数: 788 | 11 那就再套#define macro
#define P_a (struct A*)p_v
#define P_b (struct B*)p_v
#ifndef CAST_Pv
#define CAST_Pv(type) \
if...
P_a
or P_b
#endif
然后用CAST_Pv(type).
【在 h*****n 的大作中提到】 : #define p_s (struct A*)p_v ? : 如果只map成一个类型当然这个方法是可以的。 : 但是如果一个函数需要根据输入参数来动态决定把p_v映射成相应类型就不行了。 : void foo(void *p_v, int type) : { : struct A* p_a; : struct B* p_b; : if (type == 1) : { : p_a = (struct A*)p_v,
|
h*****n 发帖数: 209 | 12 你想的和我一样,呵呵。
看来只能这样了。
【在 j******t 的大作中提到】 : 那就再套#define macro : #define P_a (struct A*)p_v : #define P_b (struct B*)p_v : #ifndef CAST_Pv : #define CAST_Pv(type) \ : if... : P_a : or P_b : #endif : 然后用CAST_Pv(type).
|
b***i 发帖数: 3043 | 13 你给出一个完整的例子好吗?就是,p_a,p_b到底后面怎么被使用。
【在 h*****n 的大作中提到】 : #define p_s (struct A*)p_v ? : 如果只map成一个类型当然这个方法是可以的。 : 但是如果一个函数需要根据输入参数来动态决定把p_v映射成相应类型就不行了。 : void foo(void *p_v, int type) : { : struct A* p_a; : struct B* p_b; : if (type == 1) : { : p_a = (struct A*)p_v,
|
j******t 发帖数: 788 | 14 别忘了.
#ifndef CAST_Pv哦.
【在 h*****n 的大作中提到】 : 你想的和我一样,呵呵。 : 看来只能这样了。
|
m**s 发帖数: 97 | 15 CAST_Pv(type)在程序里总是看着别扭。要强记它是个pointer.
【在 j******t 的大作中提到】 : 那就再套#define macro : #define P_a (struct A*)p_v : #define P_b (struct B*)p_v : #ifndef CAST_Pv : #define CAST_Pv(type) \ : if... : P_a : or P_b : #endif : 然后用CAST_Pv(type).
|
m**s 发帖数: 97 | 16 void foo(void *p, int type, int v)
{
switch (type)
{
case 1:
(struct A*)p->value = v;
case 2:
(struct B*)p->value = v;
case 3:
(struct C*)p->value = v;
default:
...
}
}
如果case不多就直接cast了;如果case太多,操作又一样,还是macro
【在 b***i 的大作中提到】 : 你给出一个完整的例子好吗?就是,p_a,p_b到底后面怎么被使用。
|
k********a 发帖数: 101 | 17 typecast的结果是个rvalue,不能放在assignment operator左边吧。
【在 m**s 的大作中提到】 : void foo(void *p, int type, int v) : { : switch (type) : { : case 1: : (struct A*)p->value = v; : case 2: : (struct B*)p->value = v; : case 3: : (struct C*)p->value = v;
|
m*******l 发帖数: 12782 | 18 ????
【在 k********a 的大作中提到】 : typecast的结果是个rvalue,不能放在assignment operator左边吧。
|
b***i 发帖数: 3043 | 19 你是楼主马甲?
我以前用过这样的宏,Define(double**, CosM);
不知道是不是楼主想要的。
【在 m**s 的大作中提到】 : void foo(void *p, int type, int v) : { : switch (type) : { : case 1: : (struct A*)p->value = v; : case 2: : (struct B*)p->value = v; : case 3: : (struct C*)p->value = v;
|
h*****n 发帖数: 209 | 20 那个不是我马甲,呵呵。
你那个macro具体怎么写的啊?
【在 b***i 的大作中提到】 : 你是楼主马甲? : 我以前用过这样的宏,Define(double**, CosM); : 不知道是不是楼主想要的。
|
k********a 发帖数: 101 | 21 哦记错了,把pointer conversion和type casting搞混了。。。
【在 m*******l 的大作中提到】 : ????
|
b***i 发帖数: 3043 | 22 #define ASSIGN(nick, var, v)
((nick*) var)->value = v
还是不清楚你的需求到底是什么?你具体写一个完整的函数吧
【在 h*****n 的大作中提到】 : 那个不是我马甲,呵呵。 : 你那个macro具体怎么写的啊?
|