Split a class across two source files

J

jeremy.figgins

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!
 
M

mlimber

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
 
J

jeremy.figgins

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top