Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!cai!davel From: davel@cai.uucp (David W. Lauderback) Newsgroups: comp.lang.c++ Subject: Re: different member functions with same arg types? Message-ID: <1991Jan24.045312.10886@cai.uucp> Date: 24 Jan 91 04:53:12 GMT References: <42908@ut-emx.uucp> Reply-To: davel@cai.UUCP (David W. Lauderback) Organization: Century Analysis Incorporated Lines: 79 In article <42908@ut-emx.uucp> jamshid@ut-emx.uucp (Jamshid Afshar) writes: >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); >> ... >> } > > >> >>I'm open to suggestions for alternate interfaces. >> >>Skip (montanaro@crdgw1.ge.com) > >This may be overkill for what you want, but why don't you make an >abstract class, say SourceCode, that has pure virtual functions >for everything that a parser will want to do with some source code. > >class SourceCode { >public: > virtual void init() = 0; > virtual char getChar() = 0; > virtual void putBackChar(char) = 0; > virtual void skipWhiteSpace() = 0; > virtual bool isAtEnd() = 0; > virtual void close() = 0; > ... >}; > >Then derive new classes, 'SourceFromFile' and 'SourceFromString', from >SourceCode and implement those virtual functions however they need to > more suff... > >Jamshid Afshar >jamshid@emx.utexas.edu I have not use C++ very much and do not have easy access to a C++ compiler to test this out but could Skip use something like the following code to solve his problem? typedef char * FileName; class parser : public ... { ... public: parse(FileName filename); parse(char *string); ... } char *test_string = "Test string"; FileName test_file = (FileName) "TestFile"; func() { class parser x; ... x.parse("Test string"); x.parse((FileName) "TestFile"); x.parse(test_string); x.parse(test_file); } Or would C++ not know the difference between FileName and char *, since there both really the same thing. In any case, FileName could be made a class, instead of just a typedef, to resolve the function selection problem. Am I right here or way out in left-field? -- David W. Lauderback (a.k.a. uunet!cai!davel) Century Analysis Incorporated Disclaimer: Any relationship between my opinions and my employer's opinions is purely accidental.