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

I have header file "myClass.h" with:

class myClass
    // 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 :).


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.


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.

  • Do you think partial classes are always a violation of SRP, then? – Jordan Jun 3 '12 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 Jun 4 '12 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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