zoukankan      html  css  js  c++  java
  • C++中>操作符的重载

    1 // main.cpp
    2  #include <iostream>
    3 using namespace std;
    4 #include <conio.h>
    5 struct Dummy
    6 {
    7 int a;
    8 int b;
    9 int c;
    10 };
    11 class Ptr
    12 {
    13 public:
    14 Dummy* p;
    15 Ptr()
    16 {
    17 this->p = 0;
    18 }
    19 Ptr(Dummy* p)
    20 {
    21 this->p = p;
    22 }
    23 Ptr(Ptr& p)
    24 {
    25 this->p = p.p;
    26 }
    27 void operator=(Dummy* p)
    28 {
    29 this->p = p;
    30 }
    31 void operator=(Ptr& p)
    32 {
    33 *(this->p) = *(p.p);
    34 }
    35 Dummy* operator->()
    36 {
    37 return this->p;
    38 }
    39 //operator Dummy*()
    40 //{
    41 // return this->p;
    42 //}
    43 };
    44 void main()
    45 {
    46 Dummy a;
    47 a.a = 0;
    48 a.b = 1;
    49 a.c = 2;
    50 Ptr p = &a; // 当使用Ptr指针的时候,使用->重载会与原有的指针取成员变量操作符冲突,
    51 // 因此,只有在使用Ptr对象的时候才可以使用此操作符。而下面这种写法是无法通过编译的
    52 // Ptr p = new Ptr(&a);
    53 // cout<<p->a;
    54 cout<<p->a;
    55 getch();
    56 }
  • 相关阅读:
    ptmalloc内存分配和回收详解(文字版)
    HITCTF2018
    缓冲区溢出保护机制——Windows
    缓冲区溢出保护机制——Linux
    TAMUCTF
    反汇编简介
    apkg命令
    ubuntu基本命令
    ubuntu命令~
    apt-get用法
  • 原文地址:https://www.cnblogs.com/lilei9110/p/1825544.html
Copyright © 2011-2022 走看看