结构体的操作符重载和内部的函数定义及使用(结构体的进一步使用)

来源:互联网 发布:php 工作环境 编辑:程序博客网 时间:2024/05/22 02:08

首先结构体和类是有相似的地方的,可以进行运算符重载。

下面以=为例,

#include<iostream>  using namespace std;  struct P  {      int a;      int b;      //虽然已经对结构体进行=重载,但是这里只是举一个例子而已      P& operator=(P& temp)      {          a=temp.a;          b=temp.b;                  return *this;          }  };  int main()  {      P p={1,2};          P x;          x=p;          cout<<x.a;//输出:1      return 0;  }  


还有结构体运算符的重载是不能用友元函数来重载的,因为友元是针对类,结构体虽和类相似,但是毕竟不是类,也无法和类“相等”。

不过我也发现可以在结构体内写个构造函数等。。


#include<iostream>  using namespace std;  struct P  {      int a;      int b;        //P(){}      //P(int x,int y):a(x),b(y){}      P(int x=0,int y=0):a(x),b(y){}          void ShowAdd(){cout<<a+b<<endl;}   };  int main()  {      P temp;//right          //P p1={1,2};//error          P p2(1,2);//right          p.ShowAdd();//输出:3          cout<<p.a<<endl;//输出:1          return 0;  }  
这样的话,结构体几乎就和类一样了。注意结构体中定义的变量和方法是public的。

这里要解释下下面这几句代码

//P(){}  //P(int x,int y):a(x),b(y){}  P(int x=0,int y=0):a(x),b(y){}  

如果你写了第三行的代码,那就千万不要写第一第二行,为什么?放你编译器运行看看吧,这里只是提个醒,不作解释,很简单。

结构体运算符重载

1、定义结构体

struct Currency
{
    int Dollar;
    int Cents;
}

2、重载IO输出操作,在结构体内部将输入操作的重载定义为友元函数重载

friend ostream &operator<<(ostream &out,Currency value);

在结构体外部进行具体定义

ostream& operator<<(ostream &out,Currency value)
{
    out<<"The dollar = "<<value.Dollar<<" and The Cents = "<<value.Cents<<endl;
    return out;
}

3、重载结构体的“=”操作符(在结构体内部)

Currency& operator=(Currency& value)
    {
        Dollar = value.Dollar;
        Cents = value.Cents;
        return *this;
    }

4、重载结构体的“+”操作符(在结构体内部)

Currency& operator+(Currency& value)
    {
        Dollar += value.Dollar;
        Cents += value.Cents;
        return *this;
    }

5、重载结构体的"-"操作符(在结构体内部)

Currency &operator-(Currency& value)
    {
        Dollar = Dollar - value.Dollar;
        Cents = Cents - value.Cents;
        return *this;
    }

6、重载结构体的“*”操作符(在结构体内部)

Currency& operator*(Currency& value)
    {
        Dollar *= value.Dollar;
        Cents *= value.Cents;
        return *this;
    }

7、定义函数模板格式的输出函数

template <typename T>
void DisplayValue(T value)
{
    cout<<value<<endl;
}

8、进行运行测试。。。

Currency c1;
c1.Dollar = 10;
c1.Cents = 5;
DisplayValue(c1);
Currency c2,c3;
c2 = c1;
c3= c1+c2;
DisplayValue(c3);

附上完整程序代码。。。

#include "stdafx.h"
#include <iostream>
using namespace std;
 
template <typename T>
void DisplayValue(T value)
{
    cout<<value<<endl;
}
 
struct Currency
{
    int Dollar;
    int Cents;
     
    Currency& operator=(Currency& value)
    {
        Dollar = value.Dollar;
        Cents = value.Cents;
        return *this;
    }
    Currency& operator+(Currency& value)
    {
        Dollar += value.Dollar;
        Cents += value.Cents;
        return *this;
    }
    Currency &operator-(Currency& value)
    {
        Dollar = Dollar - value.Dollar;
        Cents = Cents - value.Cents;
        return *this;
    }
    Currency& operator*(Currency& value)
    {
        Dollar *= value.Dollar;
        Cents *= value.Cents;
        return *this;
    }
    friend ostream &operator<<(ostream &out,Currency value);
};
 
ostream& operator<<(ostream &out,Currency value)
{
    out<<"The dollar = "<<value.Dollar<<" and The Cents = "<<value.Cents<<endl;
    return out;
}
int _tmain(int argc, _TCHAR* argv[])
{
 
    Currency c1;
    c1.Dollar = 10;
    c1.Cents = 5;
    DisplayValue(c1);
    Currency c2,c3;
    c2 = c1;
    c3= c1+c2;
    DisplayValue(c3);
    system("pause");
    return 0;
}

The end...

结构体中定义函数指针

转自:http://blog.csdn.NET/unix21/article/details/9293877

结构体指针变量的定义,定义结构体变量的一般形式如下:

形式1:先定义结构体类型,再定义变量
struct结构体标识符
{
成员变量列表;…
};
struct 结构体标识符 *指针变量名;

变量初始化一:struct结构体标识符 变量名={初始化值1,初始化值2,…, 初始化值n };


形式2:在定义类型的同时定义变量
struct结构体标识符
{
成员变量列表;…
} *指针变量名;

变量初始化二:


形式3:直接定义变量,用无名结构体直接定义变量只能一次
struct
 {
成员变量列表;…
}*指针变量名;


其中“指针变量名”为结构体指针变量的名称。形式1是先定义结构体,然后再定义此类型的结构体指针变量;形式2和形式3是在定义结构体的同时定义此类型的结构体指针变量。


函数指针的定义

  一般的函数指针可以这么定义:

  int(*func)(int,int);

  表示一个指向含有两个int参数并且返回值是int形式的任何一个函数指针. 假如存在这样的一个函数:

  int add2(int x,int y)

  {

  return x+y;

  }

  那么在实际使用指针func时可以这样实现:

  func=&add2; //指针赋值,或者func=add2; add2与&add2意义相同

  printf("func(3,4)=%d\n",func(3,4));

  事实上,为了代码的移植考虑,一般使用typedef定义函数指针类型.

  typedef int(*FUN)(int,int);

  FUN func=&add2;

  func();



结构体中包含函数指针

其实在结构体中,也可以像一般变量一样,包含函数指针变量.下面是一种简单的实现.

#include <stdio.h>  struct DEMO  {  int x,y;  int (*func)(int,int); //函数指针  };    int add1(int x,int y)  {  return x*y;  }    int add2(int x,int y)  {  return x+y;  }    void main()  {  struct DEMO demo;  demo.func=add2; //结构体函数指针赋值  //demo.func=&add2; //结构体函数指针赋值  printf("func(3,4)=%d\n",demo.func(3,4));  demo.func=add1;  printf("func(3,4)=%d\n",demo.func(3,4));  }    /* 输出: func(3,4)=7 func(3,4)=12 */  

结构体中指向函数的指针                                          

C语言中的struct是最接近类的概念,但是在C语言的struct中只有成员,不能有函数,但是可以有指向函数的指针,这也就方便了我们使用函数了。举个例子,如下:
#include <stdio.h>  #include <stdlib.h>  #include <string.h>    typedef struct student  {      int id;      char name[50];       void (*initial)();      void (*process)(int id, char *name);      void (*destroy)();  }stu;    void initial()  {      printf("initialization...\n");  }    void process(int id, char *name)  {      printf("process...\n%d\t%s\n",id, name);  }    void destroy()  {      printf("destroy...\n");  }    int main()  {      stu *stu1;      //在VC和TC下都需要malloc也可以正常运行,但是linux gcc下就会出错,为段错误,必须malloc      stu1=(stu *)malloc(sizeof(stu));      //使用的时候必须要先初始化      stu1->id=1000;      strcpy(stu1->name,"C++");      stu1->initial=initial;      stu1->process=process;      stu1->destroy=destroy;      printf("%d\t%s\n",stu1->id,stu1->name);      stu1->initial();      stu1->process(stu1->id, stu1->name);      stu1->destroy();      free(stu1);      return 0;  }  

输出:

/*
1000    C++
initialization...
process...
1000    C++
destroy...
*/

C语言中,如何在结构体中实现函数的功能?把结构体做成和类相似,让他的内部有属性,也有方法
这样的结构体一般称为协议类,提供参考: 
struct { 
 int funcid; 
 char *funcname; 
 int (*funcint)(); /* 函数指针 int 类型*/ 
 void (*funcvoid)(); /* 函数指针 void类型*/ 
}; 
每次都需要初始化,比较麻烦

#include <stdio.h>    typedef struct  {  int a;  void (*pshow)(int);  }TMP;    void func(TMP *tmp)  {      if(tmp->a >10)//如果a>10,则执行回调函数。      {          (tmp->pshow)(tmp->a);      }  }    void show(int a)  {      printf("a的值是%d\n",a);  }    void main()  {      TMP test;      test.a = 11;      test.pshow = show;      func(&test);  }    /* 一般回调函数的用法为: 甲方进行结构体的定义(成员中包括回调函数的指针)  乙方定义结构体变量,并向甲方注册, 甲方收集N个乙方的注册形成结构体链表,在某个特定时刻遍历链表,进行回调。 当 函数指针 做为函数的参数,传递给一个被调用函数, 被调用函数就可以通过这个指针调用外部的函数,这就形成了回调<p>一般的程序中回调函数作用不是非常明显,可以不使用这种形式</p><p>最主要的用途就是当函数不处在同一个文件当中,比如动态库,要调用其他程序中的函数就只有采用回调的形式 通过函数指针参数将外部函数地址传入来实现调用</p><p>函数的代码作了修改,也不必改动库的代码,就可以正常实现调用便于程序的维护和升级</p>*/  



阅读全文
0 0