Exercise: how to remove mutual dependencies ?

A

Anonimo

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.
 
A

Anonimo

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

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.
 
J

Juha Nieminen

Anonimo said:
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

// 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.
 
J

Juha Nieminen

Öö Tiib said:
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

  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?
 
J

Juha Nieminen

Öö Tiib said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top