Say i have the following class :
class Abc {
int id;
public:
int getID() { return id; }
int setID(int id) { this->id = id; }
};
Is there any logical error in this ? I seem to be getting unexpected results (read : wrong values of id). I know this is not the way to write a getter .. but still there shudn’t be any error in this code ?
Here is the class declaration :
class ClientConn {
static int num;
short pos;
sockaddr_in tcpAddress;
sockaddr_in udpAddress;
int connFD;
public:
ClientConn();
int getConnFD();
void setConnFD(int connFD);
void setPos(short pos);
short const& getPos();
void setUdpAddress(short port);
void setTcpAddress(sockaddr_in address);
void setUdpAddress(sockaddr_in address);
void setTcpAddress(short port, char* serverIP);
void setUdpAddress(short port, char * serverIP);
sockaddr_in const& getTcpAddress() const;
sockaddr_in const& getUdpAddress() const;
};
the two functions had been defined as follows :
int ClientConn :: getConnFD() {
return connFD;
}
void ClientConn :: setConnFD(int connFD) {
this->connFD = connFD;
}
I had set the value of connFD to 7 using the setter, and then when i was using the getter, i was getting the value 65534.
(Should i answer my question or keep editing my post ? im new)
A few notes:
EDIT: Now that you posted your code, I would assume that something is stomping your variables. What compiler are you using? A memory breakpoint would be the fastest way to tell you what’s going on. Assuming that’s not an option, sprinkle your code with debug output that shows the current value of the variable and do a divide and conquer until you find out where it gets stomped.
Also, the new code you posted still doesn’t demonstrate any actual usage. A simple test program would help.