Accessing derived class members from base class

Discussion in 'C++' started by Bhawna, Aug 25, 2008.

  1. Bhawna

    Bhawna Guest

    I am into c++ code maintenance for last 3-4 years but recently I am
    put into design phase of a new project. Being a small comapany I dont
    have enough guidance from seniors.

    Currently I am into a situation where I am implementing base class
    functions by including a pointer to subclass member in base class.

    Reason being functionality is common for subclasses but the members
    are common within subclass only (static member of subclass) but vary
    across different subclasses.

    I am confused is it a godd design decision or there is another
    alternative to this. If anybody can provide me details of situations
    where baseclass accesses derived class members I would be better able
    to justify myself.

    Can anybody refer me a good step by step design guide for C++
    programming.

    Any help in this regard will be greatky appreciated.

    Thanks
    Bhawna
    Bhawna, Aug 25, 2008
    #1
    1. Advertising

  2. Bhawna

    Bhawna Guest

    On Aug 25, 5:54 pm, "Alf P. Steinbach" <> wrote:
    > * Bhawna:
    >
    >
    >
    >
    >
    > > I am into c++ code maintenance for last 3-4 years but recently I am
    > > put into design phase of a new project. Being a small comapany I dont
    > > have enough guidance from seniors.

    >
    > > Currently I am into a situation where I am implementing base class
    > > functions by including a pointer to subclass member in base class.

    >
    > > Reason being functionality is common for subclasses but the members
    > > are common within subclass only (static member of subclass) but vary
    > > across different subclasses.

    >
    > > I am confused is it a godd design decision or there is another
    > > alternative to this. If anybody can provide me details of situations
    > > where baseclass accesses derived class members I would be better able
    > > to justify myself.

    >
    > > Can anybody refer me a good step by step design guide for C++
    > > programming.

    >
    > > Any help in this regard will be greatky appreciated.

    >
    > Try virtual functions.
    >
    > To make that work, dispense with those static members, make them non-static.
    >
    > It's common newbie error to make data and functions static, don't.
    >
    > Cheers, & hth.,
    >
    > - Alf
    >
    > PS: For more specific help, post some (real) code.
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?- Hide quoted text -
    >
    > - Show quoted text -


    Thank you very much for your quick response. The situation I have is
    fuctions do not need to be virtual as functionality is completely
    common among all derived classes (not all functions but the ones which
    are in question right now), only data manipulation is specific.

    Actually I am converting a set of database tables as classes to
    cutomize query generation for database specific to the table. Scenario
    is - table is base class and specific tables are subclasses. I am
    using table name, column names of the table and such table specific
    data as static members and then once they are accessible in base
    class, function need not be virtual as logic for creating queries is
    completely common. This needs we have values of table and columns
    names along with number of columns and few flags detailing keys and
    relationships, so I defined them all as static members of derived
    class.

    If hope this helps.

    Thanks again
    Bhawna
    Bhawna, Aug 25, 2008
    #2
    1. Advertising

  3. On Aug 25, 3:11 pm, Bhawna <> wrote:
    > Thank you very much for your quick response. The situation I have is
    > fuctions do not need to be virtual as functionality is completely
    > common among all derived classes (not all functions but the ones which
    > are in question right now), only data manipulation is specific.
    >
    > Actually I am converting a set of database tables as classes to
    > cutomize query generation for database specific to the table. Scenario
    > is - table is base class and specific tables are subclasses. I am
    > using table name, column names of the table and such table specific
    > data as static members and then once they are accessible in base
    > class, function need not be virtual as logic for creating queries is
    > completely common. This needs we have values of table and columns
    > names along with number of columns and few flags detailing keys and
    > relationships, so I defined them all as static members of derived
    > class.
    >
    > If hope this helps.
    >
    > Thanks again
    > Bhawna


    There's nothing wrong with having a base class with non-virtual and
    virtual functions. Consider the following example:

    class Base
    {
    public:
    // Non-virtual function which does something with the value
    specified in the derived class.
    void Foo()
    {
    std::cout << "Member's value is: " << GetMember() << std::endl;
    }

    // Pure virtual function for retrieving the actual member value.
    virtual GetMember() const = 0;
    };

    class Derived1 : public Base
    {
    public:
    virtual GetMember() const { return member_; }
    private:
    static const int member_ = 100;
    };

    class Derived2 : public Base
    {
    public:
    virtual GetMember() const { return member_; }
    private:
    static const int member_ = 200;
    };

    If your other existing functions don't need to be virtual, then simply
    don't make them virtual.

    Regards,
    Tonni
    Tonni Tielens, Aug 25, 2008
    #3
  4. Bhawna

    Puppet_Sock Guest

    On Aug 25, 8:50 am, Bhawna <> wrote:
    > I am into c++ code maintenance for last 3-4 years but recently I am
    > put into design phase of a new project. Being a small comapany I dont
    > have enough guidance from seniors.
    >
    > Currently I am into a situation where I am implementing base class
    > functions by including a pointer to subclass member in base class.
    >
    > Reason being functionality is common for subclasses but the members
    > are common within subclass only (static member of subclass) but vary
    > across different subclasses.
    >
    > I am confused is it a godd design decision or there is another
    > alternative to this. If anybody can provide me details of situations
    > where baseclass accesses derived class members I would be better able
    > to justify myself.


    It does not seem like a good design.

    If the base class does not have the functionality, it should
    not try to get it by "borrowing" it from a derived class.

    You should be reading up on virtual functions, as others
    have suggested. You should also be reading up on when to
    use inheritance and how to design a good inheritance tree.

    It's very confusing that you are mentioning static functions
    here. Not clear what you are trying to accomplish.

    There is a very frequent pattern in new programmers. It
    goes like so: New programmer wants to accomplish end
    goal X. Thrashes around and finds that if they could
    do thing L, they could use it to do X. New Prog asks
    how to do L. L is actually very weird, and usually not
    a good thing to do. New Prog gets half-assed answers,
    gets frustrated. Thrashes around, comes up with new
    thing M that would also manage to do X, but is also
    usually a bad practice. Cycle repeats. Everybody gets
    frustrated.

    What is your X? That is, what are you really trying to
    get to? Usually there are good ways to do things, things
    that match up to the standard good patterns in C++.
    Don't ask how to do the L and M things that you have
    thrashed to get to. Ask how to do the final X thing.

    At least ask in the pattern: I'm trying to do X, and
    I'm trying to use L to accomplish it. How?

    > Can anybody refer me a good step by step design guide for C++
    > programming.
    >
    > Any help in this regard will be greatky appreciated.


    There are several good books. Start with "Accelerated C++"
    by Koenig and Moo. Then the "Effective" series
    by Meyers. The C++ FAQ book is quite good. Cruise to

    www.accu.org

    and see some of the book reviews. Read. Enjoy. Later we'll talk.
    Socks
    Puppet_Sock, Aug 25, 2008
    #4
  5. Bhawna

    Bhawna Guest

    Thank you very much for your valuable suggestions. Tonnie's post helps
    me undertsand things better.

    Sock:
    Here is my problem X defined. I hope I am able to clearly define my
    problem now.


    Actually I am converting a set of database tables as classes to
    cutomize query generation for tables specific to my database. Scenario
    is - table is base class and specific tables are subclasses. I want to
    keep table specific details within derived class as static memeber
    because they are all common to whole class. (eg. table name, names of
    columns, no. of columns, foreign key columns etc). But at the same
    time I want to use them within functions specified in base class as
    handling all these members is common for many fucntions so need to be
    implemented in base class. I dont need virtual functions here because
    even if I do so I would copy same code in all derived classes.

    My confusion in this scenario is how to make those derived class
    specific static members available in base class. Or rather then
    choosing myself I would ask what is the best design pattern to map the
    solution of my problem.

    Thanks
    Bhawna




    On Aug 25, 8:30 pm, Puppet_Sock <> wrote:
    > On Aug 25, 8:50 am, Bhawna <> wrote:
    >
    > > I am into c++ code maintenance for last 3-4 years but recently I am
    > > put into design phase of a new project. Being a small comapany I dont
    > > have enough guidance from seniors.

    >
    > > Currently I am into a situation where I am implementing base class
    > > functions by including a pointer to subclass member in base class.

    >
    > > Reason being functionality is common for subclasses but the members
    > > are common within subclass only (static member of subclass) but vary
    > > across different subclasses.

    >
    > > I am confused is it a godd design decision or there is another
    > > alternative to this. If anybody can provide me details of situations
    > > where baseclass accesses derived class members I would be better able
    > > to justify myself.

    >
    > It does not seem like a good design.
    >
    > If the base class does not have the functionality, it should
    > not try to get it by "borrowing" it from a derived class.
    >
    > You should be reading up on virtual functions, as others
    > have suggested. You should also be reading up on when to
    > use inheritance and how to design a good inheritance tree.
    >
    > It's very confusing that you are mentioning static functions
    > here. Not clear what you are trying to accomplish.
    >
    > There is a very frequent pattern in new programmers. It
    > goes like so: New programmer wants to accomplish end
    > goal X. Thrashes around and finds that if they could
    > do thing L, they could use it to do X. New Prog asks
    > how to do L. L is actually very weird, and usually not
    > a good thing to do. New Prog gets half-assed answers,
    > gets frustrated. Thrashes around, comes up with new
    > thing M that would also manage to do X, but is also
    > usually a bad practice. Cycle repeats. Everybody gets
    > frustrated.
    >
    > What is your X? That is, what are you really trying to
    > get to? Usually there are good ways to do things, things
    > that match up to the standard good patterns in C++.
    > Don't ask how to do the L and M things that you have
    > thrashed to get to. Ask how to do the final X thing.
    >
    > At least ask in the pattern: I'm trying to do X, and
    > I'm trying to use L to accomplish it. How?
    >
    > > Can anybody refer me a good step by step design guide for C++
    > > programming.

    >
    > > Any help in this regard will be greatky appreciated.

    >
    > There are several good books. Start with "Accelerated C++"
    > by Koenig and Moo.  Then the "Effective" series
    > by Meyers. The C++ FAQ book is quite good. Cruise to
    >
    > www.accu.org
    >
    > and see some of the book reviews. Read. Enjoy. Later we'll talk.
    > Socks
    Bhawna, Aug 26, 2008
    #5
  6. Bhawna

    Guest

    On Aug 25, 7:50 am, Bhawna <> wrote:
    > I am into c++ code maintenance for last 3-4 years but recently I am
    > put into design phase of a new project. Being a small comapany I dont
    > have enough guidance from seniors.


    Understood. I recommend http://www.parashift.com/c -faq-lite/ as a
    very accessible introduction to C++ design idioms.

    [Yes, reading all of chapters 6-40 is a fairly large exercise. Relax;
    the literary style is better than most novels I've read. Marshall
    Cline does a good job of making even completely familiar material
    pleasant reading.)

    Aside: the proper answer to your question is closely related to the
    answers to Chapter 23 Questions 5-7 of the C++ FAQ Lite.

    > Currently I am into a situation where I am implementing base class
    > functions by including a pointer to subclass member in base class.


    The only way I can visualize this compiling is including, as a data
    member in BaseClass, a BaseClass* _my_real_type member, then using
    some other means to decide which typecasting to use. There is little
    advantage to reimplementing what C++ already gives you: virtual
    functions.

    (Note: general overview of thrashing around in other reply is
    technically correct, but I do understand if a nondisclosure agreement
    prevents mentioning what your ultimate objective is.)

    > Reason being functionality is common for subclasses but the members
    > are common within subclass only (static member of subclass) but vary
    > across different subclasses.


    This makes slightly more sense for static data members of the
    subclass, than static member functions of the subclass.

    While directly reimplementing virtual functions may be useful for self-
    tutoring, it drives up the future maintenance cost of that code
    dramatically. To cooperate with C++:
    * Replace all those static member functions with a virtual function
    that the base class calls, and specializations of that virtual
    function in the subclasses.
    * Likewise, decide what virtual function the base class should use to
    get at the static data items in the subclasses. Then override that
    virtual function in the subclasses.
    * If there are any remaining references to that pointer to a subclass
    instance in your base class, replace their uses with virtual
    functions.
    * Remove that pointer to a subclass instance in your base class.

    > I am confused is it a good design decision or there is another
    > alternative to this. If anybody can provide me details of situations
    > where baseclass accesses derived class members ....


    All such examples should be errors on reasonably modern C++
    implementations. If anything like that *does* slip past the
    implementation, expect undefined behavior. (If you're lucky, the
    program will merely crash.)
    , Aug 26, 2008
    #6
  7. Bhawna

    Guest

    On Aug 26, 1:35 am, Bhawna <> wrote:
    > Thank you very much for your valuable suggestions. Tonnie's post helps
    > me undertsand things better.
    >
    > Sock:
    > Here is my problem X defined. I hope I am able to clearly define my
    > problem now.
    >
    > Actually I am converting a set of database tables as classes to
    > cutomize query generation for tables specific to my database. Scenario
    > is - table is base class and specific tables are subclasses. I want to
    > keep table specific details within derived class as static memeber
    > because they are all common to whole class. (eg. table name, names of
    > columns, no. of columns, foreign key columns etc). But at the same
    > time I want to use them within functions specified in base class as
    > handling all these members is common for many fucntions so need to be
    > implemented in base class. I dont need virtual functions here because
    > even if I do so I would copy same code in all derived classes.


    For this particular task, agreed: I would first try a non-virtual
    function in the base class.

    > My confusion in this scenario is how to make those derived class
    > specific static members available in base class.


    I would first try using virtual functions to relay the static data to
    the non-virtual function in the base class.

    As a matter of strategy:
    * There are open-source libraries I would consider for accessing a
    database in a known format, for or embedding an SQL transactional
    database engine in a C++ program.
    * I don't have a clear idea why subclassing the individual tables
    could be the most maintainable approach. Wouldn't an enumeration
    providing human-readable indexes into a const static member-of-base-
    class array of reference data, combined with a size_t member to index
    which table to use, be enough?
    , Aug 26, 2008
    #7
  8. Bhawna

    Bhawna Guest

    On Aug 26, 1:04 pm, wrote:
    > On Aug 26, 1:35 am, Bhawna <> wrote:
    >
    >
    >
    >
    >
    > > Thank you very much for your valuable suggestions. Tonnie's post helps
    > > me undertsand things better.

    >
    > > Sock:
    > > Here is my problem X defined. I hope I am able to clearly define my
    > > problem now.

    >
    > > Actually I am converting a set of database tables as classes to
    > > cutomize query generation for tables specific to my database. Scenario
    > > is - table is base class and specific tables are subclasses. I want to
    > > keep table specific details within derived class as static memeber
    > > because they are all common to whole class. (eg. table name, names of
    > > columns, no. of columns, foreign key columns etc). But at the same
    > > time I want to use them within functions specified in base class as
    > > handling all these members is common for many fucntions so need to be
    > > implemented in base class. I dont need virtual functions here because
    > > even if I do so I would copy same code in all derived classes.

    >
    > For this particular task, agreed: I would first try a non-virtual
    > function in the base class.
    >
    > > My confusion in this scenario is how to make those derived class
    > > specific static members available in base class.

    >
    > I would first try using virtual functions to relay the static data to
    > the non-virtual function in the base class.
    >
    > As a matter of strategy:
    > * There are open-source libraries I would consider for accessing a
    > database in a known format, for or embedding an SQL transactional
    > database engine in a C++ program.
    > * I don't have a clear idea why subclassing the individual tables
    > could be the most maintainable approach.  Wouldn't an enumeration
    > providing human-readable indexes into a const static member-of-base-
    > class array of reference data, combined with a size_t member to index
    > which table to use, be enough?- Hide quoted text -
    >
    > - Show quoted text -


    Thank you very much. All that stuff was really very helpful.
    Bhawna, Aug 26, 2008
    #8
    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. John Harrison
    Replies:
    0
    Views:
    482
    John Harrison
    Aug 6, 2003
  2. Tim Clacy
    Replies:
    3
    Views:
    579
    Nick Hounsome
    Mar 4, 2004
  3. flopbucket
    Replies:
    2
    Views:
    413
    Andrey Tarasevich
    Jun 23, 2006
  4. Replies:
    1
    Views:
    383
    myork
    May 23, 2007
  5. Replies:
    1
    Views:
    371
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page