• C++ 语言中的重载、内联、缺省参数、隐式转换等机制展现了很多优点


    C++ 语言中的重载、内联、缺省参数、隐式转换等机制展现了很多优点,但是这些 优点的背后都隐藏着一些隐患。正如人们的饮食,少食和暴食都不可取,应当恰到好处。 我们要辨证地看待 C++的新机制,应该恰如其分地使用它们。

    虽然这会使我们编程时多 费一些心思,少了一些痛快,但这才是编程的艺术。

     1 #include <iostream>
     2 
     3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
     4 using namespace std;
     5 int main(int argc, char** argv) {
     6         //定义一个包含指针成员的结构类型
     7     struct test {
     8        char *str;
     9        int  *ip;
    10     } x;
    11 
    12     //使用结构变量x中的整型指针ip
    13     x.ip=new int;    //分配1个单元
    14     *(x.ip)=100;
    15     cout<<"x.ip:"<<x.ip<<'	'<<*(x.ip)<<endl;
    16     cout<<"---------------"<<endl;
    17     delete x.ip;
    18     x.ip=new int[5];    //分配5个单元
    19     for(int i=0;i<5;i++)
    20         *(x.ip+i)=100+i;
    21     cout<<"x.ip:"<<endl;
    22     for(int i=0;i<5;i++)
    23         cout<<x.ip+i<<'	'<<(*(x.ip+i))<<endl;
    24     delete x.ip;
    25     cout<<"---------------"<<endl;
    26 
    27     //使用结构变量x中的字符型指针str
    28     x.str=new char('A');    //分配1个单元
    29     cout<<"x.str:"<<(*x.str)<<endl;
    30     cout<<"---------------"<<endl;
    31     delete x.str;
    32     x.str=new char[5];    //分配多个单元
    33     *x.str='G';
    34     *(x.str+1)='o';
    35     *(x.str+2)='o';
    36     *(x.str+3)='d';
    37     *(x.str+4)='';
    38     cout<<"x.str:"<<x.str<<endl;
    39     delete x.str;
    40     cout<<"---------------"<<endl;
    41 
    42     //在声明结构变量时初始化
    43     test y={"Very Good!",NULL};
    44     cout<<"y.str:"<<y.str<<endl;
    45     cout<<"y.ip:"<<y.ip<<endl;
    46     return 0;
    47 }
  • 相关阅读:
    Perl的Open函数
    较详细的介绍JNI
    Java多线程单元测试
    sleep函数的简单原理
    Struts与Servlet的冲突
    Ant学习记录
    JDK转码工具
    Throwable
    Entity Framework系列文章导航
    多核时代 .NET Framework 4 中的并行编程1概述
  • 原文地址:https://www.cnblogs.com/borter/p/9406534.html
Copyright © 2020-2023  润新知