关于c++指针的问题
我在学习指针的过程中遇到这个情况:#include<iostream>usingnamespacestd;intmain(){inta=3;int*p=&a;}可以编译通...
我在学习指针的过程中遇到这个情况:
#include <iostream>
using namespace std;
int main()
{
int a=3;
int *p=&a;
}可以编译通过,但是把int *p=&a;换成int *p;*p=&a;就出错了,这是为什么?
另外就int *p=&a而言,&a返回的不是a的地址吗,那*p不就等于a的地址了?指针变量p保存的又是谁的地址呢?经测试,int *p=&a;和int *p;p=&a;是等效的. 展开
#include <iostream>
using namespace std;
int main()
{
int a=3;
int *p=&a;
}可以编译通过,但是把int *p=&a;换成int *p;*p=&a;就出错了,这是为什么?
另外就int *p=&a而言,&a返回的不是a的地址吗,那*p不就等于a的地址了?指针变量p保存的又是谁的地址呢?经测试,int *p=&a;和int *p;p=&a;是等效的. 展开
13个回答
展开全部
#include<iostream>
#include<string>
using namespace std;
class Teacher
{
public:
Teacher(string p,string n,char s,int a,int t,string ad);
void display();
protected:
string title;
string name;
char sex;
int age;
int telephone;
string address;
};
void Teacher::display()
{
cout<<"title:"<<title<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"age:"<<age<<endl;
cout<<"telephone:"<<telephone<<endl;
cout<<"address:"<<endl;
}
Teacher::Teacher(string t,string n,char s,int a,int te,string ad)
{
title=t;
name=n;
sex=s;
age=a;
telephone=te;
address=ad;
}
class Cadre
{
public:
Cadre(string p,string n,char s,int a,int te,string ad);
void display();
protected:
string post;
string name;
char sex;
int age;
int telephone;
string address;
};
Cadre::Cadre(string p,string n,char s,int a,int te,string ad)
{
post=p;
name=n;
sex=s;
age=a;
telephone=te;
address=ad;
}
void Cadre::display()
{
cout<<"post:"<<post<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"age:"<<age<<endl;
cout<<"telephone:"<<telephone<<endl;
cout<<"address:"<<endl;
}
class Teacher_Cadre:public Teacher,public Cadre
{
public:
Teacher_Cadre(string t,string n,char s,int a,int te,string ad,string p,int w)
:Teacher(t, n, s, a, te, ad),
Cadre( p, n, s, a, te, ad)
{
wages=w;
}
void show()
{
Teacher::display();
cout<<"post:"<<endl;
cout<<"wages:"<<endl;
}
private:
int wages;
};
int main()
{
Teacher_Cadre tc("处长","余建江",'g', 20,123," 江西","书记",500);
tc.show();
return 0;
}
#include<string>
using namespace std;
class Teacher
{
public:
Teacher(string p,string n,char s,int a,int t,string ad);
void display();
protected:
string title;
string name;
char sex;
int age;
int telephone;
string address;
};
void Teacher::display()
{
cout<<"title:"<<title<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"age:"<<age<<endl;
cout<<"telephone:"<<telephone<<endl;
cout<<"address:"<<endl;
}
Teacher::Teacher(string t,string n,char s,int a,int te,string ad)
{
title=t;
name=n;
sex=s;
age=a;
telephone=te;
address=ad;
}
class Cadre
{
public:
Cadre(string p,string n,char s,int a,int te,string ad);
void display();
protected:
string post;
string name;
char sex;
int age;
int telephone;
string address;
};
Cadre::Cadre(string p,string n,char s,int a,int te,string ad)
{
post=p;
name=n;
sex=s;
age=a;
telephone=te;
address=ad;
}
void Cadre::display()
{
cout<<"post:"<<post<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"age:"<<age<<endl;
cout<<"telephone:"<<telephone<<endl;
cout<<"address:"<<endl;
}
class Teacher_Cadre:public Teacher,public Cadre
{
public:
Teacher_Cadre(string t,string n,char s,int a,int te,string ad,string p,int w)
:Teacher(t, n, s, a, te, ad),
Cadre( p, n, s, a, te, ad)
{
wages=w;
}
void show()
{
Teacher::display();
cout<<"post:"<<endl;
cout<<"wages:"<<endl;
}
private:
int wages;
};
int main()
{
Teacher_Cadre tc("处长","余建江",'g', 20,123," 江西","书记",500);
tc.show();
return 0;
}
追问
这是怎么个意思?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-07-22
展开全部
int *p;
p=&a;
p=&a;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询