c++classmethods

Some class methods failing to access some attributes of the same class, while other succeed


The overall structure of my code is to have two struttures, with their own methods, nested in a class that operates on them, like this:

class Solution {

  struct TypeText{
    //some code, attributes and methods
  };

  struct TypePattern{
    //some code, attributes and methods
    void printPattern(){} //this is the debug function that get's called in a weird way
  };

  TypeText text;
  TypePattern pattern;

  bool increaseOccurrencies(){
    //some code
    pattern.printPattern();
    //some code
  }

public:
  bool isMatch(string s, string p){
    TypeText text(s);
    TypePattern pattern(s);
    //some code
    pattern.printPattern(); //if I call this method here, it prints the correct values of pattern 
    increaseOccurrencies(); //if I call this method, that then calls pattern.printPattern, it prints a "new" pattern which is void (it has experienced only the default contructor and didn't receive the string p)
    //some code
  }

I'm autonosmly studing/practicing c++, so I expect my overall structure to be lacking, yet I'm having serius trouble understanding why this behaviour occurs...

I've tryied moving increaseOccurrencies() inside TypePattern, and that fixed the problem, but since I need to have access to TypeText too I didn't like this solution.


Solution

  • The problem here is that you have two objects called pattern and you are getting confused between them (or maybe you think they are the same object).

    This is one pattern, it is a member of the Solution class

    TypePattern pattern;
    

    Here is the other pattern, it is a local variable in the isMatch function, it is not a member of the Solution class.

    TypePattern pattern(s);
    

    Now pattern.printPattern() in the increaseOccurrencies function operates on the pattern that is part of the Solution class, because that is the decalration that is visiable at that point. But pattern.printPattern() in the isMatch function operates on the pattern that is not part of the Solution class, because, again, that is the declaration that is visble at that point. In fact the declaration TypePattern pattern(s); hides the declaration TypePattern pattern; (technically this is called shadowing).

    I would offer a solution to this problem, but I'm not completely sure what you are trying to achieve. Maybe you just need to replace

    TypeText text(s);
    TypePattern pattern(s);
    

    with

    text = s;
    pattern = s;
    

    so that you do not have multiple objects with similar names.

    One other point (not strictly related to your question). Just because you are using TextType and TextPattern inside of Solution does not mean that you have to put the classes inside of Solution. It would be better to place them before the Solution class.

    class TypeText
    {
    };
    
    class TypePattern
    {
    };
    
    class Solution
    {
        TypeText text;
        TypePattern pattern;
    };
    

    Neted classes are rarely used in C++ because they don't really bring any benefits. It's better for classes to be independent.