c/c+{}コードブロックの使用
6855 ワード
変数がコードブロック"{}"で定義されている場合、その変数の生存期間と役割ドメインはコードブロック内に制限されます.
コード#コード#
しゅつりょく
コード#コード#
#include
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 : "<std::endl;
}
~SimpleTestClass()
{
std::cout<<"the SimpleTestClass destroys"<<std::endl;
std::cout<<"the value of the private member m_id is : "<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 : "<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