Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial class definition on C++?

Tags:

c++

Anyone knows if is possible to have partial class definition on C++ ?

Something like:

file1.h:

   class Test {     public:         int test1(); }; 

file2.h:

 class Test {     public:         int test2(); }; 

For me it seems quite useful for definining multi-platform classes that have common functions between them that are platform-independent because inheritance is a cost to pay that is non-useful for multi-platform classes.

I mean you will never have two multi-platform specialization instances at runtime, only at compile time. Inheritance could be useful to fulfill your public interface needs but after that it won't add anything useful at runtime, just costs.

Also you will have to use an ugly #ifdef to use the class because you can't make an instance from an abstract class:

 class genericTest {     public:         int genericMethod(); }; 

Then let's say for win32:

 class win32Test: public genericTest {     public:         int win32Method(); }; 

And maybe:

 class macTest: public genericTest {     public:         int macMethod(); }; 

Let's think that both win32Method() and macMethod() calls genericMethod(), and you will have to use the class like this:

  #ifdef _WIN32  genericTest *test = new win32Test();  #elif MAC  genericTest *test = new macTest();  #endif   test->genericMethod(); 

Now thinking a while the inheritance was only useful for giving them both a genericMethod() that is dependent on the platform-specific one, but you have the cost of calling two constructors because of that. Also you have ugly #ifdef scattered around the code.

That's why I was looking for partial classes. I could at compile-time define the specific platform dependent partial end, of course that on this silly example I still need an ugly #ifdef inside genericMethod() but there is another ways to avoid that.

like image 315
Edwin Jarvis Avatar asked Sep 26 '08 17:09

Edwin Jarvis


People also ask

What is partial keyword in C#?

The partial keyword indicates that other parts of the class, struct, or interface can be defined in the namespace. All the parts must use the partial keyword. All the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public , private , and so on.

How do you create a partial class?

cs" has user interface control definition. When working with automatically generated source, the code can be added to the class without having to recreate the source file. For example, you are working with LINQ to SQL and create a DBML file. Now when you drag and drop a table it creates a partial class in designer.

What does the partial class allows?

Partial classes are portions of a class that the compiler can combine to form a complete class. Although you could define two or more partial classes within the same file, the general purpose of a partial class is to allow the splitting of a class definition across multiple files.

How do you call a partial class in C#?

It is mandatory to use the partial keyword if we are trying to make a class partial. All the parts of the class should be in the same namespace and available at compile time to form the final type. All the parts must have same access modifier i.e. private, public, or so on.


2 Answers

This is not possible in C++, it will give you an error about redefining already-defined classes. If you'd like to share behavior, consider inheritance.

like image 122
John Millikin Avatar answered Sep 18 '22 13:09

John Millikin


Try inheritance

Specifically

class AllPlatforms { public:     int common(); }; 

and then

class PlatformA : public AllPlatforms { public:     int specific(); }; 
like image 21
Jamie Avatar answered Sep 18 '22 13:09

Jamie