AnsiString类的赋值问题?string1=string2 是传值还是传址???

dreamnyj 2002-05-07 06:28:35
两个AnsiString类的对象

使用下面语句。

AnsiString string1,string2;

string1="dkjfkdjflksjdf";
string2=string1;

这时是传值调用,还是传址调用呢?

我的string2 修改后,string1也同时被修改????
...全文
47 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
niyashi 2002-05-21
  • 打赏
  • 举报
回复
传值,就是传值,

不过使用AnsiString类的时候还是要小心的,因为这些类在创建和释放的时候都可能会有一些限制,如果不幸在那里的话,那就会出错啦。
不过建议你使用的时候要使用异常处理。
dreamnyj 2002-05-08
  • 打赏
  • 举报
回复
我用了上面兄弟的AnsiString teststring[1]='3';的方法以后,程序异常处理,幸好我有备份,所以就又恢复了回来。
dreamnyj 2002-05-08
  • 打赏
  • 举报
回复
这是打开优化的结果!

谢谢各位……,我想知道打开优化的结果,还会有什么副作用呢?
xdspower 2002-05-08
  • 打赏
  • 举报
回复
这是打开优化的结果!
thunder7971 2002-05-08
  • 打赏
  • 举报
回复
nRoundLittle[0]= 'd',nRoundLittle[1]='f'
知道了吧!
thunder7971 2002-05-08
  • 打赏
  • 举报
回复
AnsiString nRoundingNumber = "dfdsfd";
char *nRoundLittle = new char[20];
StrCopy(nRoundLittle,nRoundingNumber.c_str());

你的方法是传地址!
silver_sky 2002-05-07
  • 打赏
  • 举报
回复
上面那个函数说得还不够清楚吗?
每个String的实例都有自己的Data, = 操作就是把str1的Data挎到str2的Data里
AnsiString 的定义在dstrings里,你自己可以看一下
toby 2002-05-07
  • 打赏
  • 举报
回复
大家都说了,的确是传值。
你可以看dstring.h重第318行的定义:
template<unsigned char sz> SmallString<sz>& __fastcall
SmallString<sz>::operator =(const SmallString& rhs)
{
if (this != &rhs)
{
Len = rhs.Len;
for (int i = 0; i < Len; i++)
Data[i] = rhs.Data[i];
}
return *this;
}
Maxwell 2002-05-07
  • 打赏
  • 举报
回复
应该是传值的,我以前用过,确信。
AnsiString会自己调用析构函数的。
我来看看CB 2002-05-07
  • 打赏
  • 举报
回复
传值拷贝,不是传递地址
ken_henderson 2002-05-07
  • 打赏
  • 举报
回复
看一下AnsiString的operator=是怎样定义的,应该是传值的
kimryo 2002-05-07
  • 打赏
  • 举报
回复
memberwise copy constructor
kimryo 2002-05-07
  • 打赏
  • 举报
回复
memberwise copy constructor
dreamnyj 2002-05-07
  • 打赏
  • 举报
回复
并且,如何把一个AnsiString对象赋值给一个字符串数组呢?

使用char *otherstring=ansistring.c_str();
的方法是传值还是传址呢?
我不懂电脑 2002-05-07
  • 打赏
  • 举报
回复
你用这段检查一下,我的str1没变呀。AnsiString是一个类=实际上是进行了算符重载,估计相当于一个拷贝构造函数,是不会改变传过来的对象的。
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString str1 = "abcd", str2;

str2 = str1;
str2 = "aaa";
}
关于AnsiString的bug主要是在建立DLL的时候存在,在BCB的DLL向导里说得很明白了,拷给你看。
//---------------------------------------------------------------------------
// Important note about DLL memory management when your DLL uses the
// static version of the RunTime Library:
//
// If your DLL exports any functions that pass String objects (or structs/
// classes containing nested Strings) as parameter or function results,
// you will need to add the library MEMMGR.LIB to both the DLL project and
// any other projects that use the DLL. You will also need to use MEMMGR.LIB
// if any other projects which use the DLL will be performing new or delete
// operations on any non-TObject-derived classes which are exported from the
// DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
// EXE's to use the BORLNDMM.DLL as their memory manager. In these cases,
// the file BORLNDMM.DLL should be deployed along with your DLL.
//
// To avoid using BORLNDMM.DLL, pass string information using "char *" or
// ShortString parameters.
//
// If your DLL uses the dynamic version of the RTL, you do not need to
// explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------
dreamnyj 2002-05-07
  • 打赏
  • 举报
回复
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString str1 = "abcd", str2;

str2 = str1;
str2 = "aaa";
}

这段代码是一定不会出错的。


我想问的一个简单的问题,就是如何象使用字符数组一样,使用下标的方法修改一个AnsiString中的任意一个字符的值呢?
dreamnyj 2002-05-07
  • 打赏
  • 举报
回复

我想这可能是BCB的一个关于Ansistring的问题吧。我的代码中决对没有修改string1的指令,但是那两个串,我在程序中调试的时候,就是一个改就会都改,如果是这样的话,我想一定是他们指向的地址是相同,否则的话怎可能会这样呢?





/*==============================================

天若有缘百年孤独又何妨 尘浪无边一夜春风也潇洒

QQ:30002580 EMail:dreamnyj@yeah.net
==============================================*/

dreamnyj 2002-05-07
  • 打赏
  • 举报
回复
如果那位知道的话,万望回复。





/*==============================================

天若有缘百年孤独又何妨 尘浪无边一夜春风也潇洒

==============================================*/

dreamnyj 2002-05-07
  • 打赏
  • 举报
回复
我想也可能是传值,不过记得以前不知道在那里下载一个关于BCB的一些BUG中,有关于ansistring的用法,当时没有注意,那位知道在那里下载吗?
/*==============================================


天若有缘百年孤独又何妨 尘浪无边一夜春风也潇洒


==============================================*/

tokens 2002-05-07
  • 打赏
  • 举报
回复
应该是传值了,不可能传址,
你看看别的地方有没有错。
加载更多回复(4)

13,826

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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