Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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 :).

share|improve this question
add comment

2 Answers

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.

share|improve this answer
add comment

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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