I am relatively new to c++. So apologies if this q is naive. But I couldn't get an answer so asking it here.
I have an existing .cpp file (one.cpp) which has a class (class A) defined inside it(one.cpp does not have corresponding one.h). Now I want another class (class B) in another file (two.h) to extend class A. But as A is not inside a header, I cannot do #include one.h
I am thinking of creating one.h, but class A is huge, so trying to avoid it.
Is there an easier way to do it?
In order for the linker to be able to identify class A in two.h, it would need reference to that class through a header file. As far as I know, there's no way around that.
Keep in mind that you only need to put the function prototypes in the header, not the actual definition.
You can include one.cpp in your two.h file like this #include "one.cpp". However this is considered bad practice. What you should do is create a header file for your class A.
Keep in mind that you don't have to actually implement the class in the header file.
Related
I came across a C++ coding pattern that is so weird that I'd really like to know where it comes from. Some classes (base classes, no ancestors) need to share certain functionality. Normally you would implement this as an ancestor class (a template if need be) from which those classes inherit which need the functionality.
Here, the programmer chose to pack the required method and data member declarations into a separate file and include this file in midst of the class declaration. They even #define'd a symbol to the name of the class being defined before the #include line. This created declarations like this:
class CMyStructure
{
public:
CMyStructure();
~CMyStructure();
__int64 m_SomeData;
double m_SomeOtherData;
#define DEFINE_CLASS CMyStructure
#include "include_definitions.h"
#undef DEFINE_CLASS
};
The included code made use of the DEFINE_CLASS symbol.
The same pattern is repeated in the implementation file, where a different file is included which defines some methods.
Does this pattern have a name? Where does it come from?
(A warning to C++ newbies: Don't use this pattern!)
Thanks, Hans
In my class in C++ I want to write a helper function (Which external users can't see or use) and found 2 ways to do that:
1) To declare it as private in the .h file and write the implementation in .cpp file.
2) To write the implementation directly in .cpp file without declaring it in the .h file.
What's the correct way, the difference or the advantage one have on the other?
Since you are not exposing this function through your interface, you should not declare it in the public header file. If you use it in a single cpp file, declare it in an anonymous namespace in that cpp file. If you use this function in multiple cpp files but you would still like to not make it a part of your interface, you can create internal header files that you #include in your source files, but aren't a part of your library's public interface.
The way is to create a class A that would be visible to the public, then create a descendant class B of this class and implement it privately. Then, when you are asked to create an A*, create a B* which is also an A* and return it. Your code sees a B* but the user of the class sees A* and may only access methods and variables declared in A. You also need to create/destroy your pointers with some function rather than with new/delete.
Pretty much like how COM implements IUnknown and stuff.
Due to the implementation of a template class in a header file, I have to include the other includes used in the implementation of the class in the header file.
Because of that, each time I include my template class that bring all other the inclusions and so on.
This could bring to an eccessive spread of inclusion, or even unnecessary for the context.
So what's the best way to deal with this issue?
Edit: Since I've not explictely mentioned it, I'm not talking about cases where forward declaration could solve it like in a normal .h/.cpp separation, but when you have to include the header, and since you don't have the cpp, you are force to use it in .h
Edit 2: Let's say my template class has a function with a dependency with a third class library. Each class using my template class now has the same depency, or can access to that header which both I could not want to. Does it exists a way to avoid that?
Use forward declaration where ever appropriate instead of #include.
Header file should #include only the needed and rest should go in source file.
For now I do not use header files at all. Classes are each in a single .cpp file completely. But to save compile time I want to make use of header files now. My hope is that Visual Studio won't compile classes which weren't modified for debug builds then.
Is there a way to mention only public methods and members in the header file. In theory that would be enough information for the compiler. If another file, say main.cpp includes the class header there is no need for private methods and members, is it?
How can I use header files without retyping the names of private methods and members? The reasons for me to want so is coding productivity. When I want do add a small helper function to the class used by another method, I don't want to have to also add it's signature to the header file.
If another file, say main.cpp includes the class header there is no need for private methods and members, is it?
No, public methods and members aren't necessarily enough. For example, if another .cpp file were to try and create an instance of your class:
SomeClass instance;
the compiler will need to know, among other things, how much memory to allocate for SomeClass. For that it requires full knowledge of SomeClass's private data members.
The way you are framing the question makes it sound as if you were intent on fighting the language. I don't think that's a good way to go about it. I think the best way is to do things the way things are usually done in the language of your choice, and depart from that only when there is a specific, clearly understood need.
The way things are usually done in C++ is that the entire class declaration goes in the header file, and the definition is in some way split between the header file and the corresponding .cpp file. The exact split is determined by various technical considerations. For example, templates and inline functions normally have to appear in the header file. On the other hand, placing code in header files increases dependencies and potentially build times.
There are ways to address these issues. However, since this involves extra complexity, I'd argue that this should only be done if there is a clearly identifiable need.
I don't know of a way to do what you're asking, but there is another way to give some isolation. You might want to take a look at the pimpl idiom as it offers isolation about private information. It's a little bit of extra work, but it can be extremely useful, especially in large projects.
All function declarations should go in header files and all function definitions should go in cpp files. It's not good coding practice to put declarations inside the cpp files.
You could put definitions inside headers though, when you write templates or inline functions.
Once you declare the class in the header file, you have to declare all its methods and members inside the class' declaration in the header, given that your class is no longer declared in the cpp file.
I am completely confused as to the proper way to layout a C++ project.
I had all my classes in separate .cpp files, with their definitions in .h files. I then had one "header.h" which contained all the class headers, external dependencies and a few other things. But I wasn't able to use class names in the header files, where I needed to declare a pointer to one.
Can someone please explain the proper object-orientated layout for a C++ project.
You can fix the problem "wasn't able to use class names in the header files, where I needed to declare a pointer to one" by using forward class declarations, like:
class myClass;
However, having every class include a header.h that then includes every class is overkill. Instead, you should have each class specifically include only the classes and external dependencies that it actually needs.