0

I want some advice about class designs. Let's say that I have 3 classes, "class A", "class B" and "class C". Each class has different namespaces. "A" has an instance of "B", and "B" has an instance of "C". Each class have a "struct Setting" and each class is set with a SetSettings(). Actually, "A" uses "B" to do its job, and "B" uses "C" to do its job.

My question is, is there any better way to do these hierarchy settings?

For example, to break the relation between "A" and "C", "B" could have the same parameters of "C::Settings" instead of defining a c_settings...

Thanks in advance!

A.h

#include "B.h"
namespace A {
struct Settings {
  int param_for_A_1;
  B::Settings b_settings;
};
class A {
  void SetSettings(const Settings& source) {
    settings_ = source;
    b_.SetSettings(source.b_settings);
  }
  Settings settings_;
  B::B b_;
};
}

B.h

#include "C.h"
namespace B {
struct Settings {
  int param_for_B_1;
  int param_for_A_2;
  C::Settings c_settings;
};
class B {
  void SetSettings(const Settings& source) {
    settings_ = source;
    c_.SetSettings(source.c_settings);
  }
  Settings settings_;
  C::C c_;
};
}

C.h

namespace C {
struct Settings {
  int param_for_C_1;
};
class C {
  void SetSettings(const Settings& source) {
    settings_ = source;
  }
  Settings settings_;
};
}    

main.cpp

#include "A.h"
int main() {
  A::Settings settings;
  // Hierarchy settings...
  settings.param_for_A_1 = 1;
  settings.b_settings.param_for_B_1= 2;
  settings.b_settings.param_for_B_2 = 3;
  settings.b_settings.c_settings.param_for_C_1= 4;
  class A::A a;
  a_.SetSettings(settings);
  return;
}
Andrew KeepCoding
  • 7,040
  • 2
  • 14
  • 21
  • 1) depends on how visible you want to have c-class in your application. 2) I somehow dont get your reason for using that strcut to bundle an int to those classes :( – Najzero May 23 '13 at 05:44
  • Thanks for your comment Najzero. 1)In this example hierarchy is not so deep by in my actual project the hierarchy is deeper and I wanted to hide lower levels. 2)Actually there are few parameters depending on the class. – Andrew KeepCoding May 23 '13 at 06:02

1 Answers1

0

As of now, you are not indeed using inheritance, but rather composition. Composition means that a class has an attribute of the type of another class, rather than inheriting the functions and attributes of a parent class.

If I wanted to use inheritance, I probably would have used the class C as a base from which B would inherit. A would then inherit from B. Each "generation" having their own int attribute corresponding to their "settings".

Depending on the situation, this might be more appropriate.

Matzar
  • 243
  • 2
  • 9
  • Thanks Matzar. I'm really sorry for the lack of info, but "A", "B" and "C" are "has-a" case and not "is-a" case, so composition was better for me. – Andrew KeepCoding May 23 '13 at 08:00
  • Well, I don't have much more to say then. :P Without a context it is hard to improve the code. For example, I don't immediately see the use in having `C::Settings c_settings` in the struct for `B` AND having an instance of the class `C` as an attribute, whose only attribute is `c_settings`. I'm also fairly new to programming (< 1 year), so sorry for not being much help. Hope you find your answer. – Matzar May 23 '13 at 08:18
  • The relation between these classes: "A" uses "B" to do its job, and "B" uses "C" to do its job. Thanks anyway! – Andrew KeepCoding May 23 '13 at 08:27