Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Exercise: how to remove mutual dependencies ? (http://www.velocityreviews.com/forums/t745041-exercise-how-to-remove-mutual-dependencies.html)

Anonimo 03-13-2011 02:44 PM

Exercise: how to remove mutual dependencies ?
 
Hello,

I have two classes with one static member in each class. Each static
member uses/references the other static member during construction, and
viceversa. Is there a way to remove the mutual dependencies by modifying
the classes containing the static members?

Can you provide a solution useful for a real case with minimal
definitions of both the classes and the static members?

Thanks.

Anonimo 03-13-2011 03:30 PM

Re: Exercise: how to remove mutual dependencies ?
 
> It sounds like you have two static objects defined in different TUs
> which reference each other; this is bad as the construction order of
> static objects across multiple TUs is undefined. You need to rethink
> your design keeping static objects to a minimum; static objects are more
> or less just as bad as global variables.


This exercise has been taken from a Stroustrup's book. The exercise does
not impose to define the members in two different TUs, but explicitly
asks how to avoid the mutual dependency by modifying the containing
classes.. The exercise also asks to mention a possible real case where
one static member could use the other and viceversa.

I know static objects might be bad is some cases, but this is an
exercise and I must answer to what it asks for.

Thanks




Öö Tiib 03-13-2011 03:34 PM

Re: Exercise: how to remove mutual dependencies ?
 
On Mar 13, 4:44*pm, Anonimo <n...@no.it> wrote:
> Hello,
>
> I have two classes with one static member in each class. Each static
> member uses/references the other static member during construction, and
> viceversa. Is there a way to remove the mutual dependencies by modifying
> the classes containing the static members?
>
> Can you provide a solution useful for a real case with minimal
> definitions of both the classes and the static members?


There are no real cases where you need such monsters. So ... minimal
solution of real case is to delete both the static members.

Juha Nieminen 03-13-2011 09:10 PM

Re: Exercise: how to remove mutual dependencies ?
 
Anonimo <no@no.it> wrote:
> I have two classes with one static member in each class. Each static
> member uses/references the other static member during construction, and
> viceversa. Is there a way to remove the mutual dependencies by modifying
> the classes containing the static members?


// Header:
class MyClass
{
private:
class InnerClass;
static InnerClass& innerClass();
//...
};

// Compilation unit:
class MyClass::InnerClass
{
//...
};

MyClass::InnerClass& MyClass::innerClass()
{
static InnerClass instance;
return instance;
}

Öö Tiib 03-13-2011 10:44 PM

Re: Exercise: how to remove mutual dependencies ?
 
On Mar 13, 11:10*pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> Anonimo <n...@no.it> wrote:
> > I have two classes with one static member in each class. Each static
> > member uses/references the other static member during construction, and
> > viceversa. Is there a way to remove the mutual dependencies by modifying
> > the classes containing the static members?

>
> // Header:
> class MyClass
> {
> *private:
> * * class InnerClass;
> * * static InnerClass& innerClass();
> * * //...
>
> };
>
> // Compilation unit:
> class MyClass::InnerClass
> {
> * * //...
>
> };
>
> MyClass::InnerClass& MyClass::innerClass()
> {
> * * static InnerClass instance;
> * * return instance;
>
>
>
> }


It is private static so you can remove any traces of it from interface
of class to implementation.

Juha Nieminen 03-15-2011 02:30 PM

Re: Exercise: how to remove mutual dependencies ?
 
Öö Tiib <ootiib@hot.ee> wrote:
> It is private static so you can remove any traces of it from interface
> of class to implementation.


You have to declare the inner class inside the outer class if you
want the inner class to be able to access the private section of the
outer class.

Öö Tiib 03-15-2011 09:34 PM

Re: Exercise: how to remove mutual dependencies ?
 
On Mar 15, 4:30*pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> Öö Tiib <oot...@hot.ee> wrote:
> > It is private static so you can remove any traces of it from interface
> > of class to implementation.

>
> * You have to declare the inner class inside the outer class if you
> want the inner class to be able to access the private section of the
> outer class.


Ok. I have never needed private static members for such access. So ...
i have no experience. If i did need such access then perhaps a
(forward declared) friend or private class would grant same?

Juha Nieminen 03-16-2011 06:01 PM

Re: Exercise: how to remove mutual dependencies ?
 
Öö Tiib <ootiib@hot.ee> wrote:
> On Mar 15, 4:30*pm, Juha Nieminen <nos...@thanks.invalid> wrote:
>> Öö Tiib <oot...@hot.ee> wrote:
>> > It is private static so you can remove any traces of it from interface
>> > of class to implementation.

>>
>> * You have to declare the inner class inside the outer class if you
>> want the inner class to be able to access the private section of the
>> outer class.

>
> Ok. I have never needed private static members for such access. So ...
> i have no experience. If i did need such access then perhaps a
> (forward declared) friend or private class would grant same?


Isn't that exactly what I did?

Öö Tiib 03-16-2011 07:32 PM

Re: Exercise: how to remove mutual dependencies ?
 
On Mar 16, 8:01*pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> Öö Tiib <oot...@hot.ee> wrote:
> > On Mar 15, 4:30*pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> >> Öö Tiib <oot...@hot.ee> wrote:
> >> > It is private static so you can remove any traces of it from interface
> >> > of class to implementation.

>
> >> * You have to declare the inner class inside the outer class if you
> >> want the inner class to be able to access the private section of the
> >> outer class.

>
> > Ok. I have never needed private static members for such access. So ...
> > i have no experience. If i did need such access then perhaps a
> > (forward declared) friend or private class would grant same?

>
> * Isn't that exactly what I did?


Yes. I was about private static that i have never needed to expose in
header ... neither functions nor data members.


All times are GMT. The time now is 11:54 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.