1

Is it possible define methods from the same class in different "CPP" files?


I have header file "myClass.h" with:

class myClass
{
public:
    // methods for counting
    ...
    // methods for other
    ...
};

I would like to define "methods for counting" in one CPP and "methods for other" in other CPP. For clarity.

Both groups of methods sometime use the same attributes.

Is it possible? Thanks :).

2 Answers 2

3

Really stop and think before you do this as your question is one of the red flags for needing to break up a class into multiple classes.

Also I don't really see how much 'clarity' can be gained by placing methods in two separate files. In most IDEs it is a simple matter to look through the methods list while in a cpp file and find the appropriate method alphabetically.

1

Yes, methods can be defined in any TU you care to define them in and do not need to be grouped together. However, this smells like you are violating SRP.

2
  • Do you think partial classes are always a violation of SRP, then?
    – Jordan
    Commented Jun 3, 2012 at 22:13
  • @Jordan: No language feature can ever violate SRP. It's the use of a language feature, and I'd only judge that on a case by case basis. Partial classes are, afaik, mostly used when the code is partially autogenerated, not because the class's methods serve two responsibilities.
    – DeadMG
    Commented Jun 4, 2012 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.