将std:string指针作为参数返回 [英] returning an std:string pointer as a parameter

查看:102
本文介绍了将std:string指针作为参数返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我是std库的新手。

我有一个函数可以读取数据并从中创建一个std字符串。

我想通过它回到调用函数作为参数,而不是作为

函数返回。


我尝试了各种间接等但没有任何作用。


我想要这样的psudo类型代码:


typedef basic_string< TCHAR> tstring; // TCHAR的字符串


BOOL readFunction(tstring * pret)

{

tstring * ptstrpage = new tstring;

..

..

ptstrpage =装载数据

pret = ptstrpage;

返回(TRUE)

}


callingfunc

{


tstring * pData1 ,pData2;


if(!readFunction(pData1)

{

错误处理

}

if(!readFunction(pData2)

{

错误处理

}

删除pData1,pData2;

}

Hi I''m new to the std library.
I have a function which reads data and creates a std string from it.
I want to pass that back to the calling function as a parameter, not as the
function return.

I have tried various indirections etc but nothing works.

I want something like this psudo type code:

typedef basic_string<TCHAR> tstring; // string of TCHARs

BOOL readFunction(tstring *pret)
{
tstring * ptstrpage = new tstring;
..
..
ptstrpage = loaded data
pret=ptstrpage;
return(TRUE)
}

callingfunc
{

tstring * pData1,pData2;

if(!readFunction(pData1)
{
error processing
}
if(!readFunction(pData2)
{
error processing
}

delete pData1,pData2;
}


推荐答案

Fred写道:
BOOL readFunction(tstring * pret)
BOOL readFunction(tstring *pret)




这是创建字符串指针的副本而不是允许你

修改pData1和pData2。


尝试使用对指针的引用:


BOOL readFunction(tstring *& pret)


Josh McFarlane



This is creating a copy of the string pointer rather than allowing you
to modify pData1 and pData2.

Try using a reference to the pointer:

BOOL readFunction(tstring*& pret)

Josh McFarlane


Fred写道:
你好我是std图书馆的新手。
我有一个函数可以读取数据并从中创建一个std字符串。
我想将它作为参数传递回调用函数,而不是
函数返回。

我尝试了各种各样的间接等但没有任何作用。

我想要这样的psudo类型代码:

typedef basic_string< TCHAR> tstring; // TCHAR的字符串

BOOL readFunction(tstring * pret)


使用bool而不是BOOL

{
tstring * ptstrpage = new tstring;
为什么在这里使用指针?以下就足够了

tstring ptstrpage;



ptstrpage =加载数据


可疑:谁是加载数据的拥有者?

我猜它的类型是TCHAR * for tstring *

=>最可能是内存泄漏。

pret = ptstrpage;
返回(TRUE)


返回true;


内存泄漏:ptstrpage从未删除。

}

callingfunc

tstring * pData1,pData2;


没有为pData1和pData2分配内存!

再次:为什么正常变量可以做的指针?

if(!readFunction (pData1)
{
错误处理
}

if(!readFunction(pData2)
{
错误处理
}

删除pData1,pData2;
}
Hi I''m new to the std library.
I have a function which reads data and creates a std string from it.
I want to pass that back to the calling function as a parameter, not as the
function return.

I have tried various indirections etc but nothing works.

I want something like this psudo type code:

typedef basic_string<TCHAR> tstring; // string of TCHARs

BOOL readFunction(tstring *pret)
Use bool instead of BOOL
{
tstring * ptstrpage = new tstring; Why use a pointer here? The following is enough
tstring ptstrpage;
.
.
ptstrpage = loaded data
Suspicous: who is owner of `loaded data''?
I guess it''s type is TCHAR* for tstring*
=> most probaly a memory leak.
pret=ptstrpage;
return(TRUE)
return true;

Memory leak: ptstrpage never deleted.
}

callingfunc
{

tstring * pData1,pData2;
No memory allocated for pData1 and pData2!
Again: why pointers where normal variables would do?

if(!readFunction(pData1)
{
error processing
}
if(!readFunction(pData2)
{
error processing
}

delete pData1,pData2;
}




签名

bool readFunction(tsting& dest);

并避免完全使用你发布的代码指针。


问候,斯蒂芬



Make the signature
bool readFunction(tsting& dest);
and avoid the usage of pointers completely for the code you''ve posted.

Regards, Stephan




" Stephan Br?nnimann"< br **** @ hotmail.com>写在留言中

news :11 ********************** @ f14g2000cwb.googlegr oups.com ...

"Stephan Br?nnimann" <br****@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Fred写道:
我是std库的新手。
我有一个函数可以读取数据并从中创建一个std字符串。
我想把它传递给调用函数作为参数,而不是
函数返回。

我尝试了各种间接等但没有任何作用。

我想要这样的psudo类型代码:

typedef basic_string< TCHAR> tstring; // TCHAR的字符串

BOOL readFunction(tstring * pret)
Hi I''m new to the std library.
I have a function which reads data and creates a std string from it.
I want to pass that back to the calling function as a parameter, not as the function return.

I have tried various indirections etc but nothing works.

I want something like this psudo type code:

typedef basic_string<TCHAR> tstring; // string of TCHARs

BOOL readFunction(tstring *pret)



使用bool而不是BOOL



Use bool instead of BOOL

{
tstring * ptstrpage = new tstring;
{
tstring * ptstrpage = new tstring;


为什么在这里使用指针?以下就足够了
tstring ptstrpage;


Why use a pointer here? The following is enough
tstring ptstrpage;



ptstrpage =加载数据
.
.
ptstrpage = loaded data



可疑:谁是加载数据的拥有者?
我猜它的类型是TCHAR * for tstring *
=>最可能是内存泄漏。



Suspicous: who is owner of `loaded data''?
I guess it''s type is TCHAR* for tstring*
=> most probaly a memory leak.

pret = ptstrpage;
return(TRUE)
pret=ptstrpage;
return(TRUE)



返回true;
<内存泄漏:ptstrpage从未删除。



return true;

Memory leak: ptstrpage never deleted.

}

callingfunc

tstring * pData1,pData2 ;
}

callingfunc
{

tstring * pData1,pData2;



没有为pData1和pData2分配内存!
再次:为什么正常变量可以做的指针?



No memory allocated for pData1 and pData2!
Again: why pointers where normal variables would do?

if(!readFunction(pData1)
{
错误处理
}

if(!readFunction(pData2)
{
错误处理
}

删除pData1,pData2;
}

if(!readFunction(pData1)
{
error processing
}
if(!readFunction(pData2)
{
error processing
}

delete pData1,pData2;
}



制作签名
bool readFunction(tsting& dest);
并避免完全使用你发布的代码指针。



Make the signature
bool readFunction(tsting& dest);
and avoid the usage of pointers completely for the code you''ve posted.



抱歉。该函数不会删除数据。它在我的伪

表明我希望它能够持久化t。

它将徘徊很长一段时间,并且可以通过消息循环在不同时间调用几个函数来接收



加载数据是一个缓冲区,他已经读取了外部数据。

TCHAR lpReadBuff [BUFSIZE];


在一个循环中,我做了


* ptstrpage + = lpReadBuff;

每次阅读后



如果我可以通过ptstrpage传回指针参数我不需要

在函数中删除它,如果在全局定义了pret,我可以随时随地删除它




我意识到我的伪代码并没有显示出我想要的细节,并且导致误解 - 我刚刚发布它就像我做的那样展示我想要的东西

通过指针返回的数据。


很抱歉。


Sorry. The data will niot be deleted by that function. It was in my pseudo
to show that I wished it to persist.
It will "hang around" for a long time, and be accessed by several functions
called at various times by the message loop.
"loaded data" is a buffer into which he external data has been read.

TCHAR lpReadBuff[BUFSIZE];

In a loop I do

*ptstrpage+=lpReadBuff;

after each read.

If I can pass ptstrpage back through the pointer parameter I wont need to
delete it in the function, and if pret is defined globally I can delete it
whenever and wherever I like.

I realise my pseudo code didn''t show the detail of what I wanted and has
lead to misunderstanding - I just posted it like I did to show how I wanted
the data returned through a pointer.

Sorry about that.


这篇关于将std:string指针作为参数返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆