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 1 1423
"Tony Johansson" <jo************ *****@telia.com > wrote in message
news:m5******** ***********@new sb.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Doug Mitchell |
last post by:
Dear Group,
My son who is in grade 7 has *just* started going through the book "Python
for the Absolute Beginner" by Michael Dawson. He and I have no programming
experience. He is beginning this on an old win 95 computer with Python 2.2 I
think.
He is getting a bit frustrated with color coding.
For example in Chapter 2 page 18 and 19 Mr. Dawson describes a more fancy
way of printing "Game Over" on the screen. According to *him*...
|
by: Paul |
last post by:
I've bought two books on CSS, and found they're no good.
Now I'm reading "Cascading Style Sheets, level 2 revision 1
CSS 2.1 Specification" , but it's tough going.
I have lots of questions about parts in that spec. Does anyone know who can
answer them?
Is there maybe an "Annotated CSS 2.1 Spec" ? That is, one with extra
explanation added to every complex piece of text.
As an example of two of my questions:
|
by: Tony Johansson |
last post by:
Hello
I'm trying to learn XML by reading a book.
There is some text in the book that I don't understand so I try to ask you
out there.
My first question is does this statement mean that the character string
from is placed into the attribute with name type
<contect type = "from">
|
by: Ron Vecchi |
last post by:
I am creating my own MainMenu control similar to VS.Net. (yes I know theirs
some out their).
I have predominatly been a web developer for about 5 years and am playing
with Windows forms. So this should help me learn.
My question is about IntPtr.
Basically what is it, I know its a pointer but what is a pointer.(of course
I know it points to something but how) .
In my MenuControl I needed a graphics object in my OnPaint override.
|
by: hema chandran |
last post by:
Hello sir
I am fresher in DOTNET sir, so plz help me referents book
Thank u sir
This is my mail i.d
hemudotnet@yahoo.com
| |
by: Steven T. Hatton |
last post by:
No, this is not a troll, and I am not promoting Java, C-flat, D, APL, Bash,
Mathematica, SML, or LISP. A college teacher recently posted to this
newsgroup regarding her observation that there has been a significant
decline in the number of students opting to take courses in C++. I just
want to let people know what I believe are the biggest obstacles to C++
language acquisition, and what aspects of the language make it less
appealing than...
|
by: boki_pfc |
last post by:
Hi Everybody,
I am looking for an advice on following:
I have that "pleasure" of reading C++ codes that have been written by
person(s) that have not attended the same C++ classes that I did or
have not read the same C++ books that I have read. This kind of
people has written some parts of the code that use notations that I am
not familiar with (and that probably also includes also 50 % of other C
++ programmers). While everybody who...
|
by: Jianwei Sun |
last post by:
Hello Alf,
Thank you, and I like "that could be like interpreting as favorably as possible
the writings of a
chimpanzee posing as a college professor.".
However, I will still read this book, with more attention to the c++ code.
J.W.
|
by: lorlarz |
last post by:
I still have a question regarding the following code,
in a commonly used routine. First, Here's the code in
question:
Function.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object =
args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |