C结构体 – 成员函数访问父结构体的变量

huangapple go评论106阅读模式
英文:

C struct - member function accessing variable of parent struct

问题

在C++中,你可以这样做:

  1. struct Person {
  2. int ID;
  3. char* name;
  4. void Display() {
  5. cout << "Person " << name << " ID: " << ID << endl;
  6. }
  7. };

成员函数可以访问结构体中的其他变量吗?

英文:

In C++ you could do:

  1. class Person{
  2. public:
  3. int ID;
  4. char* name;
  5. void Display(){
  6. cout &lt;&lt; &quot;Person &quot; &lt;&lt; name &lt;&lt; &quot; ID: &quot; &lt;&lt; ID &lt;&lt; endl;
  7. }
  8. }

Where the member function can access other variables in a class, is there anyway to do the same with a struct in C?

答案1

得分: 1

以下是您的C++代码的翻译部分:

  1. class Person {
  2. public:
  3. int ID;
  4. char* name;
  5. void Display() {
  6. cout << "Person " << name << " ID: " << ID << endl;
  7. }
  8. }
  9. ...
  10. Person person;
  11. ...
  12. person.Display();
  13. ...

以下是相似的C代码:

  1. struct Person {
  2. int ID;
  3. char* name;
  4. }
  5. void Display(struct Person *this) {
  6. printf("Person %s ID: %d\n", this->name, this->ID);
  7. }
  8. ...
  9. struct Person person;
  10. ...
  11. Display(&person);
  12. ...
英文:

Your C++ code:

  1. class Person {
  2. public:
  3. int ID;
  4. char* name;
  5. void Display() {
  6. cout &lt;&lt; &quot;Person &quot; &lt;&lt; name &lt;&lt; &quot; ID: &quot; &lt;&lt; ID &lt;&lt; endl;
  7. }
  8. }
  9. ...
  10. Person person;
  11. ...
  12. person.Display();
  13. ...

In C there are no member functions, but similar code in C could look like this:

  1. struct Person {
  2. int ID;
  3. char* name;
  4. }
  5. void Display(struct Person *this) {
  6. printf(&quot;Person %s ID: %d\n&quot;, this-&gt;name, this-&gt;ID);
  7. }
  8. ...
  9. struct Person person;
  10. ...
  11. Display(&amp;Person);
  12. ...

答案2

得分: 0

C不是一种面向对象的语言,但你可以做类似于这样的事情。

  1. #include <stdio.h>
  2. #include <string.h>
  3. typedef void (*DoRunTimeChecks)();
  4. struct student
  5. {
  6. char name[20];
  7. DoRunTimeChecks func;
  8. };
  9. void Print(char name[])
  10. {
  11. printf("打印学生信息\n");
  12. printf("姓名:%s", name);
  13. }
  14. void main ()
  15. {
  16. struct student s = {"shriram", Print};
  17. s.func = Print;
  18. s.func(s.name);
  19. }
英文:

c is not a object oriented language, but you can do something like this.

  1. #include&lt;stdio.h&gt;
  2. #include &lt;string.h&gt;
  3. typedef void (*DoRunTimeChecks)();
  4. struct student
  5. {
  6. char name[20];
  7. DoRunTimeChecks func;
  8. };
  9. void Print(char name[])
  10. {
  11. printf(&quot;Printing student information\n&quot;);
  12. printf(&quot;Name: %s&quot;,name);
  13. }
  14. void main ()
  15. {
  16. struct student s = {&quot;shriram&quot;, Print};
  17. s.func = Print;
  18. s.func(s.name);
  19. }

huangapple
  • 本文由 发表于 2020年1月6日 22:39:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613986.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定