帮忙解释一下*&与&*

wudongxu 2009-07-05 07:30:04
#include <iostream>
#include <string>
using namespace std;

void swap(string* &s1,string* &s2)
{
string *temp;
temp=s1;
s1=s2;
s2=temp;
}

int main()
{
string a("hello");
string b("world");
swap(a,b);
cout<<a<<endl<<b<<endl;
return 0;
}

上面程序在vc中编译通过!
如果我没猜错的话这条语句void swap(string* &s1,string* &s2)里s1,s2应该是地址的引用类型;
但是实参传的却是string类型(swap(a,b)).
当我把swap(a,b)改为swap(&a,&b)编译不能通过!
还有c++里引用有没有指针或者说有没有指向引用的指针.
请大虾帮忙解释一下!
...全文
72 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
dqdx_zch 2009-07-05
  • 打赏
  • 举报
回复
9楼的,受教了!
wudongxu 2009-07-05
  • 打赏
  • 举报
回复
不好意思,我知道错在了.那个函数名swap,在string类里也有这个方法,而这个方法接收的参数刚好是string类型,
即string::swap(string,string),所以真正调查用的是这个函数,而不是我自己写的那个.
把调用改为swap(&a,&b)编译不能通过,是因为&a,&b都是常量,而我在函数里修改了它们的值所以出错.
所以把开始的 string a("hello"); string b("world"); 改为 string a=new string("hello");
string b=new string("world"); 传的还是a,b就可以了.因为此时a,b都是变量.也会调用自己写的那个函数了.

谢谢各位了.
guoxianyu521 2009-07-05
  • 打赏
  • 举报
回复

int swap(string* &s1,string* &s2)
{
string *temp;
temp=s1;
s1=s2;
s2=temp;
return 1 ;
}

int main()
{
string a("hello");
string b("world");
swap(a,b);
cout <<a <<endl <<b <<endl;
return 0;
}


你把你的程序改成这样,然后单步调试,你会发现程序跟本没执行你的swap,而是运行了系统中自带的swap函数(他是string的由友元函数)。
这是调试中发现的:
friend void swap(_Myt& _X, _Myt& _Y)
{_X.swap(_Y); }



& 引用:当实参与形参类型相同时,使用引用,相当与把实参换了个名字(有了这个名字就获得的在这个函数里“行走”的权利)

* 指针:当实参是对象,形参是对象的指针时,也是一种址传地(接收了实参对象的地址),通过操作获得指针来改变原有的对象。
guoxianyu521 2009-07-05
  • 打赏
  • 举报
回复
址传递写法一:
void swap(string* s1,string* s2)
{
string *temp;
temp=s1;
s1=s2;
s2=temp;
}

int main()
{
string a("hello");
string b("world");
swap(&a,&b);
cout << a <<endl << b <<endl;
return 0;
}
址传递写法二:

void swap(string &s1,string &s2)
{
string temp;
temp=s1;
s1=s2;
s2=temp;
}

int main()
{
string a("hello");
string b("world");
swap(a,b);
cout << a <<endl << b <<endl;
return 0;
}

你的那种写法本身就有问题,我调试时,发现程序根本就没有进入你写的swap函数!很奇怪。呵呵
arong1234 2009-07-05
  • 打赏
  • 举报
回复
1. swap(a,b)成功不是因为你这个函数,而是系统有个同名函数使得他成功被交换。如果要交换两个对象,必须是void swap(string& a, string&b)类型的函数或者等价的模板函数才能做到,你这个函数根本不可能做到
2. swap(&a, &b)不成功是因为你对&a的理解有问题
string a; &a的类型不是string *,而是string * const ;因此你这样是无法匹配到那个函数的,如果你要交换指针,唯一的办法是:

string a,b;
string *pa = &a;
string *pb = &b;
swap(pa,pb);
但是我不认为你这样的实现能实现a,b值的改变,只能使得pa,pb被交换


[Quote=引用楼主 wudongxu 的帖子:]
#include <iostream>
#include <string>
using namespace std;

void swap(string* &s1,string* &s2)
{
string *temp;
temp=s1;
s1=s2;
s2=temp;
}

int main()
{
string a("hello");
string b("world");
swap(a,b);
cout < <a < <endl < <b < <endl;
return 0;
}

上面程序在vc中编译通过!
如果我没猜错的话这条语句void swap(string* &s1,strin…
[/Quote]
Vegertar 2009-07-05
  • 打赏
  • 举报
回复
嘿,楼主可知你定义的 swap 要本就没有调用,实际调用的是标准库的全局函数。
arong1234 2009-07-05
  • 打赏
  • 举报
回复
引用是没有指针的,*&表示指针的引用,而
&*个人认为是一种不严谨的写法

[Quote=引用 2 楼 Walf_ghoul 的回复:]
按题意*&是表示指向引用的指针。
[/Quote]
yashuwa0622 2009-07-05
  • 打赏
  • 举报
回复
看了下VS下的反汇编,发现传的是 a 和 b的地址过去
lea eax,[a]
push eax
lea eax,[b]
push eax
call swap....
难道是编译器自动优化了
zzcmx2008 2009-07-05
  • 打赏
  • 举报
回复
void swap(string* &s1,string* &s2)
{
string *temp;
temp=s1;
s1=s2;
s2=temp;
}
这里你用的是指针的引用。
int main()
{
string a("hello");
string b("world");
swap(a,b);
cout<<a <<endl <<b <<endl;
return 0;
}
main函数里面的a和b本身就是代表这两个字符串的地址,所以使用swap(a,b)才是对的。
你改为swap(&a,&b),&a,&b是存储a和b的地址,而不是字符串a和b的地址了,所以不对了。
yashuwa0622 2009-07-05
  • 打赏
  • 举报
回复
也不是很明白,静待高手解惑
dinjay 2009-07-05
  • 打赏
  • 举报
回复
指针可以不初始化,引用必须初始化~可以声明指针的引用。不能声明引用的指针~在同一生命周期中,指针可以改变,可以指向不同的变量,但是引用只能引用一个变量~
这就是指针和引用的区别
Walf_ghoul 2009-07-05
  • 打赏
  • 举报
回复
按题意*&是表示指向引用的指针。
lingling1989r 2009-07-05
  • 打赏
  • 举报
回复
当我把swap(a,b)改为swap(&a,&b)编译不能通过!


没错啊,你是不是忘了把你的temp的类型也改一下了啊??
#include <iostream> 
#include <string>
using namespace std;

void swap(string &s1,string &s2)
{
string temp;
temp=s1;
s1=s2;
s2=temp;
}

int main()
{
string a("hello");
string b("world");
swap(a,b);
cout<<a <<endl <<b <<endl;
return 0;
}

64,654

社区成员

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

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