none
deriving classes RRS feed

  • Question

  • if class X creates an instance of class Y and then calls class Y functions (no other activities exist) would this justify deriving class Y from class X to ensure the functions exist (using pure virtual functions) or is there another/better way?
    • Edited by Dalai Wednesday, December 18, 2013 1:16 PM
    Wednesday, December 18, 2013 1:16 PM

Answers

  • "I 'd just like to ensure that FuncY() exists."

    In this particular case you're ensuring that by simply calling it. Such code would not compile if the Y class didn't have a FuncY function.

    • Marked as answer by Dalai Wednesday, December 18, 2013 3:05 PM
    Wednesday, December 18, 2013 2:59 PM
    Moderator

All replies

  • What I understand from your question looks something like this:

    class Y {
    public:
        void FuncY() { }
    };
    
    class X {
        Y *y;
    public:
        X() : y(new Y) {
        }
    
        void FuncX() {
            y->FuncY();
        }
    };
    If this is what you have in mind then the answer is simply 'no'. Not to say that deriving would be the wrong thing to do here but there's nothing about this example that requires you to derive Y from X.
    Wednesday, December 18, 2013 1:46 PM
    Moderator
  • ok Mike thanks "another/better way?" I 'd just like to ensure that FuncY() exists.
    • Edited by Dalai Wednesday, December 18, 2013 1:57 PM
    Wednesday, December 18, 2013 1:53 PM
  • "I 'd just like to ensure that FuncY() exists."

    In this particular case you're ensuring that by simply calling it. Such code would not compile if the Y class didn't have a FuncY function.

    • Marked as answer by Dalai Wednesday, December 18, 2013 3:05 PM
    Wednesday, December 18, 2013 2:59 PM
    Moderator