Converting from hpp to h and cpp

Started by
2 comments, last by civguy 21 years, 6 months ago
I got pretty tired of writing both h and cpp files for each and every class. So I thought of writing both definitions and implementations in a hpp file and then converting it to h and cpp with my own precompiler. So I would have MyClass.hpp:
  #include <iostream> //+


class MyClass {
public:
  void hello() const {
    std::cout << "hello" << std::endl;
  }
};  
and after precompiling, it would become MyClass.h:
  #ifndef MYCLASS_H_INCLUDED
#define MYCLASS_H_INCLUDED

class MyClass {
public:
  void hello() const;
};

#endif  
MyClass.cpp:
  #include <iostream>
#include "MyClass.h"

void MyClass::hello() const {
  std::cout << "hello" << std::endl;
}  
(ignore any typos ) The //+ -comment behind iostream means that it's only needed by the implementation. Anyway, does this idea sound reasonable? And has anyone done a program to do this already? It would be a little more like coding Java . Inclusion guards would be added automatically and h+cpp would always be in sync without you having to worry about copying the definitions (void hello() const) from one place to another. [edited by - civguy on September 28, 2002 5:32:28 AM] [edited by - civguy on September 28, 2002 5:33:58 AM]
Advertisement
g++ already has that feature as a #pragma.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on September 28, 2002 5:42:23 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
The .hpp extension is used by Boost instead of .h for header files... it might not be a good idea to give it another meaning if you''re expecting people to catch on. (I wouldn''t be surprised if Boost will be pushing the C++ committee to change from no extension to .hpp... which would of course confuse more people during the transition stage just like .h to extensionless did.
quote:Original post by damienj
The .hpp extension is used by Boost instead of .h for header files... it might not be a good idea to give it another meaning if you''re expecting people to catch on. (I wouldn''t be surprised if Boost will be pushing the C++ committee to change from no extension to .hpp... which would of course confuse more people during the transition stage just like .h to extensionless did.

.hpp (and .hxx) are old and well-known extensions for C++ (only) header files. Boost will not be pushing for the conversion of STL header filess into *.hpp files as it serves absolutely no purpose.

civguy:
First off, I dislike the Java scheme of requiring member function definitions within the class declaration. I like being able to separate declaration and definition when I want to (for example, distributing a statically compiled library and just the header). The core issue to solving the .h and .cpp file redundancy (you don''t need one for every class, as you probably know; you can have multiple definitions and declarations in a variety of files that overlap in terms of functionality) is eliminating the need for forward declarations (a type/object must be declared before it can be used) at global scope. If you can write a preprocessor that does a multipass to extract all declarative data, then fills out definitions and generates a single source file that can be fed to the compiler, you have a winner.

Extra (brownie) points: Make the preprocessor emit warnings when it encounters obvious pre-standard C++ behavior. Have the warning suggest standard alternatives (useful as a teaching tool). Also have it flag certain "risky" behavior (the assignment of string literals to mutable char pointers, for example).

Aw, hell. I''ll write it myself.

This topic is closed to new replies.

Advertisement