• C++指针与数组


    #include <iostream>
    #include "Node.cpp"
    using namespace std;
    
    int main()
    {
        std::cout << "Hello World!
    ";
    
        int a[5], * p;
        int sum, i;
    
        a[0] = 1;
        a[1] = 2; 
        a[2] = 3;
        a[3] = 4;
        a[4] = 5;
    
        for ( sum = a[0],i = 1; i < 5; i++)
        {
            sum += a[i];
        }
    
        sum = 0;
        
        for (sum = *a, i = 1; i < 5; i++)
        {
            sum += *(a + i);
        }
        
        sum = 0;
    
        for (sum = *a, p = a + 1; p < a + 5; p++)
        {
            sum += *p;
        }
        //a+1 等同于&a[1]
    
        p = new int[10];
    
        delete[]p;
    
        Node node1("Roger", 20), node2(node1);
    
        strcpy_s(node2.name, sizeof("Wendy"), "Wendy");
    
        node2.age = 30;
    
        cout << node1.name << ' ' << node1.age << '  ' << node2.name << '  ' << node2.age;
    
        node1 = node2;
    
        /*free(node1.name);
        free(node2.name);*/
    }
    #include <string.h>
    #include <corecrt_malloc.h>
    struct Node
    {
        char* name;
        int age;
    public:
        Node(char* n = " ", int a = 0)
        {
            name = _strdup(n);
            age = a;
        }
    
        ~Node()
        {
            if (name != 0)
            {
                free(name);
            }
        }
    
        Node(const Node& n) 
        {
            name = _strdup(n.name);
            age =n.age;
        }
    
        Node& operator =(const Node& n) 
        {
            if (this != &n)
            {
                if (name != 0)
                {
                    free(name);
                }
    
                name = _strdup(n.name);
                age = n.age;
            }
    
            return *this;
        }
    };
  • 相关阅读:
    redis数据结构-list
    reids数据结构1-string
    jedis工具类
    静态资源放行
    SpringMVC拦截器
    xinetd
    Linux-open函数
    Linux简单的文件读取
    复习ssm02
    springMVC文件上传
  • 原文地址:https://www.cnblogs.com/ms_senda/p/11299839.html
Copyright © 2020-2023  润新知