Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C-style unit -> C++ class, implementation function / structure issue

Reply
Thread Tools

C-style unit -> C++ class, implementation function / structure issue

 
 
Jason Doucette
Guest
Posts: n/a
 
      07-17-2007
> // --- auto.h ---
>
> class Car{
> private:
> struct car;
> car *theCar;
> };
>
> // --- auto.cpp ---
>
> struct Car::car{
> ...........
> }
>
> Is that what you were/are doing?


This is what I am doing now, yes. So, now the structure is define in
the .cpp file, and can change, without affecting the interface, or
being visible in the interface. Just like it was back in the C-style
code.

Jason

 
Reply With Quote
 
 
 
 
Jason Doucette
Guest
Posts: n/a
 
      07-17-2007
> > > Typically, declaring
> > > more in the header will result in slightly faster execution
> > > times, but more compiler dependencies.

> >
> > Fast execution times? Why is that?

>
> The more the compiler knows, the better it can optimize.


Ah, of course. Kind of like how it can inline code, but only if it
has access to the code to inline (in the .h file). Probably a much
simpler example, but the same concept.


> In
> this case, however, I was thinking more along the lines of being
> able to allocate the data directly on the stack, or in the
> class, rather than needing a dynamic allocation.


Right, with knowledge of the struct, it could place it on the stack,
or within the class data members, without requiring that extra pointer
(in both cases) that dynamic allocation would require, and the extra
dereference required by that point will slow things down.

I guess all of my structs are so large that I want them dynamically
allocated. I am somewhat paranoid about stack space, since it's easy
to forget how much you're using, so, all structs are passed by
pointers (or reference, which is the same thing, internally -- just a
pointer on the stack). But, I get your point.


> If you want to see just how high the real cost can be, implement
> a trivial Complex class (e.g. "double real, imag ;"---no need
> for the operators for this test), then reimplement it using the
> compilation firewall idiom (
> class Complex
> {
> class Impl ;
> Impl * myImp ;
> public:
> // ...
> } ;
> ).
> Now create an std::vector of a couple of million of each.


Oh, right, of course, that extra dereference each time will be a
killer. I already had structures, and they were so large, that I was
already dynamically allocating them, and I don't have many instances
of such classes. Still, to access the data in these structs, I need
that deference. And if the structs were right in the class
themselves, then they wouldn't need that dereference. I'll keep this
in mind. Having the struct definition itself public isn't really a
problem, as long as the instance of it within the class is private, it
should be ok (although this entire thread commenced at my desire to
solve exactly this -- to make the struct definition private, as well).


> The question is what the difference is, and when. Using the
> compilation firewall idiom requires an allocation in the
> constructor, a delete in the destructor, and (generally) an
> extra indirection in every function call. If the object
> lifetimes are long, and the functions are complex, the
> difference isn't measurable. If objects are frequently created
> and destroyed (value semantics with deep copy, for example), and
> the functions are almost trivial (complex:perator+), the
> difference can be important.


Creation/destruction hardly ever happens. But, the data in the struct
will be accessed, and there will be a dereference each time. I'll
make a note of this to test it out. For the most part, if there's any
serious data accessing going on, I get a direct pointer to that data,
and directly access it... instead of dereferencing it each time.

Jason

 
Reply With Quote
 
 
 
 
Jason Doucette
Guest
Posts: n/a
 
      07-18-2007
I made 2 posts via Google Groups to this thread yesterday, and I don't
see them... I'll post a short note here to see if this works...
perhaps Google is merely delayed...

Jason

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User Friendly Easy To Install Unit For Voip Like Vonage Unit SuggestionsPlease? Heidy UK VOIP 1 06-22-2007 09:53 AM
unit--, a unit test framework for C++ VvanN C++ 5 04-28-2006 10:01 AM
connect a source unit with an S-Video connection to a receiving unit that has an RCA composite video connection ? worth it ? OCZ Guy DVD Video 6 08-01-2004 05:44 PM
Connect a source unit DVD with an S-Video connection to a receiving unit TV that has an RCA composite video connection Worth it. OCZ Guy Computer Information 0 07-31-2004 12:29 PM



Advertisments