| chief (159) | |||
|
I tried updating a private var via inheritance using a setter from the base class but it did not work. Here is the setter:
So I went around that and sent a reference object of the base class and called the setter that way and that worked. Here is the function:
When I called &cMovePlayer.SetPlayerPositionY(yAxis) it worked. It just seems like there is a better way of updating variables than passing objects...Could someone share a better way. Thanks | |||
|
|
|||
| Darkmaster (324) | |
|
i would use a set function for this or "friend" http://www.cplusplus.com/doc/tutorial/inheritance/ | |
|
|
|
| maeriden (230) | |
|
Is the base class inherited as public? This http://stackoverflow.com/questions/2676443/inheriting-private-members-in-c says that setters and getters of the base class can access private base members If you need to modify members of a base class you should consider making them protected. I can't specifically answer your question though | |
|
|
|
| chief (159) | ||||
|
@Darkmaster A setter doesn't work for me if I use a different object other than the object that set the private var so I will try using a friend function. @maeriden The base class is inherited as public and I am aware the sets/gets can access private member of a function through a public function but my problem comes when I want to update(set) a private member var form outside the class in which the private member variable resides and when I call a setter from outside the class, the base class private member variable does not update...here is a quick mock up of what I am getting at:
The number from the base class never updates and if I understand correctly it is because I set the number with a different object but that it just it. In my other program, my base setter sets a variable and I want a function outside the class to be able to set that same variable and have it updated in the child AND the base class and the only way I figured to do it is pass the base class' object through to the child class' function...sorry for that long explanation but I am just trying to be real clear. Will work with friend function for now and see if they work... | ||||
|
|
||||
| cire (1845) | ||
You're confusing yourself. Each object of type Yeehaw has its own numInQuestion object. If you want one object that is shared by all objects of a class, make the contained object static. | ||
|
|
||
| chief (159) | |
|
Hey thanks. I searched around on static objects and couldn't make sense of what you mean. Would I declare a static object in main like "static Yeehaw cYeehaw"? I tried that and it did not work though. | |
|
|
|
| cire (1845) | ||||
Yeehaw is not the contained object. numInQuestion is.
| ||||
|
|
||||
| chief (159) | |
| Thanks a bunch. I have too many questions but i'll just ask this one...after messing around with your mockup I noticed I have to initialize the static member variable globally. When I did not do it at first I kept getting a compiler error saying undefined reference to A. Why do have to initialize a value to the static member globally especially since the variable is initialized in the constructor? Thanks!!! | |
|
|
|
| cire (1845) | |
|
The variable is not initialized in a constructor. You may assign to it there, but that is not initialization. It is initialized when it is defined. (Line 12 above.) The member is only declared in the class definition. It is defined outside the class, but it retains class scope. Normally that definition would be in the class implementation file and not in the same file as main. | |
|
|
|
| chief (159) | |
|
Thanks | |
|
|
|