Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uwm.edu!lll-winken!sun-barr!apple!netcom!teda!ditka!mcdchg!tellab5!balr!clrcom!rmartin From: rmartin@clear.com (Bob Martin) Newsgroups: comp.lang.c++ Subject: Re: different member functions with same arg types? Message-ID: <1991Jan21.171120.29536@clear.com> Date: 21 Jan 91 17:11:20 GMT References: Organization: Clear Communications, Inc. Lines: 51 In article montanaro@crdgw1.ge.com (Skip Montanaro) writes: > >I am writing a parser class for a small language. I'd like it to be able to >parse input from strings and files. The natural class interface (for me) is >something like: > > class parser : public ... > { > ... > public: > parse(char *string); > parse(char *filename); > ... > } > >Unfortunately, both strings and filenames are char *'s, so the compiler >can't distinguish the two interfaces. Is it kosher to add a dummy argument >to one member function? It seems crude to me: > > class parser : public ... > { > ... > public: > parse(char *string); > parse(char *filename, unsigned int dummy); > ... > } I agree that this is crude. It is also dangerous since you, or another programmer may forget to use the dummy and accidentaly induce a bug which could be very hard to find. > >The other idea that occurs to me is to use a class to contain either the >string or the filename, but not both: >This seems like overkill, when all I want is the string's value, not all the >other baggage of the String class. > On the contrary, I think this is an excellent solution. A class to hold strings and another class to hold file names is an obvious and compelling separation of abstractions. You could think of many things that you would like to do to filename that you would not like to do to strings: delete, open, close, etc. IMHO you should create these two separate classes. Then the interface to your parser will be very clear and unambiguous. -- +-Robert C. Martin-----+:RRR:::CCC:M:::::M:| Nobody is responsible for | | rmartin@clear.com |:R::R:C::::M:M:M:M:| my words but me. I want | | uunet!clrcom!rmartin |:RRR::C::::M::M::M:| all the credit, and all | +----------------------+:R::R::CCC:M:::::M:| the blame. So there. |