// 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;
}
0 条评论。