Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Logical public variable in object programming
Forum Updated to NodeBB v4.3 + New Features

Logical public variable in object programming

Scheduled Pinned Locked Moved Solved C++ Gurus
9 Posts 3 Posters 1.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • elicatE Offline
    elicatE Offline
    elicat
    wrote on last edited by
    #1

    Hello,
    sorry there is still something that escapes me in object programming (C++).
    I have three Classes, for example ClassA and ClassB and ClassC.

    1. In class B I declare the string variable Public strPippo
    QString strPippo;
    
    1. in class A the Class B is imposed and the variable is established
    classB * engineClasseB = new ClassB;
    engineClasseB-> strPippo = "xxxyyyzzz22";
    
    1. I would like to read the public variable strPippo in classC, but it is empty.
    classB * engineClasseB = new ClassB;
    qDebug () << engineClasseB-> strPippo;
    

    Where am I wrong with reasoning?

    Thank you all
    P.S. this is another step to clarify my ideas

    Saluti, Gianfranco Elicat

    raven-worxR JonBJ 2 Replies Last reply
    0
    • elicatE elicat

      Hello,
      sorry there is still something that escapes me in object programming (C++).
      I have three Classes, for example ClassA and ClassB and ClassC.

      1. In class B I declare the string variable Public strPippo
      QString strPippo;
      
      1. in class A the Class B is imposed and the variable is established
      classB * engineClasseB = new ClassB;
      engineClasseB-> strPippo = "xxxyyyzzz22";
      
      1. I would like to read the public variable strPippo in classC, but it is empty.
      classB * engineClasseB = new ClassB;
      qDebug () << engineClasseB-> strPippo;
      

      Where am I wrong with reasoning?

      Thank you all
      P.S. this is another step to clarify my ideas

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @elicat
      since you create a new instance of ClassB everytime it's public strPippo is empty.
      It seems you rather look for a static variable? But then you also do not need to create instance of Class B then.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      3
      • elicatE elicat

        Hello,
        sorry there is still something that escapes me in object programming (C++).
        I have three Classes, for example ClassA and ClassB and ClassC.

        1. In class B I declare the string variable Public strPippo
        QString strPippo;
        
        1. in class A the Class B is imposed and the variable is established
        classB * engineClasseB = new ClassB;
        engineClasseB-> strPippo = "xxxyyyzzz22";
        
        1. I would like to read the public variable strPippo in classC, but it is empty.
        classB * engineClasseB = new ClassB;
        qDebug () << engineClasseB-> strPippo;
        

        Where am I wrong with reasoning?

        Thank you all
        P.S. this is another step to clarify my ideas

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @elicat
        Either you intend just what @raven-worx has written.
        Or you need, in some shape or form, to pass the engineClasseB instance you created in classA around to some classC instance you created.

        In your code you have two separate occurrences of classB * engineClasseB = new ClassB;. Every time you see a new you are creating a brand new instance, and what you do to one instance (your case #2) has no effect on some other instance (your case #3).

        elicatE 1 Reply Last reply
        2
        • elicatE Offline
          elicatE Offline
          elicat
          wrote on last edited by
          #4

          @raven-worx said in Logical public variable in object programming:

          since you create a new instance of ClassB everytime it's public strPippo is empty.
          It seems you rather look for a static variable? But then you also do not need to create instance of Class B then.

          the value of strPippo is variable not static. How can I read the value set in classA of classeb->strPippo from another class?

          Saluti, Gianfranco Elicat

          JonBJ 1 Reply Last reply
          0
          • elicatE elicat

            @raven-worx said in Logical public variable in object programming:

            since you create a new instance of ClassB everytime it's public strPippo is empty.
            It seems you rather look for a static variable? But then you also do not need to create instance of Class B then.

            the value of strPippo is variable not static. How can I read the value set in classA of classeb->strPippo from another class?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @elicat
            Like I wrote, you need to understand instances. Instances are not classes. That's where your confusion lies. In classA you created an instance

            classB * engineClasseB = new ClassB;
            

            Only that particular engineClasseB instance has its strPippo = "xxxyyyzzz22". It doesn't matter which way up or down you look at it, until you pass that instance variable to something in your classC it won't be looking at the instance which has that set.

            1 Reply Last reply
            2
            • JonBJ JonB

              @elicat
              Either you intend just what @raven-worx has written.
              Or you need, in some shape or form, to pass the engineClasseB instance you created in classA around to some classC instance you created.

              In your code you have two separate occurrences of classB * engineClasseB = new ClassB;. Every time you see a new you are creating a brand new instance, and what you do to one instance (your case #2) has no effect on some other instance (your case #3).

              elicatE Offline
              elicatE Offline
              elicat
              wrote on last edited by
              #6

              @JonB Ok JonB, ok clear.
              So the correct question, maybe it's:
              how can I set a value of a variable defined in a class and then read it, or modify it, from another class?

              Saluti, Gianfranco Elicat

              JonBJ 1 Reply Last reply
              0
              • elicatE elicat

                @JonB Ok JonB, ok clear.
                So the correct question, maybe it's:
                how can I set a value of a variable defined in a class and then read it, or modify it, from another class?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @elicat
                Is it a class variable (i.e. has static in front of it) or an instance ("member") variable?

                Assuming you mean a normal instance/member variable then you read/write it via the instance. Like the following, just an example (my C++ is rusty, excuse any slips):

                class classA
                {
                public:
                    # member variable
                    QString m_pippo;
                
                    # constructor, takes a QString argument
                    classA(QString initialValueForPippo)
                    {
                        # set the instance being created to have its m_pippo equal the string passed
                        m_pippo = initialValueForPippo;
                    }
                }
                
                class classB
                {
                public:
                    QString whatIsClassAInstanceValueOfPippo(const classAInstance *pA)
                    {
                        return pA->m_Pippo;
                    }
                
                    void changeClassAInstanceValueOfPippo(classAInstance *pA, QString newValue)
                    {
                        pA->m_Pippo = newValue;
                    }
                }
                
                # Outside world goes:
                classA *pA = new classA("Hello world");
                classB *pB = new classB;
                qDebug() << pA->m_Pippo;
                qDebug() << pB->whatIsClassAInstanceValueOfPippo(pA);
                pB->changeClassAInstanceValueOfPippo(pA, "Goodbye, cruel world");
                qDebug() << pB->whatIsClassAInstanceValueOfPippo(pA);
                

                But somehow I don't think this is what you're expecting. There isn't any real relationship between all these classes (we're just passing instances around), I don't know if you're expecting there to be any relationships between them....

                I think, politely, that either you're still confusing the distinction between a class and an instance of that class, or you're trying to ask for something specific which your question/definition does not convey.

                1 Reply Last reply
                1
                • elicatE Offline
                  elicatE Offline
                  elicat
                  wrote on last edited by elicat
                  #8

                  I do not look for relationships but only the way to read a variabil and manage it and probably the example that I posted could solve the problem ..
                  I'll try but. . . I did not understand when declaring the methods of the classB

                  void changeClassAInstanceValueOfPippo(classAInstance *pA, QString newValue)
                  
                  and
                  
                  QString whatIsClassAInstanceValueOfPippo(const classAInstance *pA)
                  

                  ??

                  it is clear that * pA is a pointer to a new class A instance
                  what type of object is * pA ??
                  is an instance of the class but how do I declare it?

                  Buongiono JonB, I understand for those who know well object programming is exhausting when someone asks a "stupid" question.
                  But after so many years of "classical" programming and object planning theory, it is necessary to practice and experience to learn the concepts.

                  It is clear that an instance of a class exegundes the variables that are imposed only visible in the competence in which it was performed. It has been said that if I need the value of the variable set I have to pass it .. ok just and clear .. but if I have set this variable in a program phase and then I will have to read it later in other classes and at other times of the process .. . how can I do? Do I have to carry the variable with me at every step?

                  Saluti, Gianfranco Elicat

                  JonBJ 1 Reply Last reply
                  0
                  • elicatE elicat

                    I do not look for relationships but only the way to read a variabil and manage it and probably the example that I posted could solve the problem ..
                    I'll try but. . . I did not understand when declaring the methods of the classB

                    void changeClassAInstanceValueOfPippo(classAInstance *pA, QString newValue)
                    
                    and
                    
                    QString whatIsClassAInstanceValueOfPippo(const classAInstance *pA)
                    

                    ??

                    it is clear that * pA is a pointer to a new class A instance
                    what type of object is * pA ??
                    is an instance of the class but how do I declare it?

                    Buongiono JonB, I understand for those who know well object programming is exhausting when someone asks a "stupid" question.
                    But after so many years of "classical" programming and object planning theory, it is necessary to practice and experience to learn the concepts.

                    It is clear that an instance of a class exegundes the variables that are imposed only visible in the competence in which it was performed. It has been said that if I need the value of the variable set I have to pass it .. ok just and clear .. but if I have set this variable in a program phase and then I will have to read it later in other classes and at other times of the process .. . how can I do? Do I have to carry the variable with me at every step?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @elicat said in Logical public variable in object programming:

                    it is clear that * pA is a pointer to a new class A instance
                    what type of object is * pA ??
                    is an instance of the class but how do I declare it?

                    You already answered for yourself what type of object pA is --- it's a pointer to a class instance.

                    How to declare it? I gave you:

                    # Outside world goes:
                    classA *pA = new classA("Hello world");
                    

                    but if I have set this variable in a program phase and then I will have to read it later in other classes and at other times of the process .. . how can I do? Do I have to carry the variable with me at every step?

                    Yes, like any other variable, if you need to access the object all over the place you have to pass it around. If it turns out(?) you often access the classA instance from your classB instance, you could create a classA * member variable in your classB and then if you're passing a classB instance around it will carry with it a classA * to save you having to pass that around separately.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved