两个问题~

wh62592855 2008-11-05 09:14:23
1,
假设在链表中,节点类型为node,有p,q,r三个节点,head是指向node类型的指针.
head指向p,
p->next=q,
q->next=r,
r->next=NULL,
那么在此链表中,头结点是head还是p呢? 我觉得是head,可书上的定义为:链表中的第一个节点(head是个指针而不是节点呀),请指教.

2,
友元为什么不能是const函数或const运算符?
...全文
137 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
jia_xiaoxin 2008-11-06
  • 打赏
  • 举报
回复
1.那么在此链表中,头结点是p,因为head只是一个指针而不是一个结点
如果把原题定义成这样的
假设在链表中,节点类型为node,有head,p,q,r三个节点.
head->next=p,
p->next=q,
q->next=r,
r->next=NULL,
那么head就是头结点


2.因为友元函数是非成员函数,所以不存在隐含的this指针,所以也就没有必要加const,因为在函数尾的const其实就是限定这个this指针的。
太乙 2008-11-06
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 hqin6 的回复:]
C/C++ code

#include <iostream>
using namespace std;
void fun() const
{
cout<<"hello"<<endl;
}
int main(void)
{
fun();
return 0;
}

别说友元了,就上面这代码,都编译不过去!
[/Quote]

糗大了~~~~~~~~~~

当我没说~!
太乙 2008-11-06
  • 打赏
  • 举报
回复


#include <iostream>
using namespace std;
void fun() const
{
cout<<"hello"<<endl;
}
int main(void)
{
fun();
return 0;
}

别说友元了,就上面这代码,都编译不过去!

太乙 2008-11-06
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 jia_xiaoxin 的回复:]
2.因为友元函数是非成员函数,所以不存在隐含的this指针,所以也就没有必要加const,因为在函数尾的const其实就是限定这个this指针的。
[/Quote]

non-member function  cannot have `const' method qualifier



jia_xiaoxin 2008-11-06
  • 打赏
  • 举报
回复
2,
友元为什么不能是const函数或const运算符?

这个问题问的不全面,如果是指友元函数,那么它不能是const函数或const运算符
但是如果是友元成员函数,却是可以的。
下面是一个例子

#include <iostream> 
#include <string>
using namespace std;

class Teacher;
class Student
{
int num;
string name;
string sex;
public:
void input();
void display();
Student operator=(Teacher &T) const ; //这里用到了const
};
class Teacher
{
int num;
string name;
string sex;
public:
void input();
void display();
friend Student Student::operator=(Teacher &T) const; //这里用到了const
};

void Teacher::input()
{
cin>>num>>name>>sex;
}

void Student::input()
{
cin>>num>>name>>sex;
}

void Teacher::display()
{
cout <<num <<endl;
cout <<name <<endl;
cout <<sex <<endl;
}

void Student::display()
{
cout <<num <<endl;
cout <<name <<endl;
cout <<sex <<endl;
}
Student Student::operator=(Teacher &T) const //这里用到了const
{
Student S;
S.name=T.name;
S.num=T.num;
S.sex=T.sex;
return S;
}
int main()
{
Teacher t;
t.input();
t.display();
Student s;
s.input();
s.display();
s=t;
s.display();
return 0;

}
R9R9R9 2008-11-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wh62592855 的回复:]
引用 4 楼 R9R9R9 的回复:
1: 别拘泥于书,看懂意思就好了。

2:
class A
{
public:
void foo() const
{
}
};

class T
{
public:
friend void A::foo() const;
friend void foobar() const; //这样当然不行,因为一个非成员函数是不可以用const来修辞的,而不是friend引起的。
};
你意思是说只要是非成员函数,都不能用const来修饰?

换句话说也就是,不能用const修饰并不是因为它是友元函数,而是因为它是非…
[/Quote]

对啊。因为那个const是用来修辞什么的呢?是用来修辞this指针的,而只有成员函数才有那个this指针啊
wh62592855 2008-11-05
  • 打赏
  • 举报
回复
请高手解答!
yuhudie203 2008-11-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xhs_lh04 的回复:]
JF
head,是指针,指向头节点的指针,p是头节点
第二个不知道,呵呵
[/Quote]
顶~
wh62592855 2008-11-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 R9R9R9 的回复:]
1: 别拘泥于书,看懂意思就好了。

2:
class A
{
public:
void foo() const
{
}
};

class T
{
public:
friend void A::foo() const;
friend void foobar() const; //这样当然不行,因为一个非成员函数是不可以用const来修辞的,而不是friend引起的。
};
[/Quote]你意思是说只要是非成员函数,都不能用const来修饰?

换句话说也就是,不能用const修饰并不是因为它是友元函数,而是因为它是非成员函数?
firstdad 2008-11-05
  • 打赏
  • 举报
回复
类中的友员是个声明, 而不是定义啊
R9R9R9 2008-11-05
  • 打赏
  • 举报
回复
1: 别拘泥于书,看懂意思就好了。

2:
class A
{
public:
void foo() const
{
}
};

class T
{
public:
friend void A::foo() const;
friend void foobar() const; //这样当然不行,因为一个非成员函数是不可以用const来修辞的,而不是friend引起的。
};
xhs_lh04 2008-11-05
  • 打赏
  • 举报
回复
JF
head,是指针,指向头节点的指针,p是头节点
第二个不知道,呵呵
zgy1353246 2008-11-05
  • 打赏
  • 举报
回复
1,head为头指针,指向头节点P,head本身不是节点,只是个指针!!!
2.。。。貌似我也不太清楚,等待高手!
qqwx_1986 2008-11-05
  • 打赏
  • 举报
回复
1 head和p指向同一个地址都是头节点

65,211

社区成员

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

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