Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Exercise: how to remove mutual dependencies ?

Reply
Thread Tools

Exercise: how to remove mutual dependencies ?

 
 
Anonimo
Guest
Posts: n/a
 
      03-13-2011
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.
 
Reply With Quote
 
 
 
 
Anonimo
Guest
Posts: n/a
 
      03-13-2011
> 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



 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      03-13-2011
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.
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      03-13-2011
Anonimo <> 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;
}
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      03-13-2011
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.
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      03-15-2011
Öö Tiib <> 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.
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      03-15-2011
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?
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      03-16-2011
Öö Tiib <> 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?
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      03-16-2011
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
ISIS and EIGRP mutual redistribution --Help! phillip Cisco 2 09-19-2008 08:27 PM
mutual class dependencies mosfet C++ 19 01-29-2008 03:38 AM
Problems mutual redistributing of ISIS and EIGRP phillip Cisco 5 12-07-2004 07:49 PM
Mutual EJB possible? Vjeran Marcinko Java 3 10-17-2003 09:21 PM



Advertisments