Base class manipulation of a derived class' structure

Discussion in 'C++' started by Victor Hannak, Aug 25, 2003.

  1. I have two classes derived from a base class. The two derived classes each
    utilize a structure that is slightly different from one another. i.e.

    DerivedClass1:

    struct NodeStruct {
    float NodeValue;
    ListStruct *NextNode;
    }

    DerivedClass2:

    struct NodeStruct {
    int NodeValue1;
    int NodeValue2;
    ListStruct *NextNode;
    }

    I would like to put a function in the base class to delete linked lists
    created with these structs. The problem is that these structures are not
    declared in the base class, nor can they be because each derived class
    requires a slightly different structure. So the question is, how can I
    reference a structure that will be declared in the derived class?

    I am thinking that this is similar to a virtual function where it is
    dynamically determined which derived class function to call. In my case, I
    would like to define a generic structure pointer such that when the base
    class function is called, the right structure is used according to which
    derived class I am operating on.

    Thanks, Vic
    Victor Hannak, Aug 25, 2003
    #1
    1. Advertising

  2. "Victor Hannak" <> wrote...
    > I have two classes derived from a base class. The two derived classes

    each
    > utilize a structure that is slightly different from one another. i.e.
    >
    > DerivedClass1:
    >
    > struct NodeStruct {
    > float NodeValue;
    > ListStruct *NextNode;
    > }
    >
    > DerivedClass2:
    >
    > struct NodeStruct {
    > int NodeValue1;
    > int NodeValue2;
    > ListStruct *NextNode;
    > }
    >
    > I would like to put a function in the base class to delete linked lists
    > created with these structs. The problem is that these structures are not
    > declared in the base class, nor can they be because each derived class
    > requires a slightly different structure. So the question is, how can I
    > reference a structure that will be declared in the derived class?


    So the answer is, you cannot. It's simple. You cannot reference
    anything that hasn't been declared yet. What you can do is to make
    each of the derived classes do their own cleanup. Polymorphically.

    > I am thinking that this is similar to a virtual function where it is
    > dynamically determined which derived class function to call. In my case,

    I
    > would like to define a generic structure pointer such that when the base
    > class function is called, the right structure is used according to which
    > derived class I am operating on.


    class BaseClass {
    virtual void clearList() = 0;
    };

    class DerivedClass1 : public BaseClass {
    void clearList(); // do what #1 needs
    };

    class DerivedClass2 : public BaseClass {
    void clearList(); // do what #2 needs
    };

    You _could_ of course, define some kind of LinkStruct class in the
    Base, then define a data _member_ a pointer to that type, but then
    you would need to make DerivedClass1 and DerivedClass2 create those
    objects in the derived class objects dynamically and it's not any
    easier or prettier.

    Victor
    Victor Bazarov, Aug 25, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    393
    Alf P. Steinbach
    May 23, 2007
  2. Replies:
    1
    Views:
    387
    myork
    May 23, 2007
  3. Replies:
    1
    Views:
    374
    Victor Bazarov
    May 23, 2007
  4. David
    Replies:
    3
    Views:
    393
    Grizlyk
    Jan 29, 2008
  5. Replies:
    2
    Views:
    695
Loading...

Share This Page