C++ Help ...
Kevin O'Gorman
kogorman at gmail.com
Fri Aug 14 17:41:35 PDT 2009
On Fri, Aug 14, 2009 at 7:15 AM, Ben Duncan<bend at linux4ms.net> wrote:
> Ok, I have been looking at some C++ code lately and even though I am
> proficient in C, I cant seem to figure certain things ount in C++. maybe
> some
> can explain them to me....
>
> In the following snippet of code:
> --------------------------------------------------------------------------------------
>>
>> FileMgr::FileMgr(const char *fname, int rsize, int ctype, int mode, int
>> file_des) :
>> DiskMgr(ctype)
>> {
>> aaao = new char[strlen(fname) + 1];
>> strcpy(aaao, fname);
>> aaar = rsize;
>> aaaq = aaar + 2;
>> fd = -1;
>> aaas = 1;
>> if (mode == moOpen)
>> {
>> open_file();
>> }
>> if (mode == moAlreadyOpen)
>> {
>> fd = file_des;
>> aaas = 0;
>> int tmp_aaaq = -1;
>> DiskMgr::read(fd, &tmp_aaaq, 0, sizeof(tmp_aaaq));
>> if (tmp_aaaq != aaaq)
>> {
>> IsamError("Error: Program and Data Files out of
>> sync!");
>> exit(0);
>> }
>> }
>> aaau = -1;
>> aaav = 6;
>> }
>>
>> FileMgr::~FileMgr(void)
>> {
>> close_file();
>> delete aaao;
>> }
>
> ------------------------------------------------------------------------------------------
>
> What does the "FileMgr::FileMgr(c..." mean to a C programmer like me?
The short answer is that it means you should educate yourself on
Object Oriented (OO) programming. But here's the
longer (very incomplete) answer.
This is a method (oops, that's a function to you) of the FileMgr
class. Because the method has the same name as the class, it's one of
the constructors of the class, one of which is called automatically
when an object of the class is created. If you don't define one, a
"default constructor" that does nothing is used.
> Embedded in the that code is "DiskMgr::read(fd, ...." whatd does that
> mean and do?
>
This is a call to a "read" method/function in the DiskMgr class.
Presumably a static method because it's not being called on an object
of that class.
> And finally, there is this at the bottom: "FileMgr::~FileMgr(void)"
> What does that do?
It's a destructor, more or less the opposite of a constructor, and is
called when an object is being destroyed. Most programs can get away
without defining these, but they're useful if you want to close files
or reclaim resources. The one in the example does both.
>
> Thanks ...
HTH
++ kevin
--
Kevin O'Gorman, PhD
More information about the Linux-users
mailing list