0

(I found partial answer to this question here, but question still holds)

In C++ you have this annoying 'feature', where despite defining function outside class definition, you still have to declare it inside class. It has it's pros, but still is really annoying, since more than having transparent code I want to have a lot of auxiliary functions, without need to show them in final API.

I understand that design of object-oriented C++ doesn't allow it (maybe with some crazy pointer-to-function way, which, if you know it, I want to know it too :D ), but I'm interested if there exists any popular programming language with ability to declare/define functions outside the class body. Of course I know other way around (with just keeping every variable public), but it doesn't allow us to operate on existing API without changing read-only headers.

Example of a problem (from Unreal Engine):

FVector a(1,2,3), b(3,4,5);
float c1 = FVector::DotProduct(a,b);
//but...
float c2 = a.Dot(b); 
//method doesn't exist. I have to make new child class inheriting from FVector 
//to actually be able to use simple .Dot method
Community
  • 1
  • 1
Ch3shire
  • 1,095
  • 2
  • 14
  • 39
  • 1
    So you want the main functions of class A in file X and other functions of class A which are not used very often in file Y? In C# this is possible by using partial classes. – Markus Müller Jan 06 '17 at 23:28
  • Good to know! I know C# a little from Unity, but didn't know this feature. Thanks! If you write full answer with C# example I'll accept it. – Ch3shire Jan 06 '17 at 23:30
  • 1
    Python and Ruby have it. Javascript has it, as far as you can say it has classes (even with the new "class" syntactical sugar). – user2357112 Jan 06 '17 at 23:32

2 Answers2

1

In C# a class can be spanned over multiple files. This feature is called partial classes. You can use partial classes to:

  • Work at the same class with multiple devs without having to merge the class file all the time
  • You can add additional code to auto-generated classes which you might want to regenerate at a later time

Sample (taken from MSDN)

public partial class Employee
{
    public void DoWork()
    {
    }
}

public partial class Employee
{
    public void GoToLunch()
    {
    }
}

All parts of the class need to be flagged with the partial keyword. This means that you can only expand those classes that their authors intended to be expanded. This makes sure that the original developer keeps control over what can be done with his class (like abstract, sealed, ...)

Markus Müller
  • 2,611
  • 1
  • 17
  • 25
1

From what I know you can do that in C# (partial classes), Objective-C (extensions + categories), Swift (extensions). Also you can do some sort of workaround in C++, you can define auxiliary class which will contain all functions you don't want to show in public API (don't forget to make it friend class if needed) and just store pointer to instance of this class in your public class. With this approach you even not obliged to include header with auxiliary class interface to your public class header (forward declaration is enough) and can hide it somewhere. You just need that header in your implementation file.

user3237732
  • 1,976
  • 2
  • 21
  • 28