T
Tony Johansson
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
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