开发者

Is it possible to define a class in 2 or more file in C++?

开发者 https://www.devze.com 2023-02-03 20:07 出处:网络
I know it\'s possible to do class implementation in more than one file(yes, I know that this is bad idea), but I want to know if it\'s possible to write class definition in separate files without gett

I know it's possible to do class implementation in more than one file(yes, I know that this is bad idea), but I want to know if it's possible to write class definition in separate files without getting a redefi开发者_如何学编程nition error (maybe some tricks, or else...)


No, not the same class, but why would you?

You could define two classes with the same name in the same namespace (the same signature! That will actually be the same class at all, just defined in two ways) in two different header files and compile your program, if your source files don't include both headers. Your application could even be linked, but then at runtime things won't work as you expect (it'll be undefined behavior!), unless the classes are identical.

See Charles Bailey's answer for a more formal explanation.


EDIT:

If what you want is to have a single class defined by more than one file, in order to (for example) add some automatically generated functions to your class, you could #include a secondary file in the middle of your class.

/* automatically generated file - autofile.gen.h */
void f1( ) { /* ... */ }
void f2( ) { /* ... */ }
void f3( ) { /* ... */ }
/* ... */

/* your header file with your class to be expanded */
class C
{
    /* ... */
    #include "autofile.gen.h"
    /* ... */
};


Not as nice and clean as the partial keyword in C#, but yes, you can split your class into multiple header files:

class MyClass
{
#include "my_class_aspect1.h"
#include "my_class_aspect2.h"
#include "my_class_aspect3.h"
};

#include "my_class_aspect1.inl"
#include "my_class_aspect2.inl"
#include "my_class_aspect3.inl"


Well sort of ...

If you have 1 header file as follows

ClassDef1.h:

class ClassDef
{
protected:
   // blah, etc.

public:
   // more blah

and another as follows

ClassDef2.h:

public:
    // Yet more blah.
};

The class will effectively have been defined across 2 files.

Beyond that sort of trickery .. AFAIK, no you can't.


Yes, you can. Each definition must occur in a separate translation unit but there are heavy restrictions on multiple definitions.

Each definition must consist of the same sequence of tokens and in each definition corresponding names must refer to the same entity (or an entity within the definition of the class itself).

See 3.2 [basic.def.odr] / 5 of ISO 14882:2003 for full details.


Yes. Usually, people put the declaration part in .h file and then put the function body implementations in .cpp file Put the declarations in a .h file, then implement in as many files as you want, but include the .h in each.

But, you cannot implement the same thing in more than one file


I know this is a late post, but a friend just asked me a similar question, so I thought I would post our discussion:

From what I understand you want to do ThinkingStiff, it's not possible in C++. I take it you want something like "partial class" in C#.

Keep in mind, "partial class" in .NET is necessary so you don't have to put your entire implementation in one big source file. Unlike C++, .NET does not allow your implementation to be separate from the declaration, therefore the need for "partial class". So, Rafid it's not a nice feature, it's a necessary one, and a reinvention of the wheel IMHO.

If your class definition is so large you need to split it across more than one source file, then it can probably be split into smaller, decoupled classes and the primary class can use the mediator design pattern that ties them all together.

If your desire is to hide private members, such as when developing a commercial library and you don't wish to give away intelectual property (or hints to that affect), then you can use pure abstract classes. This way, all that is in the header is simple class with all the public definitions (API) and a factory function for creating an instance of it. All the implementation details are in your source file(s).

Another alternative is similar to the above, but uses a normal class (not a pure virtual one), but still exposes only the public interface. In your source file(s), you do all the work inside a nameless namespace, which can include friend functions from your class definition when necessary, etc. A bit more work, but a reasonable technique.

I bolded those things you might want to lookup and consider when solving your problem.


This is one of the nice features of C#, but unfortunately it is not supported in C++.


Perhaps something like policy based design would do the trick? Multiple Inheritience gives you the possiblity to compose classes in ways you can't in other languages. of course there are gotchas associated with MI so make sure you know what you are doing.


The short answer is yes, it is possible. The correct answer is no, you should never even think about doing it (and you will be flogged by other programmers should you disregard that directive).


You can split the definitions of the member functions into other translation units, but not of the class itself. To do that, you'd need to do preprocessor trickery.

// header file with function declarations
class C {
public:
    void f(int foo, int bar);
    int g();
};

// this goes into a seperate file

void C::f(int foo, int bar) {
    // code ...
}

// this in yet another
void C::g() {
    // code ...
}


There must be a reason why you want to split between two headers, so my guess is that you really want 2 classes that are joined together to form components of a single (3rd) class.

0

精彩评论

暂无评论...
验证码 换一张
取 消