// assign_aTest.cpp : Defines the entry point for the console application. // #include “stdafx.h” #include “boost/spirit.hpp” #include “boost/spirit/actor/ref_actor.hpp” using namespace boost; using namespace boost::spirit; #include struct MyParser : public grammar { mutable int m_i; /// Constructor MyParser() { } /// Grammar definition template struct definition { definition(MyParser const& self) { test = (ch_p(L’@')) [assign_a(self.m_i, 100)]; } rule test; // Start rule rule const& start() const { return test; } }; }; struct MyParser2 : public grammar { mutable int m_i; static const int s_i = 100; /// Constructor MyParser2() { } /// Grammar definition template struct definition { definition(MyParser2 const& self) { test = (ch_p(L’@')) [assign_a(self.m_i, s_i)]; } rule test; // Start rule rule const& start() const { return test; } }; }; int _tmain(int argc, _TCHAR* argv[]) { struct MyParser pa; pa.m_i = 0; parse(L”@”, pa); struct MyParser2 pa2; pa2.m_i = 0; parse(L”@”, pa2); std::cout<<"Parse1: "<<pa.m_i<<std::endl; std::cout<<"Parse2: "<<pa2.m_i<<std::endl; std::cin.get(); return 0; } VC在函数中将常数值赋给一个常引用,会导致出函数有效区域后此引用的无效,为一个不确定值。 更一般的测试例程: // RefTst.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include using namespace std; class A { private: const int& m_i; public: A(const int& i) : m_i(i) { } void PrintI() { cout<<"I: "<<m_i<<endl; const int* p = &m_i; cout<<"P: "<<p<<endl; } }; A* GetA() { A* pA = new A(123456789); pA->PrintI(); return pA; } void Print(A* pA) { int i = 54321; __int64 j = 13328703583; pA->PrintI(); } int _tmain(int argc, _TCHAR* argv[]) { A* pA = NULL; pA = GetA(); pA->PrintI(); Print(pA); delete pA; cin.get(); return 0; }
日存档: 2011-07-25
VS bug
作者: nick
日期: 2011-07-25
没有评论
锁事–2011.7.25
作者: amy
日期: 2011-07-25
没有评论
一天好快又要结束了,时间总是那么静悄悄的,永远也找不到它流过的痕迹。今天的工作还比较轻松,只是晚上公司的蓝球赛要多留一个小时,好郁闷- -!。明天早上争取早点起,做班车,然后下班了去理发!、
近期评论