指向指针的指针如何放入该结构中?急

山伟 2010-01-19 11:58:55

std::wstring m_DCMetadataTag;
TCHAR* CharToTChar(char* szInput)
{
_ASSERT(szInput);
#ifdef _UNICODE
return CharToWChar(szInput);
#else
char* szTmp=new char[strlen(szInput)+1];
if(!szTmp)
return 0;

::strcpy(szTmp,szInput);
return szTmp;
#endif
}
void MyParser::startElementHandler(const char *tag, const char **xmlattributes)
{
TCHAR **xmlAtts = NULL;

int i = 0;
while (xmlattributes[i] && *xmlattributes[i] && xmlattributes[i][0] != 0)
{
int iLen = strlen(xmlattributes[i]);
pAtts = new char[iLen+1];

strcpy(pAtts, xmlattributes[i]);
//////////////
//CharToTChar(char* szInput)可成功转换
这里应该对 TCHAR **xmlAtts如何处理?
//////////////

i++;
}

m_mapIdToHref.insert(pair<wstring, wstring>(xmlAtts [1],xmlAtts [3]));
}



这里的xmlattributes是 const char **,而我需要将 xmlattributes先传给 char **pTemp,然后再将pTemp从char转换成TCHAR
...全文
128 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yshuise 2010-01-19
  • 打赏
  • 举报
回复
特化的T应该是char

template<T>
struct remove_const;


template<>
struct remove_const<const char**>
{
typedef char** type;
};
yshuise 2010-01-19
  • 打赏
  • 举报
回复
template<T>
struct remove_const;


template<>
struct remove_const<const T**>
{
typedef T** type;
};

  • 打赏
  • 举报
回复
reinpreter_cast

用法:reinpreter_cast<type-id> (expression)

type-id必须是一个指针、引用、算术类型、函数指针或者成员指针。它可以把一个指针转换成一个整数,也可以把一个整数转换成一个指针(先把一个指针转换成一个整数,在把该整数转换成原类型的指针,还可以得到原先的指针值)。

用reinpreter_cast作转换
山伟 2010-01-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lianshaohua 的回复:]
用const_cast <char**>来转一下不知道行不行
[/Quote]

不行。。。
需要对const char **xmlattributes赋值给TCHAR **xmlAtts
好像只能通过循环来一个一个转换了赋值,但是我对这里的TCHAR **xmlAtts 不知道该如何定义和初始化。
ztenv 版主 2010-01-19
  • 打赏
  • 举报
回复
用const_cast<char**>来转一下不知道行不行
山伟 2010-01-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 cattycat 的回复:]
从你的这个m_mapIdToHref.insert参数看,xmlAtts应该是TCHAR的指针数组。
xmlAtts=new TCHAR*[n];
在你的while循环里,将pAtts转换一下 赋给xmlAtts.
xmlAtts[i]= ChartoTChar(pAtts) ;
[/Quote]

我当时也是这样的思路,但是我始终不知道应该如何对xmlAtts进行初始化。
原来new一个指针型数组。谢谢。
我试了下。可以成功获得!
山伟 2010-01-19
  • 打赏
  • 举报
回复
m_mapIdToHref.insert的问题解决了:

不过我还是对const char **转 TCHAR没搞明白。
本来想定义一个TCHAR的二维数组,可是大小不确定,每次单独初始化一个TCHAR后虽然能够获得const char** 的其中一个字串但是在循环中我不知道该如何避免内存泄露。

hairetz兄,yshuise兄,baihacker兄提供的建议我抽时间试试,先结贴吧。
谢谢各位!
baihacker 2010-01-19
  • 打赏
  • 举报
回复
#include <iostream>
#include <typeinfo>
using namespace std;

int main()
{
const char* * p;
cout << typeid(p[0]).name() << endl;
cout << typeid(const_cast<char*>(p[0])).name() << endl;
return 0;
}
cattycat 2010-01-19
  • 打赏
  • 举报
回复
从你的这个m_mapIdToHref.insert参数看,xmlAtts应该是TCHAR的指针数组。
xmlAtts=new TCHAR*[n];
在你的while循环里,将pAtts转换一下 赋给xmlAtts.
xmlAtts[i]= ChartoTChar(pAtts) ;

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧