Re: [Ctool-develop] using ctool as a library
Brought to you by:
flisakow
From: Steven S. <ste...@cs...> - 2003-08-01 18:49:21
|
Stefan Seefeld wrote: > Steven Singer wrote: >> Can I ask why all the functions have different names? Wouldn't it be >> more natural to give them all the same name and then overload on the >> argument type? > > mainly a matter of taste I guess. It's easier to distinguish. The single > most important point is if you derive a new Traversal. If you use the > same name for all methods, you have to redefine all of them (or specify > 'using Base::traverse') or the compiler will complain. I think this depends what you're doing. For example, this works fine: #include <iostream> class V { public: virtual void operator()(int) = 0; virtual void operator()(double) = 0; virtual void operator()(char *) = 0; }; class V1 : public V { public: virtual void operator()(int) { std::cout << "A"; } virtual void operator()(double) { std::cout << "B"; } virtual void operator()(char *) { std::cout << "C"; } }; class V2 : public V1 { public: virtual void operator()(char *) { std::cout << "D"; } }; class Obj { public: void accept(V &v) { v(1); } }; int main() { Obj ob; V2 v_instance; ob.accept(v_instance); std::cout << std::endl; return 0; } This works because all the accept routines have a reference to a V, not to the specific subtype. Provided you're always going through V then it's OK. However, the real killer is if you need to inherit from a class over which you have no control. So if V1 and V2 were in a library and I wanted to write: class V3 : public V2 { public: virtual void operator()(int i) { std::cout << "E"; ????::operator()(i); } }; then it's difficult to know in advance whether I need to put V1 or V2 in place of the ???? to pass control up to the base class. Of course, I could solve this by delegation: class V3 : public V2 { public: virtual void operator()(int i) { std::cout << "E"; ((V &) v2)(i); } private: V2 v2; }; But that's getting needlessly complicated. So I think your initial objection is valid. Different names make inheritance easier. >> If they all have the same name then instead of calling it traverse, >> it could be operator(). Accept code could then look like: > > yes it could. But I find it much clearer to spell out what it is doing. > I find 'operator ()' a little too compact. OK, that's a completely fair comment. To much use of terse coding tricks can make things illegible. - Steven -- ********************************************************************** The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer. ********************************************************************** |