Is it bad or good programming practice to update private variables by passing references to objects to other functions???

I tried updating a private var via inheritance
using a setter from the base class but it
did not work. Here is the setter:

/**/void MovingThePlayer::SetPlayerPositionY(int y){yAxisPlayer = y;}


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:

void classCollisionHandling::HandleCollision(int xAxis, int yAxis, SDL_Rect *platform, MovingThePlayer &cMovePlayer)


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
i would use a set function for this
or "friend"
http://www.cplusplus.com/doc/tutorial/inheritance/
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
@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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "iostream"
using std::cout;
using std::endl;

class Yeehaw
{
    int numInQuestion;

    public:
            Yeehaw()
            {
                numInQuestion = 50;
            }
            int GetNumInQuestion()
            {
                return numInQuestion;
            }

            void SetNumInQuestion(int x)
            {
                numInQuestion = x;
            }
};

class PlaceHolederClass : public  Yeehaw
{
    public:
            PlaceHolederClass()
            {
            }
};

int main()
{
    Yeehaw cYeehaw;
    PlaceHolederClass cPlaceHolder;
    int num = 9;

///Get Number
    cout<<"This is the original number: "<<cYeehaw.GetNumInQuestion()<<endl<<endl;
    cout<<"\""<<num<<"\" will be put into the setter via the child class object"<<endl<<endl;

///Sets the Private Member Variable of the Base Class Via Child Class
    cPlaceHolder.SetNumInQuestion(num);

    cout<<"Number from base class after update: "<<cYeehaw.GetNumInQuestion()<<endl;
    cout<<"Number from child class after update: "<<cPlaceHolder.GetNumInQuestion()<<endl;

    return 0;
}


This is the original number: 50

"9" will be put into the setter via the child class object

Number from base class after update: 50
Number from chile class after update: 9


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


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.
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.
Yeehaw is not the contained object. numInQuestion is.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

class A
{
private:
    static int num ;
public:
    static int getNum() { return num ; }
    static void setNum(int n) { num = n ; }
};

int A::num=50 ;

class B : public A
{
};

int main()
{
    A a1 ;
    A a2 ;
    B b ;

    A::setNum(9) ;

    std::cout << A::getNum() << ' ' << a1.getNum() << ' ' 
              << a2.getNum() << ' ' << b.getNum() << '\n' ;

    a1.setNum(10) ;

    std::cout << A::getNum() << ' ' << a1.getNum() << ' ' 
              << a2.getNum() << ' ' << b.getNum() << '\n' ;

    a2.setNum(34) ;

    
    std::cout << A::getNum() << ' ' << a1.getNum() << ' ' 
              << a2.getNum() << ' ' << b.getNum() << '\n' ;

    b.setNum(324) ;

    
    std::cout << A::getNum() << ' ' << a1.getNum() << ' ' 
              << a2.getNum() << ' ' << b.getNum() << '\n' ;
}
9 9 9 9
10 10 10 10
34 34 34 34
324 324 324 324
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!!!
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.
Thanks
Topic archived. No new replies allowed.