By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,889 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

Split a class across two source files

P: n/a
Hi, I have a class that is fairly large and I would like to split the
file into two files, but still use only one class. What is the best way
to accomplish this?

Thanks!

Dec 7 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
je************@gmail.com wrote:
Hi, I have a class that is fairly large and I would like to split the
file into two files, but still use only one class. What is the best way
to accomplish this?

Thanks!


A "fairly large" class may indicate that the class should be
redesigned, breaking it into smaller classes. However, you can declare
the class in a header file (say, MyClass.hpp) and then put the
implementation in two source files that both include the header (say,
MyClass1.cpp and MyClass2.cpp):

// In MyClass.hpp
class MyClass
{
void Foo();
void Bar();
};

// In MyClass1.cpp
#include "MyClass.hpp"
void MyClass::Foo()
{ /*...*/ }

// In MyClass2.cpp
#include "MyClass.hpp"
void MyClass::Bar()
{ /*...*/ }

Cheers! --M

Dec 7 '05 #2

P: n/a
Thanks... I feel silly for asking now, it was so simple. I had been
doing the same thing, only I had been including the second cpp file
from the first. Don't know what possessed me to try that, but it was
throwing the compiler for a loop.

Dec 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.