Osamu Shigematsu wrote:
> 
> Eric, thank you very much for your advice.
> 
> > > (1) With constructor, we can init member vaules.
> >
> > This can be done in C. Think about the
> > fopen/fread/fwrite/fgets/fprintf
> > etc functions. Don't they work like this?
> 
> Um, how can I do that? With struct, there is no constructor, so I have no
> idea to do that...

Have a look at libsndfile. In the public header file there is
a definition like:

typedef  void  *SNDFILE ;

This is basically a pointer to some structure however, the user
of the library knows nothing about what the pointer is pointing
to. 

Inside the library there is a typedef as follows:

typedef struct
{  /* whole pile of stuff in this struct. */
   /* It often changes between releases of the library. */

} SNDFILE_PRIVATE ;

In libsndfile, sf_open_read(), and sf_open_write() functions act
as constructors. The both look something like this:

SNDFILE *sf_open_read (char *filename)
{   SNDFILE_PRIVATE *ptr ;

    ptr = malloc (sizeof (SNDFILE_PRIVATE)) ;

    /* Do a whole pile of initialisation here. */

   return (SNDFILE*) ptr ;
}

The SNDFILE* pointer is then used much like a class object. Instead
of using a C++ calling convention of

    sndfile.sf_function (param1, param2, ...) ;

you do

    sf_function (sndfile, param1, param2, ...) ;

> > > (2) With destructor, we can dispose resources, such as
> > memory block which
> > > had been allocated with operator new, or malloc(). Of
> > course, we can close
> > > file, too.
> >
> > Isn't this what fclose does?
> 
> Yes, but I have to all fclose() when I want to close the file. With
> destructor file will be automatically close when object is removed.

Beware of compilers/languages bearing gifts :-). I use C because it
gives me complete control over what the program does. It doesn't
generate destructors etc unless I specifically tell it to. If I 
want a more high level language I would choose Python (with C
extensions if necessary).


> > > (3) To make member values private, we can check the value
> > is valid range.
> >
> > The user of the above fxxxx function knows nothing of what is
> > contained
> > in whatever the FILE* pointer points to.
> 
> But, we can access struct FILE* directly.

Well, yes you can, but it is very much discouraged. In my libsndfile 
implementation above, accessing data in the struct that SNDFILE* points 
to is very much more difficult. It would require you to include the
private header files from the library sources.

I also seem to remember someone recent stating that if they had a C++
class library where they wanted to access the private data members
they just did:

#define  private public

before including the header file.

> > Have you tried using diff and patch? These two utilities should make
> > your life very much easier.
> 
> I don't know about both diff and patch. Is this available on Macintosh or
> Windoze?

Ahhh, I keep forgeting there are a whole pile of people on this list
who don't use Unix :-). These are widely available on all Unix machines
I have ever seen. I think pathc and diff are also available for Win32
but I'm not sure where.

However, this thread seems to have wandered quite some way off the
subject of MP3 encoders/decoders. If you would like to take it further
maybe we should do it off list.

Erik
-- 
+-------------------------------------------------+
     Erik de Castro Lopo     [EMAIL PROTECTED]
+-------------------------------------------------+
"Using Java as a general purpose application development language 
is like going big game hunting armed with Nerf weapons." 
-- Author Unknown
--
MP3 ENCODER mailing list ( http://geek.rcc.se/mp3encoder/ )

Reply via email to