By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,827 Members | 2,177 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

Accessing derived class members from base class

P: n/a
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
Aug 25 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Aug 25, 3:11*pm, Bhawna <bvnbh...@gmail.comwrote:
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
Aug 25 '08 #2

P: n/a
On Aug 25, 8:50*am, Bhawna <bvnbh...@gmail.comwrote:
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
Aug 25 '08 #3

P: n/a
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 <puppet_s...@hotmail.comwrote:
On Aug 25, 8:50*am, Bhawna <bvnbh...@gmail.comwrote:
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
Aug 26 '08 #4

P: n/a
On Aug 25, 7:50 am, Bhawna <bvnbh...@gmail.comwrote:
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 '08 #5

P: n/a
On Aug 26, 1:35 am, Bhawna <bvnbh...@gmail.comwrote:
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 '08 #6

P: n/a
On Aug 26, 1:04*pm, zaim...@zaimoni.com wrote:
On Aug 26, 1:35 am, Bhawna <bvnbh...@gmail.comwrote:


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.
Aug 26 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.