更改对象的数据而不更改其他对象的数据

时间:2016-04-22 23:21:34

标签: c++

我正在创建同一个类的两个对象(使用c ++)。

当我更改一个对象的数据时,另一个对象的数据会自动更改。我知道这是它在c ++中的工作方式。

但是,有没有一种技术可以用来达到我想要的效果呢?

Lexer::token currentToken;
Lexer::token getNextToken()
{
    return currentToken = lexer.GetToken();
}

//testing
Lexer lookAheadLexer = lexer;
Lexer::token lookAhead;
Lexer::token getLookAhead()
{
    lookAheadLexer = lexer; 
    return lookAhead = lookAheadLexer.GetToken();
}
//testing

GetToken()上调用lookAheadLexer时,类数据正在发生变化,似乎GetToken上也会调用lexer

点击此链接Why do objects of the same class have access to each other's private data?

感谢您的帮助

0 个答案:

没有答案
相关问题