Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Currently I have 2 classes that are tightly coupled with one another.

Lets say the classes are MainClass and HelperClass.

mainclass.h
C++
class MainClass{
public:
 fun1();
 fun2();
 fun3();

 static MainClass* mainClass;
};

//definition of fun1
int MainClass::fun1()
{
//doSomething;
}


the helper class file includes mainclass file and define the two functions of MainClass

C++
#include <mainclass.h>

class HelperClass{
helpfun1();
helpfun2();
};

MainClass::fun1()
{
 HelperClass obj;
//dosomethmng
}

MainClass::fun2()
{
 HelperClass obj;
//dosomethmng
}

//Problem area
//
HelperClass::helpfun1()
{
  MainClass::mainClass->fun1();
}


Please suggest on decoupling these classes.
Posted
Updated 29-Nov-13 1:30am
v3
Comments
Stefan_Lang 28-Nov-13 8:32am    
Not clear. The HelperClass doesn't appear to serve any purpose, so just throw it away.

P.S.: what I mean is that *you* didn't offer any reason why you still need HelperClass. You appear to be thinking you need it, but nothing in the code you've shown proves it. Your implementation of helpfun1() doesn't require a class, you could just replace the call to helfun1() with this code. If this is a problem, please explain why.

Also your HelperClass doesn't have member variables, so it doesn't have a state - it's just a set of functions which could just as well be defined globally, or inside a namespace.
CPallini 28-Nov-13 9:08am    
my virtual 5.
Aswin Waiba 28-Nov-13 23:45pm    
Actually that is just a simplified function. The helper class file contains a lot of templatized classes, each fullfilling a particular function. And As from the above example the MainClass function definitions in helperclass.h uses those templatized classes. However some functions of HelperClass (es) also directly use the static member of MainClass. I want to separate out the two, and at the same time provide access to the static member of MainClass.

Another point to note is HelperClass(es) is derived from HelperParentClass and all the functions are virtual.
Most of the other function of MainClass access HelperClass objects via HelperParentClass pointer.
Stefan_Lang 29-Nov-13 3:02am    
You seem to be missing my point that without showing all the code involved in the coupling we cannot provide help on how to decouple it!

That's like asking us to remove a bug without showing us the code that contains it!
Nelek 29-Nov-13 7:31am    
2nd Virtual 5

This may or may not be what you're looking for, but this is the generic way of decoupling too classes, applied to the definitions you provided:
C++
// IMainClass.h
class IMainClass {
public:
   virtual int fun1()=0;
   virtual int fun2()=0;
   virtual int fun3()=0;
   static IMainClass* getInstance();
};

// MainClass.h
#include "IMainClass.h"
class MainClass : public IMainClass{
public:
   virtual int fun1();
   virtual int fun2();
   virtual int fun3();

   static MainClass* mainClass;
};

// IMainCLass.cpp
#include "IMainClass.h"
#include "MainClass.h"
IMainClass* IMainClass::getInstance() {
   if (MainClass::mainClass == 0)
      MainClass::mainClass = new MainClass;
   return MainClass::mainClass;
}

// MainClass.cpp
#include "MainClass.h"
#include "IHelperClass.h" // only include the interface!
MainClass* MainClass::mainClass = 0; // initialization of static member required!
//definition of fun1
int MainClass::fun1()
{
   //doSomething;
}
...

// IHelperClass.h
class IHelperClass {
public:
   virtual int helperfun1()=0;
   virtual int helperfun2()=0;
   static IHelperClass* getHelper();
};

// HelperClass.h
#include "IHelperClass.h"
class HelperClass : public IHelperClass {
   virtual int helperfun1();
   virtual int helperfun2();
};

// IHelperClass.h
#include "IHelperClass.h"
#include "HelperClass.h"
IHelperClass* IHelperClass::getHelper() {
   return new HelperClass;
}

// HelperClass.cpp
#include "HelperClass.h"
#include "IMainClass.h" // only include the interface!
...

Note how I split up both classes into an interface and an implementation. From MainClass you can now reference all the functionality of HelperClass through the interface alone, so you only need to include IHelperClass.h, not HelperClass.h. Similarly, the Helperclass implementation can access all functionality from MainClass via its interface IMainClass and only needs to include IMainClass.h.

I introduced get-functions to give access to actual objects of type HelperClass and MainClass without needing the implementation class declarations.
 
Share this answer
 
Are these coupled ? :) :
C++
// A.h
class A
{
public:
  static void fun() {}
};

C++
//test.cpp
#include "A.h"

class B
{
public:
  void fun() const { A::fun(); }
};

class test
{
public:
  test()
  {
    B b; b.fun();
  }
} g_test;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900