Logical public variable in object programming



  • 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


  • Moderators

    @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.



  • @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).



  • @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?



  • @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.



  • @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?



  • @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.



  • 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?



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.