Classes and Using them

Hello,

I'm having some problems programming some classes in c++. Basicly I have this in my main code:
1
2
PlayerClient robot("localhost");
	Position2dProxy p2dProxy(&robot, 0);


So we have 2 classes, PlayerClient that holds alot of member variables and basic communication, and also a Position2dProxy that has helper function for the PlayerClient class. Now I want that the robot object is made once and then passed by reference to the other class so when the helper class changes values they also happen in the robot object.

This is my PlayerClient constructor
1
2
3
4
5
6
PlayerClient::PlayerClient(char* dummy) {
	HardwareSerial* serial = &Serial;
	_serial = serial;
	_baud = 57600;
...
}


This is the constructor for the Position2dProxy
1
2
3
Position2dProxy::Position2dProxy(PlayerClient* robot, uint32_t aIndex) {
	_robot = *robot;
}


And then the function for the operator overloading
1
2
3
4
5
6
7
PlayerClient& PlayerClient::operator =(const PlayerClient& PC) {
	_serial = PC._serial;
	_baud = PC._baud;
	_pollState = PollStateIdle;
...
	return (*this);
}


And it doesn't work, instead it just makes an empty object of robot and put the changes in that one, any help? Really can't find the error :p

Thanks
Have you debugged operator=() to make sure it is being called and that it is setting the member variables properly?
You can use singleton class as it only creats one object .
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
51
52
53
54
55
#include <iostream>


using namespace std;

class Singleton
{
private:
    static bool instanceFlag;
    static Singleton *single;
    Singleton()
    {
        //private constructor

    }
public:
    static Singleton* getInstance();
    void method();
    ~Singleton()
    {
        instanceFlag = false;
    }
};

bool Singleton::instanceFlag = false;
Singleton* Singleton::single = NULL;
Singleton* Singleton::getInstance()
{
    if(! instanceFlag)
    {
        single = new Singleton();
        instanceFlag = true;
        return single;
    }
    else
    {
        return single;
    }
}

void Singleton::method()
{
    cout << "Method of the singleton class" << endl;
}

int main()
{
    Singleton *sc1,*sc2;
    sc1 = Singleton::getInstance();
    sc1->method();
    sc2 = Singleton::getInstance();
    sc2->method();

    return 0;
}
Topic archived. No new replies allowed.