External Acces to Private Slot
Should class A really be able to connect to a private slot in class B?
Here's some pseudo code to illustrate what I mean:
Code:
{
A() {
instanceOfB = new B();
connect(this, SIGNAL(mySignal()), instanceOfB, SLOT(MySlot()));
}
signals:
void mySignal();
private:
B* instanceOfB;
}
{
private slots:
void MySlot();
}
This effectively means that a class B private function can be accessed by class A, which is not really in the spirit of private function access specifier.
Re: External Acces to Private Slot
You can't call the slot directly, but you can call it via the slot mechanism as those are resolved during run time.
Re: External Acces to Private Slot
slots are not a usual functions. they are a part of metaObject. when you emitting signal slot is not called directly like this
Code:
obj->slotName( params );
emitting signal say to object to call it's function like this:
Code:
obj->invokeMethod( "slotName" // you see - in quotes
, params );
Re: External Acces to Private Slot
Maybe as an extra idea:
Code:
connect(A, SIGNAL(foo()), B, SLOT(bar()));
If object A emits signal foo(), then object B should execute function bar().
This system is an implementation of a language level observer pattern, in which object B reacts to something that object A does, rather than object A triggering something in object B.
Re: External Acces to Private Slot
Signal-Slot pattern aim is that the sender DOESN'T KNOW which slots will be called via its signal.
so actually the sender doesn't call the slot, he just emits the signal and the environment deals with it.
Re: External Acces to Private Slot
Maybe you were confused by my response, as it basically states exactly the same as what you just replied with. Eg. that obj->func() will not work from outside the class but obj->InvokeMethod will.
To have your slots truly private, create them in another private class, and contain a pointer to this class in the private part of your public class. So if you need to access your private slots you can do d->slot() whilst people outside your class can not, as 'd' would be private.
This also means your class is more flexible - you can either have data for each instance of your class, or you can share data between instances of your class (like the Qt object classes).
Re: External Acces to Private Slot
I think that Qt should prevent connecting to private slots outside their classes.
Re: External Acces to Private Slot
Slots, by their nature, are public, period. Private slots make no sense; private members are not accessible outside the class they belong to, so signals and slots make no sense, since a direct function call will do just as well in such instances. Slots are inherently public; they are meant explicitly for inter-class communication.
The whole pedantic discussion above would make more sense if protected slots were the topic. For private slots, it's just silly. And the protected case is actually more interesting; here, in order to preserve the traditional C++ meaning of "protected", the metadata system would have to keep track of object inheritance to ensure that only related classes could signal one another - which, again, is somewhat silly since the whole point is inter-object communication, but here it's somewhat less silly than it is when the topic is private slots. In the end, however, one would have to reproduce a signficant chunk of the C++ compiler in order to pull this sort of thing off.
And for what purpose? The goto statement is also considered "bad", but lots of people still use it, and even modern languages like Java support it despite being able to do perfectly well without it through the use of other, "better" constructs. Varying degrees of access protection are useful, but insisting on strict adherence to this paradigm in a signalling system is pointless. Anyone concerned about this is, presumably, a programmer. By implication, that means you should be aware of the potential pitfalls, and should simply avoid them.
Re: External Acces to Private Slot
Why? What practical problem are you trying to solve through such a constraint? And why are you using slots within a class to begin with? There's no point in using a signalling mechanism inside a single class; you've already got access to plain, old-fashioned function calls.
Slots are inherently public, by their very nature; see above.
Re: External Acces to Private Slot
Quote:
Originally Posted by
ahmdsd_ostora
I think that Qt should prevent connecting to private slots outside their classes.
Considering how the connect() statement works, this would be impossible to do at compile time, and we know that a LOT of people simply ignore the standard output, so wouldn't see the error message about connecting to a private slot at runtime. The end result would just be confusion: "Why isn't my slot being called?". We have enough of this questions on this board already, we don't need more by making the system even more restrictive. If you really want private slots there is a way of doing it by using a private class (see above), else the documentation for your class structure would clearly indicate that the slot you are trying to use is private, and surely you don't blindly bind signals to slot without checking the docs first?
Re: External Acces to Private Slot
There is a nice button on this forum under every post called "Multi-Quote This Message". Please use it next time instead of posting multiple posts.
As for the discussion: remember that access protection is done purely during compilation based on a very simple rule, there is no actual check made (run-time) whether one can call a particular method or not hence enforcing such check during run-time for Qt slots would only create overhead without any practical benefits. Many languages (like Python) have simply ignored access protection and treat all methods and fields as public. Working around protection in C++ requires 4-5 lines of code so if someone wants to have access to a private or protected member of some class, there is practically no way of stopping him. On pure academic level I can understand that Qt should support access protection to its signals and slots but as an engineer I say there is no practical argument to support this claim.
Quote:
but how else could class A respond to QTimer::timeout() other than with a slot?
Through events.