|
Need help understanding this...
Ok, I have a questiong on pointers. In the following code, the book author declares one of its private variables as a pointer. I''m just wondering what the use of making "char *itsString;" a pointer. I have a vague understanding of how this allows the value of itsString to be changed by other functions, but not totally sure on this. Also I think it has something to do with better memory allocation or something. Anyways, take a look and explain me the uses of making "char *itsString" a pointer. Thanks
One more thing: This also includes a private constructor. I''v never seen one of them so could someone explain the use of making a constructor private.
Hmmm...
Well, the reason you want itsString to be a pointer is the fact that you want to point to a block of memory of dynamic size and assumed type char. You will find this a lot in string classes, because you want to be able to put a string of any size in it (usually NULL terminated). A pointer just gives a hint about the type and a memory location at which to start reading, then it starts reading until it realizes that memory block is done (whether by running out of size for, say, a struct, or by hitting a NULL terminator in a string, which is understood by sprintf and other string handling functionality).
Private constructors: I''ve seen 2 compelling reasons for doing this. In this particular case, I''m betting it''s for functionality that you don''t want to lay open to being used commonly, but that you do want there in order to do something internally in the class. You will notice that there are several constructors that aren''t private. You might look at the implementation of the class and see where (or even if) they are using that constructor for some special functionality.
The second reason I''ve seen for using private constructors (that makes sense, anyway...) is for singletons. Singletons are classes of which there should only, ever, be one copy in memory. In order to limit the number of copies to one, the constructor is made private and the external code is required to access it through an instance handle in the class. The private constructor is mainly there to protect the class from clueless people who want to make more instances of the singleton (so they literally can''t.) Say you want an in-memory copy of, say, a table from your database that will never change. There''s no reason to read it more than once, and it won''t change, so you can make a single instance of an in-memory copy and use it freely (and with a great speed increase over reloading it from the database constantly, without the chance that you will ever have a couple hundred copies of it clogging up your allocations).
-fel
Well, the reason you want itsString to be a pointer is the fact that you want to point to a block of memory of dynamic size and assumed type char. You will find this a lot in string classes, because you want to be able to put a string of any size in it (usually NULL terminated). A pointer just gives a hint about the type and a memory location at which to start reading, then it starts reading until it realizes that memory block is done (whether by running out of size for, say, a struct, or by hitting a NULL terminator in a string, which is understood by sprintf and other string handling functionality).
Private constructors: I''ve seen 2 compelling reasons for doing this. In this particular case, I''m betting it''s for functionality that you don''t want to lay open to being used commonly, but that you do want there in order to do something internally in the class. You will notice that there are several constructors that aren''t private. You might look at the implementation of the class and see where (or even if) they are using that constructor for some special functionality.
The second reason I''ve seen for using private constructors (that makes sense, anyway...) is for singletons. Singletons are classes of which there should only, ever, be one copy in memory. In order to limit the number of copies to one, the constructor is made private and the external code is required to access it through an instance handle in the class. The private constructor is mainly there to protect the class from clueless people who want to make more instances of the singleton (so they literally can''t.) Say you want an in-memory copy of, say, a table from your database that will never change. There''s no reason to read it more than once, and it won''t change, so you can make a single instance of an in-memory copy and use it freely (and with a great speed increase over reloading it from the database constantly, without the chance that you will ever have a couple hundred copies of it clogging up your allocations).
-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
Some info about C string:
char *astring is the standard way of making a variable to hold a string.
A string is just a list of characters (type char) with the last one being 0 which means end of string. eg. "hello":
[h][e][l][l][o][0]
each box is a char, so a char* pointer points to the first character, [h]
here are 2 ways to fill a string:
astring = new char[6]; //allocate (sorry about the C/C++ mix
)
strcpy(astring, "hello");
delete []astring; //free the memory when done
or
astring = strdup("hello"); //allocate and copy
free(astring); //free the memory when done
char *astring is the standard way of making a variable to hold a string.
A string is just a list of characters (type char) with the last one being 0 which means end of string. eg. "hello":
[h][e][l][l][o][0]
each box is a char, so a char* pointer points to the first character, [h]
here are 2 ways to fill a string:
astring = new char[6]; //allocate (sorry about the C/C++ mix

strcpy(astring, "hello");
delete []astring; //free the memory when done
or
astring = strdup("hello"); //allocate and copy
free(astring); //free the memory when done
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement