|
Prev: Multi key Maps
Next: random number [0, 99]
From: Davin Pearson on 6 Feb 2006 08:44 ////////////////////////////////////////////////////////////////////// /// /// Java is elegant how it method declarations and method definitions /// are merged into one concept, and consequentally there is no need /// for header files. /// /// Wouldn't it be good if a C++ preprocessor could be written that /// takes a C++ source file and splits it into a header file and a /// footer file. /// /// That would save the hassle of mantaining separate header and footer /// files for the same class. /// /// Suppose our class is called "foo" then, the preprocessor could take /// as input a file foo.ch and as output generate files called foo.hh /// and foo.cc. /// ////////////////////////////////////////////////////////////////////// /// /// Here is the input file to the preprocessor foo.ch /// class Foo { private: B* b; C* c; public: void bar() { std::cout << "Welcome to Foo::bar!\n"; } void wibble() { std::cout << "Welcome to Foo::wibble!\n"; } }; ////////////////////////////////////////////////////////////////////// /// /// Here is the first output file from the preprocessor foo.hh /// class A { private: B* b; C* c; public: void foo(); void bar(); }; ////////////////////////////////////////////////////////////////////// /// /// Here is the second output file from the preprocessor foo.cc /// void Foo::bar() { std::cout << "Welcome to Foo::bar!\n"; } void Foo::wibble() { std::cout << "Welcome to Foo::wibble!\n"; } /// /// Some cleverness could minimise the amount /// of recompilation needed by only changing /// the header file if a geniune change has /// been made to it, /// /// /// I realise the C/C++ preprocessor makes /// this task difficult so we might outlaw /// C/C++ preprocessor commands in a *.ch file. /// For the purposes of debugging, it will /// be useful to set the __FILE__ and __LINE__ /// variables so that they correspond with /// the *.ch file. /// /// /// Some additional cleverness would be /// necessary to make inline methods work. /// [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Alberto Ganesh Barbati on 6 Feb 2006 17:28 Davin Pearson ha scritto: > /// Wouldn't it be good if a C++ preprocessor could be written that > /// takes a C++ source file and splits it into a header file and a > /// footer file. If you say so. I like having interface and implementation split into two files. I have a project (not written by myself) with cpp of files 6000+ lines... what a mess it would be if I didn't have a smaller file with the implementation only. > /// That would save the hassle of mantaining separate header and footer > /// files for the same class. That looks a pretty thin rationale for introducing a feature that is going to be quite difficult to implement. At least three things are going to be problematic: inline functions, templates and the preprocessor (that you have already ruled out, however). Moreover it's not very clear to me how you could approach the recompilation issue: I'd expect either a big impact on compilation times or some kind of cache system that might be a worse nightmare than pre-compiled headers. Just my opinion, Ganesh [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ivan Vecerina on 6 Feb 2006 17:30 "Davin Pearson" <davin.pearson(a)gmail.com> wrote in message news:1139207775.443643.195610(a)g44g2000cwa.googlegroups.com... : ////////////////////////////////////////////////////////////////////// : /// : /// Java is elegant how it method declarations and method definitions : /// are merged into one concept, and consequentally there is no need : /// for header files. : /// : /// Wouldn't it be good if a C++ preprocessor could be written that : /// takes a C++ source file and splits it into a header file and a : /// footer file. I think that others have already thought of this: http://www.google.com/search?q=C%2B%2B+preprocessor+source+header+single+fileThose might not do everything you want (ie. using #line directivesto redirect the debugger), and some will have limitations, but you'llprobably find something that comes relatively close.Some compilers (gcc) also support pragma-style delimiters todelimit interface and header sections within a single sourcefile (many developers like to keep implementation detailsout of the 'interface', more like Object Pascal than like Java).Ivan--http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ivan Kolev on 6 Feb 2006 18:37 C++ headers are actually a very good thing. They visually separate interface from implementation. It's often much better to read just headers. Documentation (doxygen style or not) usually goes there too, even more emphasizing the role of headers as interface specification. I have a C++ coding rule which forbids placing implementations of inline methods inside the class definition in the header file. Any inline method implementations should be placed in a separate .inl file. [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ted on 6 Feb 2006 18:41
"Davin Pearson" <davin.pearson(a)gmail.com> wrote in message news:1139207775.443643.195610(a)g44g2000cwa.googlegroups.com... > ////////////////////////////////////////////////////////////////////// > /// > /// Java is elegant how it method declarations and method definitions > /// are merged into one concept, and consequentally there is no need > /// for header files. > /// > /// Wouldn't it be good if a C++ preprocessor could be written that > /// takes a C++ source file and splits it into a header file and a > /// footer file. > /// > /// That would save the hassle of mantaining separate header and footer > /// files for the same class. I'd still want to break out the declaration of the class from the definition because that is the best documentation of the class that is easiest to comprehend. It gives a mile-high view of the class and when I'm designing-by-coding, the headers are the "design tool". So I always want to write the header-type stuff. It may be nice to have some directive in the header to say "process the following as if it were in a .cpp file" though: #ifndef A_H_ #define A_H_ class A { private: B* b; C* c; public: void foo(); void bar(); }; // stuff to be processes as if it were in a .cpp file // #definitions void Foo::bar() { std::cout << "Welcome to Foo::bar!\n"; } void Foo::wibble() { std::cout << "Welcome to Foo::wibble!\n"; } #enddefs #endif // hdr The above loses almost all worth when the goal is to hide the implemenation though. Ted [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |