0

I am creating two objects of the same class (using c++).

When I change the data of one object, the data of the other object is automatically changing. I know this is the way it works in c++.

However, is there a technique I can use in order to achieve what I want?

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

When the GetToken() is called on the lookAheadLexer , the class data is changing and it seems as if GetToken is also called on the lexer .

Followed this link Why do objects of the same class have access to each other's private data?

Thank you for your help

Community
  • 1
  • 1
Techworld
  • 141
  • 1
  • 12
  • 4
    "When I change the data of one object, the data of the other object is automatically changing. I know this is the way it works in c++." Uh, this is not how C++ works, at least not without references and doing this explicitly. Give a MCVE http://stackoverflow.com/help/mcve – GManNickG Apr 22 '16 at 23:28
  • followed this link http://stackoverflow.com/questions/6921185/why-do-objects-of-the-same-class-have-access-to-each-others-private-data – Techworld Apr 22 '16 at 23:30
  • `lookAheadLexer = lexer; return lookAhead = lookAheadLexer.GetToken();` lookAheadLexer IS lexer after that. Ofcourse they both change... – Unimportant Apr 22 '16 at 23:30
  • @Techworld : What's explained in that link is that a instance of a class can access another instance of the same class's private members, for example, when writing a copy constructor. It does not mean both instances share the same members asif they were static. – Unimportant Apr 22 '16 at 23:33
  • I understand what you mean. However, I need to set the value of the lookaheadlexer to the current value of the lexer in order to read the next token without changing information from the lexer – Techworld Apr 22 '16 at 23:33
  • I've been stuck on this for a very long time. Would appreciate any help – Techworld Apr 22 '16 at 23:37
  • 1
    _" I know this is the way it works in c++."_ Well it's not, so... – Lightness Races in Orbit Apr 22 '16 at 23:44
  • The reason we ask for an [MCVE], with all due respect, is because otherwise most questions here would be solely based on misconceptions. Again with all due respect, this is one of those questions. Without an MCVE, we cannot help you, because all you've shown us is misconceptions. The rules are there for a reason!! – Lightness Races in Orbit Apr 22 '16 at 23:45
  • It is only "the way it works in C++" if your code makes it so. The `return lookAhead = lookAheadLexer.GetToken()` statement in your code explicitly changes `lookAhead` and returns its new value. So your code has made a change. – Peter Apr 23 '16 at 00:20
  • I do want the lookAheadLexer's value to change however, this is changing data (such as counters and other variables) of the lexer's object. – Techworld Apr 23 '16 at 00:29
  • Then you need to provide something to use, instead of `GetToken()`, that returns required information and doesn't change the object. You can't have your cake and eat it too ..... if a function changes an object, you can't expect to call it and have that object remain unchanged. – Peter Apr 23 '16 at 00:39
  • If your Lexer does not have an UngetToken method, wrap it in a BufferedLexer class. – zdf Apr 23 '16 at 07:27

0 Answers0