0

the question is simply a design question.. i mean how to restructure this piece of code into something that simple and efficient.

So lets take the following code for example.

class D{

    var a;

}

class C{

    D d;
    //uses d.a

}

class B{

    C c;

}
Class A{

    B b;
    //needs d.a

}

here class A needs to access the variable "a" in D and classes B And C don't store variable "a" (even though C uses "a" i doesn't store it as it only needs it for initialization). Is there a way to make this code more cleaner.

right now i have made both classes B and C store variable "a". but it seems like it's not the correct way to do it. i mean i am thinking of cases where this chain is longer and the last class needs some variable in the first class. so how should i deal with it.

Adithya Sama
  • 345
  • 3
  • 16
  • `var & C::get_var() { return d.a; }`, `var & B::get_var() { return c.get_var(); }`? – Kerrek SB Mar 19 '16 at 11:44
  • yeah but wouldn't that be slow. i mean the . or -> operators are very expensive right? and a chain of them might not be a good idea. still "var & C::get_var() { return d.a; }, var & B::get_var() { return c.get_var(); }" doesn't look like a good design. is there a better design way to solve this. – Adithya Sama Mar 19 '16 at 12:15
  • I doubt `->` or the `.` would be slow, but I am not a professional – fordcars Mar 19 '16 at 20:46
  • you are right. i checked some other questions relating to that. it seems . and -> are pretty fast. a lot of them say it depends on compiler, which in this case g++, is a good one. so guess it's ok to use @Kerrek SB's way. – Adithya Sama Mar 20 '16 at 08:44

0 Answers0