菜鸟请教几个c++的问题

来源:百度知道 编辑:UC知道 时间:2024/05/18 08:17:13
#include<stdio.h>
class CType
{
private: int m_n;
public: CType& Set(const CType& r,int n);
int* Initial(int n)
{this->m_n=n;
return &m_n;
}
};
CType CType::Set(const CType& r,int n) // 1
{
static CType a;
a.m_n=n;
if(n==1) return a;
m_n=n+r.m_n;
return *this; // 2
}
CType b;
int *p=b.Initial(20);
void main()
{
printf("%d\t",*p);
b=b.Set(b,2);printf("%d\t",*p);
printf("%d\t",*b.Initial(30));
}
请问:
1.为什么CType CType::Set(const CType& r,int n) 这里还要加一个CType::不加会如何?
2.return *this 这里为什么只是返回*this,*this指什么,为什么?
3.在类当中 声明private:int m_n,为什么没加CType
而在set()当中却有static CType a;这样的定义 用static int a 不行吗?

1.2.3.一起给你答了。
看书去!书都没看完就来问!
这么基本的东西……
受不了你了……
看完了再问!ok?

因为类操作的实现代码是在类的外部实现的 所以要加上域作用符:: 如果不加那连编译这关都过不了.
return *this;
建议你看看 char *p = "xiaolin"; cout<<p <<endl <<*p;

这个应该算C吧。。。。

这里不是补课的地方。