Re: some comments about our C++ code...


Subject: Re: some comments about our C++ code...
From: Joaquin Cuenca Abela (cuenca@celium.net)
Date: Tue Apr 17 2001 - 07:01:22 CDT


On 17 Apr 2001 17:11:03 +1000, Martin Sevior wrote:
>
>
> On Mon, 16 Apr 2001, [iso-8859-1] Joaquín Cuenca Abela wrote:
>
> HI Joaquin,
> Sorry for taking up your extremely valuable development time
> with this. You're right of course.

my development time is not so extremely valuable :-)

[snip]
> > pDocument->m_docPageSize.whatever();
> >
> > instead of
> >
> > pDocument->getPageSize().whatever();
> >
>
> OK Here I'm confused. Since the m_docPageSize class is in private space
> how can we change it's internal state from outside the PD_Document class?
>
> ie:
> pDocument->getPageSize().Set(double w, double h, Unit u)
>
> alters the internal state of the fp_PageSize class. I thought this was
> forbidden outside the PD_Document class.
>
> This is the reason I put it in the public space. What am I missing?

ok, here you have a little misunderstanding. In fact, private and const
are orthogonal issues. When you do something like:

class PD_Document
{
...
private:
    fp_PageSize * m_docPageSize;
...
};

implies that nobody besides PD_Document objects can reach m_docPageSize
*directly*. Ie, if somebody (say in the class FooBar), has a pointer
through a PD_Document object (say m_pDoc), it will be illegal (and the
compiler will not allow you to do it) to do something like:

m_pDoc->m_docPageSize.Set(...);

nor

bar = m_pDoc->m_docPageSize.getScale();

Ie, it doesn't matters if the operation is const or not (well, really,
getScale() is not a const operation, but that's a bug, it should be
marked as const).

In fact, it implies that the fact that PD_Document has a m_docPageSize
member is *absolutely* *invisible* to the objects that are not of
PD_Document class.

Why is it cool? Because latter you may want to move the m_docPageSize
pointer to another place, or just remove it, or just whatever. When you
will do it, and due to his *absolute* *invisibility* to the other
classes, you will not be forced to change a *single* line of code beyond
PD_Document. Nobody knowed that this pointer was in PD_Document anyway.

Now, the another question is: "Why m_pDoc->getPageSize().getScale()
works??"

Because in the api of PD_Document you provided a *public* function with
this signature:

const fp_PageSize &getPageSize() const;

and thus, everybody knows and can suppose, that a PD_Document class has
a getPageSize() member that gives back a "const fp_PageSize &". The
point here is that nobody can know where is this object, in which class
it lays, how PD_Document manages to obtain it, etc, etc, etc... these
are implementation details, that can change (and eventually will change)
in the future.

The most simple implementation of this method for PD_Document will
probably be to (just like we have right now) have a fp_PageSize instance
between his variables, and give back a reference to this variable,
but... don't suppose it in the code extern to PD_Document!

(we can say the same thing about "void setPageSize(const fp_PageSize
&)".)

Cheers,

-- 
Joaquín Cuenca Abela
cuenca@celium.net



This archive was generated by hypermail 2b25 : Tue Apr 17 2001 - 07:01:39 CDT