0

This is simply just frustrating me to the brink of implosion. I do not know why I cannot figure this out, however, I need some assistance. Or at least some rhetorical questions to lead me in the right direction.

I have an object called expression. This object is a struct of tokenType tokens.

//expression.h file
struct expression
{

    int numTokens ;
    tokenType tokens[MAX_TOKENS_IN_EXPRESSION] ;

    void print() const ;
    ...
}

tokenType is simply either a number (operand) or a symbol (operator)

Given that, I have these lines of code in my test.cpp file:

expression expr1 , expr2 ;
...
errorCode = expr1.toPostfix( expr2 ) ;

I have to implement toPostFix( [parameter] ); Its function declaration is as follows:

//in expression.cpp
int expression::toPostfix( expression & pfx ) const
{
    int result = 1; 

    //how do I read in the 'expression' that is calling this method???

    return result;
}

Where if result = 0 the expression was successfully converted to PostFix notation, unsuccessful otherwise.

The issue I am having is the comment in the middle of the provided code...how do I retrieve the data being passed into the function???

I'm not asking for code, because I feel like this is a very simple question, and just a brain fart on my part. So if anyone could please just kick me in the right direction I would appreciate it. Thanks

3
  • Just curious if you searched at all, because there are quite a few questions like this on SO and elsewhere. Apr 11, 2012 at 1:34
  • I've tried, searching SO and google, but I guess I was asking the wrong question, I apologize for being redundant
    – persinac
    Apr 11, 2012 at 1:40
  • No apologies necessary, it's just that this is pretty baseline C++, and any tutorial/etc. covers this--being able to find stuff is a really important skill. Apr 11, 2012 at 1:47

1 Answer 1

2

When you call

expr1.toPostfix(expr2);

then the pointer to expr1 is passed as the (hidden) this pointer inside the method.

The result of this (no pun intended) is that you can access member functions and variables on the called object inside expression::toPostfix() with or without using this->. Usually, C++ programmers omit this-> when accessing members.

Of course you can access the methods of expr2 by using pfx inside your method.

I must say, this is pretty fundamental C++ stuff. You might consider following through a C++ tutorial to solidify your grasp on things.

1
  • Yea I know it's fundamental stuff, I obviously need to continue practicing, but this was really just frustrating me. Thank you for the explanation though
    – persinac
    Apr 11, 2012 at 1:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.