Accessing a class Object by pointer in another class
Hi All,
I recognize that this question falls pretty much into C++, but since I'm using the threaded server example to develop a server, please bare with me... ;)
As to my question:
the server class is defined as followed:
Code:
{
Q_OBJECT
public:
unsigned int nbrClients;
protected:
void incomingConnection ( int socketDescriptor );
};
in myunit.cpp the constructor:
myUnit ( int socketDescriptor, QObject *parent );
in server.cpp I initiate the object like this:
myUnit *munit = new myUnit ( socketDescriptor,this );
in myunit.cpp I have a member function that gets fired upon the socket disconnected state by this:
connect ( &tcpSocket, SIGNAL ( disconnected() ), this, SLOT ( disconnected() ) );
this is the member function:
void myUnit::disconnected()
{
//server::nbrClients--; provided for clarity of purpose... ;)
this->quit();
}
What I need is to access the nbrClients member inside this function as to decrement it upon socket disconnection...
I'll appreciate any pointers on how to do so.
Thanks in advance.
Pedro Doria Meunier.
Re: Accessing a class Object by pointer in another class
I don't understand the (syntactical) problem, why can't you access it via a pointer as your subject states?
But I would say that would be a wrong way of doing it (design-wise) since you are decreasing the servers member in another class.
It would be better to connect a slot that belongs to the server to the disconnected() signal, and do it there.
Re: Accessing a class Object by pointer in another class
Thanks.
I actually already solved it. It turned out that this confirms my C++ 'newbieness' :o
What I did was replacing the QObject *parent with Server *parent ...
That way I have the parent server object and can access its members... ;)
Kind regards,
Pedro Doria Meunier.