-
Hello,
sorry there is still something that escapes me in object programming (C++).
I have three Classes, for example ClassA and ClassB and ClassC.- In class B I declare the string variable Public strPippo
QString strPippo;
- in class A the Class B is imposed and the variable is established
classB * engineClasseB = new ClassB; engineClasseB-> strPippo = "xxxyyyzzz22";
- 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 -
@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 theengineClasseB
instance you created inclassA
around to someclassC
instance you created.In your code you have two separate occurrences of
classB * engineClasseB = new ClassB;
. Every time you see anew
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. InclassA
you created an instanceclassB * engineClasseB = new ClassB;
Only that particular
engineClasseB
instance has itsstrPippo = "xxxyyyzzz22"
. It doesn't matter which way up or down you look at it, until you pass that instance variable to something in yourclassC
it won't be looking at the instance which has that set. -
@elicat
Is it a class variable (i.e. hasstatic
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 classBvoid 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 yourclassB
instance, you could create aclassA *
member variable in yourclassB
and then if you're passing aclassB
instance around it will carry with it aclassA *
to save you having to pass that around separately.