Splitting a huge class to multiple files?

S

Solang

Hi,

I just start learning C++ and have no exeperience with class. I want to
port my C program to C++.
There are too many member functions and I want to split them into different
files.
What is the simplest method to do it?

Thanks.
 
L

Lionel B

Hi,

I just start learning C++ and have no exeperience with class. I want
to
port my C program to C++.
There are too many member functions and I want to split them into
different files.
What is the simplest method to do it?

You'll almost certainly want to keep all the function *declarations* in a
single header file[*], since this represents the (programatic) interface
to your class functionality. There is nothing stopping you, however, from
splitting member function *definitions* (i.e. implementations) across
several files.

That said, if your class is so huge that this is really an issue, that
suggests to me that you might want to re-think your design.

[*] in fact I know no way of splitting a class declaration across files,
except using some #include and/or other macro ugliness
 
V

Victor Bazarov

Solang said:
I just start learning C++ and have no exeperience with class. I want to
port my C program to C++.
There are too many member functions and I want to split them into
different files.
What is the simplest method to do it?

There are two answers to your inquiry. One is "just do it". Put the
class definition in a header, put the implementation in as many source
files as you like, make sure all files are included in your program,
i.e. compiled and the object modules are linked in. Nothing to it, really.

The other answer is, "review your design". If your class has "too many
member functions", then it's most likely too complex for your own
comprehension, let alone somebody else's come time to maintain it (and
that's immediately after you finish typing it in and compiled it for the
first time). So, you will do yourself and others a huge favor if you
review the design of that class and split it into smaller logical
entities. Once you identified the domains in which each part serves as
the center, you can then combine them by inheriting from all of them
(see policy design paradigm), or by containing them in your class.
Examples of that are too numerous to mention.

Here is one thing you should do, pretty much immediately: find yourself
a good book on C++ design. I recommend works by James Coplien, they
helped me quite a bit. "Modern C++ Design" by Alexandrescu is good, but
can be too "out there" for a beginner. There are probably others, and I
am sure somebody is going to recommend something outstanding.

I can only add at this point: follow the KISS principle (Keep It Simple,
Silly). The tighter the functionality is bundled in each of your types,
the more flexible you can make your system by combining those. Think
Lego: you can make almost anything from a few dozen different blocks by
combining different quantities of them in different ways. Yet each of
them stays extremely simple by itself.

Good luck!

V
 
V

Victor Bazarov

Lionel said:
Hi,

I just start learning C++ and have no exeperience with class. I want
to
port my C program to C++.
There are too many member functions and I want to split them into
different files.
What is the simplest method to do it?

You'll almost certainly want to keep all the function *declarations* in a
single header file[*], since this represents the (programatic) interface
to your class functionality. There is nothing stopping you, however, from
splitting member function *definitions* (i.e. implementations) across
several files.

That said, if your class is so huge that this is really an issue, that
suggests to me that you might want to re-think your design.

[*] in fact I know no way of splitting a class declaration across files,

Actually, it is the class *definition* that cannot be split. Just
making sure we all use the right terms.
except using some #include and/or other macro ugliness

V
 
O

osmium

Solang said:
I just start learning C++ and have no exeperience with class. I want to
port my C program to C++.
There are too many member functions and I want to split them into
different files.
What is the simplest method to do it?

Do you know how to break a C program into pieces? If not, do that first so
you can get the mechanics straight for *your* compiler. If you have an IDE
oriented compiler, the word "project" is often a key.
 
S

Solang

I have 5 working C programs and each program have about 40-50 files.
C is easy mainly because of global variables and I have a bunch of typedef
structs.
I use makefile,emacs as editor and XVT design to make all the windows and
dialogs.
 
O

osmium

Solang said:
I have 5 working C programs and each program have about 40-50 files.
C is easy mainly because of global variables and I have a bunch of typedef
structs.
I use makefile,emacs as editor and XVT design to make all the windows and
dialogs.

Your mention of how easy things are in C due to global variables make me
wonder if you take some shortcuts usually frowned on. .

If that is not the case, just remember that "class" introduces a new user
defined type and go to it. Consolidate the class definitions in one .header
file and include it in all the other globs of code. Treat class the same
way you would treat struct in a *properly organized* C program. The actual
function definitions alluded to in the class are just that, ordinary
functions, and they can be compiled.by themselves as long as they can see
the class definition.

If you personally wrote template code, all bets are off, since "export" is
mostly a dream.
 
S

Solang

You are right that I wrote those C programs by myself and if I can use
struct in C++,
I can reuse all the C functions without too much editing. I plan indeed take
a short cut to port those program to C++.
Maybe after I finish porting these programs, I will spend more time to study
class.
Also, it is easy for me to write those struct to file and read them back. I
am not
sure at this moment about write class to file and read them back. Is it
possible?
 
O

osmium

Solang said:
You are right that I wrote those C programs by myself and if I can use
struct in C++,
I can reuse all the C functions without too much editing. I plan indeed
take a short cut to port those program to C++.
Maybe after I finish porting these programs, I will spend more time to
study class.
Also, it is easy for me to write those struct to file and read them back.
I am not
sure at this moment about write class to file and read them back. Is it
possible?

You can write and read the data members of a class using the same techniques
that work for data members of a strcut. The only additional requirement is
that in the case of a class, the function doing the writing or reading must
have permission to access those members. This is usually done by making
such functions a member of the class.

The change is due to the fact that a struct defaults to "public" and a class
defaults to "private"
 
J

James Kanze

Lionel B wrote:

[...]
[*] in fact I know no way of splitting a class declaration
across files,
Actually, it is the class *definition* that cannot be split.
Just making sure we all use the right terms.

Well, the declaration can't be split either:). And
technically, a definition is a declaration, although the reverse
is not true. And of course...

All you need to do is require the user to include the files in
the proper order, with no other intervening includes. (*NOT*
that I'm actually suggesting this, of course.)
 
D

DerTopper

Solang said:
Hi,

I just start learning C++ and have no exeperience with class. I want to
port my C program to C++.

Just out of curiousity: Why do want to do so? What are you expecting
to gain?
There are too many member functions and I want to split them into different
files.
What is the simplest method to do it?

Splitting functions into separate compilation units is just the same
under C++ as under C. I don't understand why you use the term "member
functions": This term has a very special meaning under C++, and since
you said that your original code is C I'd advise you not to to use
this term when you talk in a C++ newsgroup about C source code.

Regards,
Stuart
 
V

Victor Bazarov

James said:
[...]"Modern C++ Design" by Alexandrescu is good,
[...] follow the KISS principle (Keep It Simple,
Silly).

Why do I sense a contradiction:)?

Hehe... Einstein said, "Make everything as simple as possible, but not
simpler". No contradiction.

V
 
V

Victor Bazarov

Just out of curiousity: Why do want to do so? What are you expecting
to gain?


Splitting functions into separate compilation units is just the same
under C++ as under C. I don't understand why you use the term "member
functions": This term has a very special meaning under C++, and since
you said that your original code is C I'd advise you not to to use
this term when you talk in a C++ newsgroup about C source code.

Hm... Those questions could be eliminated with a simple assumption that
the OP is rewriting his C program to wrap it in a class (or classes).
And the expected gain is experience (in certain C++ concepts, like
classes, member functions, the possibility to have them separate from
the class definition, or even in separate translation units). Am I
completely off here? Perhaps I'm guessing [a bit], but most of the
information is already available in the original post...

V
 
D

DerTopper

Victor said:
Just out of curiousity: Why do want to do so? What are you expecting
to gain?


Splitting functions into separate compilation units is just the same
under C++ as under C. I don't understand why you use the term "member
functions": This term has a very special meaning under C++, and since
you said that your original code is C I'd advise you not to to use
this term when you talk in a C++ newsgroup about C source code.

Hm... Those questions could be eliminated with a simple assumption that
the OP is rewriting his C program to wrap it in a class (or classes).
And the expected gain is experience (in certain C++ concepts, like
classes, member functions, the possibility to have them separate from
the class definition, or even in separate translation units). Am I
completely off here? Perhaps I'm guessing [a bit], but most of the
information is already available in the original post...

I can't give you the exact reference to his post, its one of his
answers to osmium. OP wrote
"Maybe after I finish porting these programs, I will spend more time
to study
class. "

This brought me to believe that he doesn't actually want to re-design
his application but actually just wants to split his source files.

Stuart
 
V

Victor Bazarov

Victor said:
Solang schrieb:
Hi,

I just start learning C++ and have no exeperience with class. I want to
port my C program to C++.
Just out of curiousity: Why do want to do so? What are you expecting
to gain?

There are too many member functions and I want to split them into different
files.
What is the simplest method to do it?
Splitting functions into separate compilation units is just the same
under C++ as under C. I don't understand why you use the term "member
functions": This term has a very special meaning under C++, and since
you said that your original code is C I'd advise you not to to use
this term when you talk in a C++ newsgroup about C source code.
Hm... Those questions could be eliminated with a simple assumption that
the OP is rewriting his C program to wrap it in a class (or classes).
And the expected gain is experience (in certain C++ concepts, like
classes, member functions, the possibility to have them separate from
the class definition, or even in separate translation units). Am I
completely off here? Perhaps I'm guessing [a bit], but most of the
information is already available in the original post...

I can't give you the exact reference to his post, its one of his
answers to osmium. OP wrote
"Maybe after I finish porting these programs, I will spend more time
to study
class. "

This brought me to believe that he doesn't actually want to re-design
his application but actually just wants to split his source files.

I am a bit confused, I guess. So you're using a different post (where
the OP's opinion about how to proceed converting a C program to C++ may
have changed) as the basis to question the motivation and form of the
original message, right? In that other post, the OP does not use the
term "member". Perhaps we need to ask the OP to re-state the problem
(if there is one still remaining)...

V
 
S

Solang

The reason I want to port my applications using C++ because my old C
PTK(portable tool kit from XVT) didn't have sound and socket. What my old
PTK dosen't have, I was forced to use MS SDK. Now that I've found Clanlib
C++ which
has GUI,graphic,sound and socket, I am trying to port my applications to run
on Linux as well.

Instead of re-design the applications, I am planning to re-use my c codes in
old applications as much as I can. I have the feeling
that by doing this way, I can speed up the process a little bit faster. By
learning C++ and Clanlip SDK at the same time, I can get all
the GUI and drawing in less than two weeks.

Victor said:
Solang schrieb:
Hi,

I just start learning C++ and have no exeperience with class. I want
to
port my C program to C++.

Just out of curiousity: Why do want to do so? What are you expecting
to gain?

There are too many member functions and I want to split them into
different
files.
What is the simplest method to do it?

Splitting functions into separate compilation units is just the same
under C++ as under C. I don't understand why you use the term "member
functions": This term has a very special meaning under C++, and since
you said that your original code is C I'd advise you not to to use
this term when you talk in a C++ newsgroup about C source code.

Hm... Those questions could be eliminated with a simple assumption that
the OP is rewriting his C program to wrap it in a class (or classes).
And the expected gain is experience (in certain C++ concepts, like
classes, member functions, the possibility to have them separate from
the class definition, or even in separate translation units). Am I
completely off here? Perhaps I'm guessing [a bit], but most of the
information is already available in the original post...

I can't give you the exact reference to his post, its one of his
answers to osmium. OP wrote
"Maybe after I finish porting these programs, I will spend more time
to study
class. "

This brought me to believe that he doesn't actually want to re-design
his application but actually just wants to split his source files.

Stuart
 
D

Daniel Pitts

Solang said:
Hi,

I just start learning C++ and have no exeperience with class. I want to
port my C program to C++.
There are too many member functions and I want to split them into
different files.
What is the simplest method to do it?

Thanks.
The simplest method is to actually decompose your program into many classes.

A class should encapsulate a single concept. If you try to find the
simplest concepts within your application, and make each of them a
class, your on the right track.

Some would argue that it is a good idea to even making a concept which
is as simple asas "Distance" its own class (as opposed using "double
meters" some places).
 
S

Solang

One of my applicatins is a small version of paint with additional features
such as macro,sound and
multiple choices, true/false questions. So, I created quite a few structs
for each object such as line,
polylines, circle arc, image, macro etc... Perhaps, I can start using
classes for this one. Should I
abandon those structs and replace them with classes instead?
 
O

osmium

Solang said:
One of my applicatins is a small version of paint with additional features
such as macro,sound and
multiple choices, true/false questions. So, I created quite a few structs
for each object such as line,
polylines, circle arc, image, macro etc... Perhaps, I can start using
classes for this one. Should I
abandon those structs and replace them with classes instead?

I think that modifying a big, working program from C to C++ is not a very
good way to learn C++. I wonder if you have been oversold on C++? Spend an
hour or two at the linked site, focus on "What's so great about C++" for
starters. You will get a lot better answer that way than a bunch of one
paragraph answers on a newsgroup.

Some of the things you have said make me think you don't understand the
difference between a struct and a class. There is a formal (language)
difference and a customary (as used by people "skilled in the art")
difference. Are you awake of that distinction? My earlier response about
access was a "language" response.

http://www.research.att.com/~bs/bs_faq.html
 

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

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top