pointer!有点弱弱的问题!!:

type="text"

新手上路
VIP
注册
2008-03-05
消息
102
荣誉分数
31
声望点数
0
请问下!

part of code:
class Buddy {
public:
char first_ca[8];
char last_ca[12];
int age_n;
public:
Buddy() { cout << "Buddy object being created.\n"; }
~Buddy() { cout << "Buddy object being destroyed.\n"; }
};
int main()
const int MAX = 14;
Buddy MyFriends_aC[MAX];
...
int numFriends_n = 0;
...
for (int i=0; i<numFriends_n; i++) {
cout << "Friend " << (i+1) << ": ";
cout << "Name:" << (*(MyFriends_aC+i)).last_ca <<
", " << (*(MyFriends_aC+i)).first_ca
<< " Age:" << (*(MyFriends_aC+i)).age_n << endl;
}
...

part of code2:

class Buddy {

public:
char first_ca[8];
char last_ca[12];
int age_n;
public:
Buddy() { cout << "Buddy object being created.\n"; }
~Buddy() { cout << "Buddy object being destroyed.\n"; }
};
int main()
{
const int MAX = 14;
Buddy* MyFriends_aPtr[MAX];
...
int numFriends_n = 0;
...
for (int i=0; i<numFriends_n; i++) {
cout << "Buddy " << (i+1) << ": ";
cout << "Name:" << MyFriends_aPtr->last_ca << ", "
<< MyFriends_aPtr->first_ca
<< " Age:" << MyFriends_aPtr->age_n << endl;
}
...

part of code 3:
class Buddy {
public:
char first_ca[8];
char last_ca[12];
int age_n;
public:
Buddy() { cout << "Buddy object being created.\n"; }
~Buddy() { cout << "Buddy object being destroyed.\n"; }
};
int main()
{
int numFriends_n=0;
cout << "Notice: No Buddy objects have yet been created.\n";
cout << "Number of buddies: "; cin >> numFriends_n;
Buddy* MyFriends_aPtr;
MyFriends_aPtr = new Buddy[numFriends_n];
...
for (int i=0; i<numFriends_n; i++) {
cout << "Friend " << (i+1) << ": ";
cout << "Name:" << (*(MyFriends_aPtr+i)).last_ca <<
", " << (*(MyFriends_aPtr+i)).first_ca
<< " Age:" << (*(MyFriends_aPtr+i)).age_n << endl;
}
。。。


这是部分code!

完整的话有点多!!
所以。。
我的问题是!我搞不清红色字体的部分是什么data type》
还有就是pointer算不算一个data type啊???像int, char。。 一样????


thanks。。。thanks。。。thanks。。。thanks。。。thanks。。。

:cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool::cool:
 
Buddy MyFriends_aC[MAX];
...
int numFriends_n = 0;
...
for (int i=0; i<numFriends_n; i++) {
cout << "Friend " << (i+1) << ": ";
cout << "Name:" << (*(MyFriends_aC+i)).last_ca <<
", " << (*(MyFriends_aC+i)).first_ca
<< " Age:" << (*(MyFriends_aC+i)).age_n << endl;
}

struct Buddy{
char *last_ca;
char *first_ca;
int age_n;
};

MyFriends_aC 是个 array
Buddy *sample = MyFriends_aC + i;
 
part of code:
class Buddy {
public:
char first_ca[8];
char last_ca[12];
int age_n;
public:
Buddy() { cout << "Buddy object being created.\n"; }
~Buddy() { cout << "Buddy object being destroyed.\n"; }
};
int main()
const int MAX = 14;
Buddy MyFriends_aC[MAX];
...
int numFriends_n = 0;
...
for (int i=0; i<numFriends_n; i++) {
cout << "Friend " << (i+1) << ": ";
cout << "Name:" << (*(MyFriends_aC+i)).last_ca <<
", " << (*(MyFriends_aC+i)).first_ca
<< " Age:" << (*(MyFriends_aC+i)).age_n << endl;
}
...

-----------------------------------------------------------------
In code example 1,

You have an array of "Buddy" objects - MyFriends_aC[MAX], which has "MAX" elements.
MyFriends_aC is the address of the first object in this array. It is kind of equivalent to a pointer to the object.

Your C++ complier appears to interprete MyFriends_aC+i as the address/pointer of the ith element/object in this array. Therefore, (*(MyFriends_aC+i)) is the ith object.

As a veteran in C, I don't really likt the expression/interpetation of MyFriends_aC+i by the complier since it gives the machine too much intelligene/power. A more primitive C-style code may look like:

((*Buddy)(((*char)MyFriends_aC)+i*sizeof(Buddy)))

If you can understand this expression then congratulations!
----------------------------------------------------------------
part of code2:

class Buddy {

public:
char first_ca[8];
char last_ca[12];
int age_n;
public:
Buddy() { cout << "Buddy object being created.\n"; }
~Buddy() { cout << "Buddy object being destroyed.\n"; }
};
int main()
{
const int MAX = 14;
Buddy* MyFriends_aPtr[MAX];
...
int numFriends_n = 0;
...
for (int i=0; i<numFriends_n; i++) {
cout << "Buddy " << (i+1) << ": ";
cout << "Name:" << MyFriends_aPtr->last_ca << ", "
<< MyFriends_aPtr->first_ca
<< " Age:" << MyFriends_aPtr->age_n << endl;
}
...

-----------------------
In code exampe 2,

you have an array of pointers - MyFriends_aPtr[MAX], which has "MAX" elements, each points to a "Buddy" object.

MyFriends_aPtr is a pointer to a "Buddy" object.

-----------------------

part of code 3:
class Buddy {
public:
char first_ca[8];
char last_ca[12];
int age_n;
public:
Buddy() { cout << "Buddy object being created.\n"; }
~Buddy() { cout << "Buddy object being destroyed.\n"; }
};
int main()
{
int numFriends_n=0;
cout << "Notice: No Buddy objects have yet been created.\n";
cout << "Number of buddies: "; cin >> numFriends_n;
Buddy* MyFriends_aPtr;
MyFriends_aPtr = new Buddy[numFriends_n];
...
for (int i=0; i<numFriends_n; i++) {
cout << "Friend " << (i+1) << ": ";
cout << "Name:" << (*(MyFriends_aPtr+i)).last_ca <<
", " << (*(MyFriends_aPtr+i)).first_ca
<< " Age:" << (*(MyFriends_aPtr+i)).age_n << endl;
}
。。。


------------------
In code example 3,

you have a pointer MyFriends_aPtr that points to a "Buddy" object.

The assignment expression:

MyFriends_aPtr = new Buddy[numFriends_n];

actually allocates a chunk of memory that can hold "numFriends_n" "Buddy" objects and set "MyFriends_aPtr " to the address/pointer of the first object in this chunk (you can indeed view this chunk as an array of objects).

Now, everything becomes the same as in code sample 1. Just replace MyFriends_aC with MyFriends_aPtr.
 
脑袋有点晕!!

谢谢啦!!慢慢研究了!
 
后退
顶部