zoukankan      html  css  js  c++  java
  • C++ 静态变量

    #include <iostream>
    #include "ros/ros.h"
    #include "std_msgs/String.h"
    #include "sensor_msgs/Imu.h"
    #include "std_msgs/Int32MultiArray.h"
    #include "nav_msgs/Odometry.h"

    #include <tf/transform_broadcaster.h>
    #include <cstdlib>
    #include <eigen3/Eigen/Dense>
    #include <eigen3/Eigen/Core>

    #include <message_filters/subscriber.h>
    #include <message_filters/time_synchronizer.h>
    #include <queue>

    using namespace std;
    //相同的内存地址
    int i=1; // i 为全局变量,具有静态生存期。

    void other(void)
    {
    static int a=2;
    static int b;
    // a,b为静态局部变量,具有全局寿命,局部可见。
    //只第一次进入函数时被初始化。
    int c=10; // C为局部变量,具有动态生存期
    //每次进入函数时都初始化。
    a=a+2; i=i+32; c=c+5;
    cout<<"---OTHER--- ";
    cout<<" i: "<<i<<" a: "<<a<<" b: "<<b<<" c: "<<c<<endl;
    b=a;
    }

    int main(int argc, char **argv)
    {
    ros::init(argc,argv,"test_c");
     
    ros::NodeHandle n;
    static int a; // 静态局部变量,有全局寿命,局部可见。
    int b=-10; // b, c为局部变量,具有动态生存期。
    int c=0;
    void other(void);
    cout<<"---MAIN--- ";
    cout<<" i: "<<i<<" a: "<<a<<" b: "<<b<<" c: "<<c<<endl;//1 0 -10 0
    c=c+8; other();// 33 4 0 15
    cout<<"---MAIN--- ";
    cout<<" i: "<<i<<" a: "<<a<<" b: "<<b<<" c: "<<c<<endl;//33 0 -10 8
    i=i+10; other(); //75 6 4 15
    other(); //107 8 6 15
    system("pause");
    ros::spin();
    return 0;
    }
  • 相关阅读:
    输入框只能输入正数且保留小数后2位数字,正则
    vscode安装ESlint配置
    kubreretes && docker 模块
    python3安装sqlite3库
    dokcerfile 制作Msql镜像
    前端模板
    Python SMTP发送邮件
    k8s-api
    openpyxl
    k8s mysql主从
  • 原文地址:https://www.cnblogs.com/nku-wangfeng/p/14419942.html
Copyright © 2011-2022 走看看