Board index » cppbuilder » Class in multiple files

Class in multiple files


2007-02-22 11:40:33 PM
cppbuilder18
Hi,
Does anyone know if it is possible to use multiple C++ files for one class.
I have a class with a lot of private functions. I would like to put some of
the functions in different C++ files, but only one header file with the
class definition.
How is that done?
Regards,
Morten
 
 

Re:Class in multiple files

"Morten Mosgaard" < XXXX@XXXXX.COM >writes:
Quote
Hi, Does anyone know if it is possible to use multiple C++ files for
one class. I have a class with a lot of private functions. I would
like to put some of the functions in different C++ files, but only
one header file with the class definition. How is that done?
There is nothing special about it.
1) #include the header in any .cpp file that defines members of that
class.
2) Link all those .cpp files into your application.
The only thing that's important to ensure is that you do not define
any member multiple times, or you break the one-definition-rule and
your program will have undefined behavior.
While scattering the implementations throughout your source files is
perfectly ok to do (according to the language/compiler) it does make
it harder for programmers to FIND the definition of the members. But
that's another issue.
--
Chris (TeamB);
 

Re:Class in multiple files

Morten Mosgaard wrote:
Quote
Hi,
Does anyone know if it is possible to use multiple C++ files for
one class. I have a class with a lot of private functions. I would
like to put some of the functions in different C++ files, but only
one header file with the class definition. How is that done?
If you're asking whether the implementation and definition of the
class can be done separately -- of course.
If you're asking about hiding even function definitions -- google
PIMPL
--
liz
 

{smallsort}

Re:Class in multiple files

"Morten Mosgaard" < XXXX@XXXXX.COM >wrote in message
Quote
Does anyone know if it is possible to use multiple C++ files for one
class.
Yes, it is. As long as the compiler can find the implementations, it
doesn't care where they are located.
Quote
I have a class with a lot of private functions. I would like to put
some
of the functions in different C++ files, but only one header file
with
the class definition.
Go right ahead. I have projects that do exactly that, and it works
fine.
Quote
How is that done?
Exactly as you described - one header file with all of the
declarations, and then put the implementations where desired. Simply
include the header in each of the source files, and then make sure
they are all added to the project. For example:
--- MyClass.h ---
#ifndef MyClassH
#define MyClassH
class TMyClass
{
private:
void Proc1();
void Proc2();
void Proc3();
void Proc4();
void Proc5();
//...
};
#endif
--- MySource1.cpp ---
#include "MyClass.h"
void TMyClass::Proc1()
{
//...
}
void TMyClass::Proc2()
{
//...
}
--- MySource2.cpp ---
#include "MyClass.h"
void TMyClass::Proc3()
{
//...
}
--- MySource3.cpp ---
#include "MyClass.h"
void TMyClass::Proc4()
{
//...
}
void TMyClass::Proc5()
{
//...
}
Gambit
 

Re:Class in multiple files

Hi,
Thanks for all the help - it worked.
Regards,
Morten
"Morten Mosgaard" < XXXX@XXXXX.COM >skrev i en meddelelse
Quote
Hi,
Does anyone know if it is possible to use multiple C++ files for one
class. I have a class with a lot of private functions. I would like to put
some of the functions in different C++ files, but only one header file
with the class definition.
How is that done?

Regards,
Morten