void指针 [英] void pointers
问题描述
嗨guyz,
i有这样的结构:
struct Column
{
char * fieldName ;
int fieldType;
void * value;
bool nullFlag;
};
列r;
如何初始化字段void *值?
如果我做这样的事情,它打印一些垃圾值!!
char * str =" ben";
r1.value = str;
cout<< r1.value ;
此外,我必须将这个初始化结构传递给某个函数,
可以显示它。那时候我可能会遇到更多的麻烦.Inn''是吗?
再见,
ben
Hi guyz,
i hav a structure like this:
struct Column
{
char *fieldName;
int fieldType;
void *value;
bool nullFlag;
};
Column r;
how do i initialize the field void * value?
if i do something like this,it prints some junk value!!
char *str="ben";
r1.value=str;
cout<<r1.value;
Also i have to pass this initialized structure to some function that
can display it.At that time i might face even more trouble.Isn''t it?
bye,
ben
推荐答案
ben <毕******* @ rediffmail.com>在消息中写道
news:11 ********************** @ f14g2000cwb.googlegr oups.com ...
"ben" <bi*******@rediffmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
嗨guyz,
我有这样的结构:
struct Column
{* char * fieldName;
int fieldType;
void *值;
bool nullFlag;
};
列r;
如何初始化字段void * value?
如果我做这样的事情,它打印一些垃圾值!!
char * str =" ben";
r1.value = str;
cout<< r1.value;
此外,我必须将这个初始化结构传递给某些可以显示它的功能。那时我可能会面临更多麻烦。不是吗?
再见,
ben
Hi guyz,
i hav a structure like this:
struct Column
{
char *fieldName;
int fieldType;
void *value;
bool nullFlag;
};
Column r;
how do i initialize the field void * value?
if i do something like this,it prints some junk value!!
char *str="ben";
r1.value=str;
cout<<r1.value;
Also i have to pass this initialized structure to some function that
can display it.At that time i might face even more trouble.Isn''t it?
bye,
ben
虚拟指针实际上并没用在C ++中。您通常使用继承和
对象来避免使用它们。如果你确实使用它,那么你需要将你的
指针强制转换为(void *),然后在你想要使用它时再回头。
例如
r1.value =(void *)str;
cout<<(char *)r1.value;
Allan
void pointers aren''t used in C++ really. You generally use inheritance and
objects to avoid their use. If you do use it, then you need to cast your
pointer to (void *) and then cast back when you want to use it.
e.g.
r1.value = (void *)str;
cout<<(char *)r1.value;
Allan
ben写道:
ben wrote:
我有这样的结构:
struct Column
{
char * fieldName;
int fieldType;
void * value;
bool nullFlag;
};
列r;
我如何初始化字段void *值?
通过创建枚举,例如
enum types {integer,string,real};
并将正确的一个分配给fieldType。
如果我做这样的事情,它会打印一些垃圾值!!
char * str =" ben" ;;
r1.value = str;
cout<< r1.value;
i hav a structure like this:
struct Column
{
char *fieldName;
int fieldType;
void *value;
bool nullFlag;
};
Column r;
how do i initialize the field void * value?
By creating an enumeration, such as
enum types { integer, string, real };
and assign the correct one to fieldType.
if i do something like this,it prints some junk value!!
char *str="ben";
r1.value=str;
cout<<r1.value;
然后根据类型切换:
开关(r1.fieldType)
{
案例字符串:
cout<< static_cast< char *>(r1.value);
...
-
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Then switch based on the type:
switch(r1.fieldType)
{
case string:
cout << static_cast<char*>(r1.value);
...
--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
" ben" <毕******* @ rediffmail.com>在消息中写道
news:11 ********************** @ f14g2000cwb.googlegr oups.com ...
"ben" <bi*******@rediffmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
嗨guyz,
我有这样的结构:
struct Column
{* char * fieldName;
int fieldType;
void *值;
bool nullFlag;
};
列r;
如何初始化字段void * value?
如果我做这样的事情,它打印一些垃圾值!!
char * str =" ben";
r1.value = str;
cout<< r1.value;
此外,我必须将这个初始化结构传递给某些可以显示它的功能。那时我可能会面临更多麻烦。不是吗?
再见,
本页
Hi guyz,
i hav a structure like this:
struct Column
{
char *fieldName;
int fieldType;
void *value;
bool nullFlag;
};
Column r;
how do i initialize the field void * value?
if i do something like this,it prints some junk value!!
char *str="ben";
r1.value=str;
cout<<r1.value;
Also i have to pass this initialized structure to some function that
can display it.At that time i might face even more trouble.Isn''t it?
bye,
ben
我认为你错误地将void指针变为变种..试试看boost.org
for boost :: any或boost :: variant
I think you mistake void pointers for variants.. Try looking at boost.org
for boost::any or boost::variant
这篇关于void指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!