0

Is it possible/logically correct for Base class to have an association relationship to it's derived classes? For example:

class First_Child;
class Second_Child;

class Base
{
private:
    First_Child** ppFirstChild;
protected:
    Second_Child** ppSecondChild;
}

class First_Child : public Base
{
    // I want First child to be able to access **ppSecondChild**
}

class Second_Child : public Base
{
   // I want Second child to NOT be able to access **ppFirstChild**
}

I found nothing on google related to this subject. Please feel free to suggest other takeaways[my objective would be to be able to access those double pointers as mentioned in comments from child classes].

lukuss
  • 128
  • 7
  • 2
    Look into [CRTP](https://en.cppreference.com/w/cpp/language/crtp) ([more here](https://stackoverflow.com/questions/4173254/what-is-the-curiously-recurring-template-pattern-crtp)) - but `First_Child` and `Second_Child` won't share a common base if you use it. – Ted Lyngmo Sep 18 '20 at 07:03
  • Looks like homework...Move everything under `private:` section. And make `First_Child` a `friend class` – malat Sep 18 '20 at 07:05
  • Btw, why are you using _pointer-pointers_? Is there any reason why you are not using `std::vector`? – Ted Lyngmo Sep 18 '20 at 07:10
  • @malat Not a homework, but some personal practice, indeed. I had this idea and just wanted to know if it is logically correct or somehow accepted by vast majority, since I've found nothing on this subject. Thanks for the suggestion! – lukuss Sep 18 '20 at 07:10
  • @Ted At work I use pointers a lot and I'm used to it. ```std::vector``` I used only once and same, in a practice problem. – lukuss Sep 18 '20 at 07:12
  • "Is it possible" is a question faster answered by compiling than writing up a question. "Logically correct" depends on your overall design. Since the code matches your intent, what is the question for us? – JaMiT Sep 18 '20 at 07:20
  • If it's logically correct and good design depends on what the goal is. It's certainly _possible_ to do this. If you use raw pointers a lot at work you must be really comfortable with the rule of 3/5/0 and RAII. Is that correct? I can't imagine getting my code through a code review if I used raw pointers where `vector`s, or in some cases smart pointers, could be used. – Ted Lyngmo Sep 18 '20 at 07:29
  • @TedLyngmo Not using ```vector``` is one of project(embedded one)'s restrictions. Nor dynamic allocation(so nothing to deallocate in destructor). Nor smart pointers. I work in C++03. But that's another topic. Anyway, you guys clarified things for me. I wanted to know if it is somehow accepted, if it is not a bad practice.Thank you! – lukuss Sep 18 '20 at 07:53
  • "_Nor dynamic allocation(so nothing to deallocate in destructor)_" - I don't see how you can fulfill that. The code you've shown will require dynamic allocation some way or another. – Ted Lyngmo Sep 18 '20 at 08:04
  • My code, yes indeed. – lukuss Sep 18 '20 at 08:15
  • @lukuss So, for your code, use `vector`s and/or smart pointers. I assume you are not working in C++03 in your own project? – Ted Lyngmo Sep 18 '20 at 08:20
  • 1
    No, no, I'm not using 03. All right, I'll use them. Thanks. – lukuss Sep 18 '20 at 08:27

0 Answers0