c/c++ {}代码块的使用

  如果在代码块”{}”中定义了变量,则该变量的生存周期和作用域将被限制在该代码块内。

代码
#include <iostream>

class SimpleTestClass
{
public:
    SimpleTestClass()
    {
        std::cout<<"the SimpleTestClass creates"<<std::endl;
    }
    SimpleTestClass(int id):m_id(id)
    {
        std::cout<<"the SimpleTestClass creates"<<std::endl;
        std::cout<<"the value of the private member m_id is : "<<m_id<<std::endl;
    }
    ~SimpleTestClass()
    {
        std::cout<<"the SimpleTestClass destroys"<<std::endl;
        std::cout<<"the value of the private member m_id is : "<<m_id<<std::endl;
    }  
    void TestRun()
    {
        std::cout<<"the SimpleTestClass public function is running."<<std::endl;
        std::cout<<"the value of the private member m_id is : "<<m_id<<std::endl;
    }
private:
    int m_id;
};

int main()
{
    SimpleTestClass object(1);
    object.TestRun();
    {
        /// 代码块内旧的object在内部代码块和外部代码块都是可见的,而新的object就只在内部代码块中可见。
        /// 两个同名对象(一个位于外部代码块中,一个位于内部代码块中),程序执行内部代码块中的语句时,
        /// 将新的object解释为局部代码块变量,新的定义隐藏了以前的定义,新定义可见,旧定义暂时不可见。
        SimpleTestClass object(2);
        object.TestRun();
        SimpleTestClass myobject(3);
        myobject.TestRun();
        /// 代码块内执行完成时,myobject和object对象析构函数被调用,资源被回收。
    }
    /// 在程序离开该代码块时,旧的定义又重新可见。
    object.TestRun();

    return 0;
}
输出

the SimpleTestClass creates
the value of the private member m_id is : 1

the SimpleTestClass public function is running.
the value of the private member m_id is : 1

the SimpleTestClass creates
the value of the private member m_id is : 2

the SimpleTestClass public function is running.
the value of the private member m_id is : 2

the SimpleTestClass creates
the value of the private member m_id is : 3

the SimpleTestClass public function is running.
the value of the private member m_id is : 3

the SimpleTestClass destroys
the value of the private member m_id is : 3

the SimpleTestClass destroys
the value of the private member m_id is : 2

the SimpleTestClass public function is running.
the value of the private member m_id is : 1

the SimpleTestClass destroys
the value of the private member m_id is : 1
  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言是一种非常常用的计算机编程语言,也是许多程序员的入门语言。但是在编写程序的过程中,难免会出现一些错误。下面我来讲解如何改正C语言程序中的错误。 首先,我们需要学会阅读并理解错误信息。当程序出现错误时,C语言编译器会输出相应的错误信息,我们可以根据错误信息来定位和解决问题。 其次,检查语法错误。语法错误是编程过程中最常见的错误之一。C语言是一种强类型语言,因此在声明变量、赋值、调用函数等地方都需要遵循一定的语法规则。如果程序中存在与语法规则不符的地方,编译器就会报错。在修复语法错误时,需要仔细检查每个字符、标点符号和代码结构,确保其符合语法要求。 还需要检查逻辑错误。逻辑错误是指程序在运行过程中的输出结果与预期不符。例如,程序计算一个数的平方根,但结果却是负数。这时候我们要检查是否在计算平方根之前没有判断数值是否合法,或者计算过程中是否存在除以0的情况等。 最后,进行调试。调试是指逐行或逐块地运行程序,查找可能出错的地方。在调试过程中,可以使用printf语句输出一些中间结果,以确定程序是否按照预期执行。如果在某个地方出现错误,我们可以通过逐个比较变量的值、条件语句的判断结果等方法来找出错误所在。 总之,改正C语言程序中的错误需要耐心和细心,需要从语法、逻辑和调试等多个方面全面考虑。通过不断的学习和实践,我们可以更好地掌握C语言编程,提高自己的编程能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值