Re: C-style unit -> C++ class, implementation function / structure issue
On Jul 12, 10:27 pm, Jason Doucette <jdouce...@gmail.com> wrote:
(I was using a C++ compiler, so my original code wasn't technically
plain C code... I was using C++ without classes. I was using C-style
units of functions, where the implementation existed all in the .cpp
file, and the interface was visible in the .h file)
I defined the struct in the .cpp file, since only the implementation
functions in the .cpp file used it. The struct was not required for
any interface functions, so it didn't have to exist in the .h file.
In other words, the compilation firewall (pimpl) idiom. You can
do exactly the same in C++.
I didn't think my issue was the Pimpl Idiom... but I don't know
exactly what Pimpl is about, so I could be wrong. I thought Pimpl was
to allow the implementation to change without any visible effects in
the interface, and thus saving compilation times.
My issue is merely that I don't want an implementation-only structure
visible at all. And this entire thread was rather unneeded, since C++
does have a way to hide an implementation structure from being visible
-- just define it in the class itself as private! I should have known
this.
It wasn't clear, but I presumed that the C idiom you were
talking about was to forward declare the struct in your header,
to pass a pointer to it to all of the functions, and to only
define it in the source files that used it. That's exactly what
the compilation firewall idiom does in C++. In both C and C++,
of course, it requires that the actual data be dynamically
allocated (or static, if there is only one of them). If that's
not the case, then I don't know what in C you are trying to map
into C++.
For the rest, of course, declaring member types (private or
otherwise) is pretty much standard C++.
My issue is that I didn't want to move the struct into the .h file
when I converted this 'unit' into a C++ class. But, I now realize I
can put the struct in the .h file, under the class's private section,
and it'll be hidden from the interface. All is well.
Yes. C++ gives you the choice; C didn't.
Actually, in C, I had the structure declared in the .cpp file, so it
wouldn't appear in the interface. I could change the .cpp file all I
wanted, and since the .h file never changed, all was well.
In which case, you can do exactly the same in C++. (I tend to
do it a lot, in fact.)
Only in C++ does the changes to the implementation all of a
sudden require a recompile for any 'users' of the class, since
they include the .h file which you are changing.
Not if you do it the same way you do in C. As I said, in C++,
you have the choice; both solutions work. Typically, declaring
more in the header will result in slightly faster execution
times, but more compiler dependencies. The smaller and simpler
the class, the more the difference in execution times will be
noticeable, and for something like complex, where the data
structure is more or less predefined and won't ever change, but
which is likely to be used in some tight loops, the C++ solution
definitly has an advantage (and in fact, in C, you'd certainly
declare the struct in header as well, even if it means that
anyone can actually access the data directly). For larger, more
complex objects, the time difference quickly becomes
negligeable, however, and in large projects (say, from a million
LOC up), the compilation firewall idiom is almost an automatism.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34