468,719 Members | 1,705 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,719 developers. It's quick & easy.

a question about understanding some piece of text from a book

Hello!

I'm reading a book about C++ and there is something that I don't understand
so I ask you.
I have marked the section from the book that is of intertest by tagging it
with BOOK START HERE and ending with BOOK ENDING HERE. All that text between
is just a copy from the book.
My question come after the text section.

The book says
BOOK START HERE
"The next example uses the class FileOps which provides three
file-processing functions to find the number of word,characters, and lines
in a file, respectively, using caching. An alternative implementation uses
class attributes that are of reference type instead of mutable members, But
first the Cache must be defined:

struct Cache
{
bool linesCached;
long lines;
bool wordCached;
long words;
bool charsCached;
long chars;

cache();
};

Cache::Cache()
{
linesCached = wordCached = charsCached = false;
lines = words = chars = 0;
}

The structure cache has all its attributes specified as public; this is not
a design flaw because it is hidden from the client.
Now consider the class FileOps, which contains a cache.
class FileOps
{
public:
FileOps(const string, Cache&);
FileOps(const FileOps&);
long lines() const;
long words() const;
long chars() const;
~FileOps();
//should have assignment operator
private:
string filename_;
ifstream fileval_;
Cache& myCache_&;
};

If the variable myCache_ were defined to be of type Cache, then the variable
would represet a nested object, and it would have to be defined as mutable
because its values is modified by constant functions, such as lines().
Reference and pointer attributes have the values referenced by them changed
instead of their own values, and
so they do not have to be specified as mutable."
BOOK ENDING HERE

Now to my question:

Question number 1. Is it possible to use struct as they have done here in
the same way as classes with constructors, destuctors and so on.

Question number 2. How can they say the following "The structure cache has
all its attributes specified as public; this is not a design flaw because it
is hidden from the client". It can't be hidden from the client if its public
or I'm I wrong

Question number 3. What does they mean with this sentence
"Reference and pointer attributes have the values referenced by them changed
instead of their own values, and
so they do not have to be specified as mutable."

//Tony
Jul 23 '05 #1
1 1266

"Tony Johansson" <jo*****************@telia.com> wrote in message
news:m5*******************@newsb.telia.net...
Hello!

I'm reading a book about C++ and there is something that I don't understand so I ask you.
I have marked the section from the book that is of intertest by tagging it
with BOOK START HERE and ending with BOOK ENDING HERE. All that text between is just a copy from the book.
My question come after the text section.

The book says
BOOK START HERE <snip>
BOOK ENDING HERE
Now to my question:

Question number 1. Is it possible to use struct as they have done here in
the same way as classes with constructors, destuctors and so on.
Yes, the only difference between a struct and a class is the default access
specifier. A struct's members are public by default while a class's members
are private by default.

Question number 2. How can they say the following "The structure cache has
all its attributes specified as public; this is not a design flaw because it is hidden from the client". It can't be hidden from the client if its public or I'm I wrong
Class FileOps has a private reference to a Cache. Therefore, Cache's public
members are only accessible from the FileOps class. Hence the client, or
user of the class, can't access Cache's internals, only FileOps can. In
other words, the client must access Cache through FileOps.

Question number 3. What does they mean with this sentence
"Reference and pointer attributes have the values referenced by them changed instead of their own values, and
so they do not have to be specified as mutable."


The content of a reference or pointer is the address of the object it is
pointing to. So these depict a memory location of a given size. If you
modify the referred_to or pointed_to object, you aren't modifying the
reference or pointer.
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Doug Mitchell | last post: by
5 posts views Thread by Paul | last post: by
1 post views Thread by Tony Johansson | last post: by
3 posts views Thread by Ron Vecchi | last post: by
1 post views Thread by hema chandran | last post: by
75 posts views Thread by Steven T. Hatton | last post: by
8 posts views Thread by boki_pfc | last post: by
reply views Thread by Jianwei Sun | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Oskars | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.