C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

inheritance ideas

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Mike Frayn
Guest





PostPosted: Sat Mar 06, 2004 10:52 pm    Post subject: inheritance ideas Reply with quote



Hi there. I'm wondering how to achieve a particular
functionality using virtual functions and class
inheritance... I don't know if its possible, but I figured
that I would give it a shot.

What I want is to have a base class with particular member
data and a virtual function that will be overloaded by the
inheriting class.

class CBaseClass
{
public:
virtual void OverLoadMe(void) {}
int DataFieldA;
};

In the inherited class, I will overload the function but I
would like to introduce new member data:

class CInheritedClassA : public CBaseClass
{
void OverLoadMe(void) { //happy now? }
float DataFieldB;
};

Now if I defined a variable like so:

CBaseClass* MyClass = new CInheritedClassA;

I would like to be able to access DataFieldB. Of course, I
can't, however the reason I'm looking for such functionality
is so that I can house a group of inheritedclassA-Z
instances in one list without having to have separate lists
for separate types.

So, while I do realize that this doesn't work, and
furthermore I also realize WHY this doesn't and shouldn't
work, I'm wondering if I can get around it without
COMPLETELY changing the design of what I have so far.

Thanks so much,
Mike


Back to top
Victor Bazarov
Guest





PostPosted: Sat Mar 06, 2004 11:02 pm    Post subject: Re: inheritance ideas Reply with quote



"Mike Frayn" <redcrocodile (AT) hotmail (DOT) com> wrote...
Quote:
Hi there. I'm wondering how to achieve a particular
functionality using virtual functions and class
inheritance... I don't know if its possible, but I figured
that I would give it a shot.

What I want is to have a base class with particular member
data and a virtual function that will be overloaded by the
inheriting class.

class CBaseClass
{
public:
virtual void OverLoadMe(void) {}
int DataFieldA;
};

In the inherited class, I will overload the function but I
would like to introduce new member data:

class CInheritedClassA : public CBaseClass
{
void OverLoadMe(void) { //happy now? }
float DataFieldB;
};

Now if I defined a variable like so:

CBaseClass* MyClass = new CInheritedClassA;

I would like to be able to access DataFieldB.

So, why not have a pure virtual function in CBaseClass that would
"access" your derived class data member? Why do you need to access
it, anyway?

Quote:
Of course, I
can't, however the reason I'm looking for such functionality
is so that I can house a group of inheritedclassA-Z
instances in one list without having to have separate lists
for separate types.

So, the ability to access data members specific to derived classes
has really nothing to do with the reasons for which you created your
hierarchy, right? Then why are you trying to use the base class for
something it wasn't created to do?

Quote:
So, while I do realize that this doesn't work, and
furthermore I also realize WHY this doesn't and shouldn't
work, I'm wondering if I can get around it without
COMPLETELY changing the design of what I have so far.

There is no reason to completely change the design. However, there
is a strong need for you to realise that WRT accessing data members
you have _no_design_ whatsoever, so there is nothing to change. If
you uncover your secret why you need that data (and public too), we
could probably suggest a couple of solutions.

V



Back to top
Thomas Matthews
Guest





PostPosted: Sun Mar 07, 2004 12:33 am    Post subject: Re: inheritance ideas Reply with quote



Mike Frayn wrote:
Quote:
Hi there. I'm wondering how to achieve a particular
functionality using virtual functions and class
inheritance... I don't know if its possible, but I figured
that I would give it a shot.
[snip]
class CBaseClass
{
public:
virtual void OverLoadMe(void) {}
int DataFieldA;
};
[snip]


Quote:
class CInheritedClassA : public CBaseClass
{
void OverLoadMe(void) { //happy now? }
float DataFieldB;
};
[snip]



Quote:
Thanks so much,
Mike

By the way, there is no reason to prefix all of
your classes with the letter 'C'. This is a
Microsoft naming convention.

See:
http://www.jelovic.com/articles/stupid_naming.htm


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library


Back to top
Cy Edmunds
Guest





PostPosted: Sun Mar 07, 2004 3:05 am    Post subject: Re: inheritance ideas Reply with quote

"Mike Frayn" <redcrocodile (AT) hotmail (DOT) com> wrote

[snip]
Quote:

I would like to be able to access DataFieldB. Of course, I
can't, however the reason I'm looking for such functionality
is so that I can house a group of inheritedclassA-Z
instances in one list without having to have separate lists
for separate types.


You might have a look at boost::any at

www.boost.org

--
Cy
http://home.rochester.rr.com/cyhome/



Back to top
Victor Bazarov
Guest





PostPosted: Sun Mar 07, 2004 3:25 am    Post subject: Re: inheritance ideas Reply with quote

"Thomas Matthews" <Thomas_MatthewsSpamBotsSuck (AT) sbcglobal (DOT) net> wrote...
Quote:
[...]
By the way, there is no reason to prefix all of
your classes with the letter 'C'. This is a
Microsoft naming convention.

Actually, it's not just Microsoft. You'd be surprised how many other
software companies have their own naming conventions that look similar
and must seem unnecessary. Of course, Microsoft gets bashed because
they published theirs...

V



Back to top
Daniel T.
Guest





PostPosted: Sun Mar 07, 2004 6:45 am    Post subject: Re: inheritance ideas Reply with quote

In article <404a5639$0$227$fa0fcedb (AT) lovejoy (DOT) zen.co.uk>,
"Mike Frayn" <redcrocodile (AT) hotmail (DOT) com> wrote:

Quote:
Hi there. I'm wondering how to achieve a particular
functionality using virtual functions and class
inheritance... I don't know if its possible, but I figured
that I would give it a shot.

What I want is to have a base class with particular member
data and a virtual function that will be overloaded by the
inheriting class.

class CBaseClass
{
public:
virtual void OverLoadMe(void) {}
int DataFieldA;
};

In the inherited class, I will overload the function but I
would like to introduce new member data:

class CInheritedClassA : public CBaseClass
{
void OverLoadMe(void) { //happy now? }
float DataFieldB;
};

Now if I defined a variable like so:

CBaseClass* MyClass = new CInheritedClassA;

I would like to be able to access DataFieldB. Of course, I
can't, however the reason I'm looking for such functionality
is so that I can house a group of inheritedclassA-Z
instances in one list without having to have separate lists
for separate types.

So, while I do realize that this doesn't work, and
furthermore I also realize WHY this doesn't and shouldn't
work, I'm wondering if I can get around it without
COMPLETELY changing the design of what I have so far.

void dealWithDataFieldB( CBaseClass* bc ) {
CInheritedClassA* ic = dynamic_cast< CInheritedClassA >( bc );
assert( ic );
ic->DataFieldB = 5;
}

Note the assert. I would only recommend using this in cases where you
know that the object in question is going to be a CInheritedClassA
object *every* time this piece of code is run. IMHO of course.

Back to top
John Harrison
Guest





PostPosted: Sun Mar 07, 2004 7:15 am    Post subject: Re: inheritance ideas Reply with quote


"Mike Frayn" <redcrocodile (AT) hotmail (DOT) com> wrote

Quote:
Hi there. I'm wondering how to achieve a particular
functionality using virtual functions and class
inheritance... I don't know if its possible, but I figured
that I would give it a shot.

What I want is to have a base class with particular member
data and a virtual function that will be overloaded by the
inheriting class.

class CBaseClass
{
public:
virtual void OverLoadMe(void) {}
int DataFieldA;
};

In the inherited class, I will overload the function but I
would like to introduce new member data:

class CInheritedClassA : public CBaseClass
{
void OverLoadMe(void) { //happy now? }
float DataFieldB;
};

Now if I defined a variable like so:

CBaseClass* MyClass = new CInheritedClassA;

I would like to be able to access DataFieldB. Of course, I
can't, however the reason I'm looking for such functionality
is so that I can house a group of inheritedclassA-Z
instances in one list without having to have separate lists
for separate types.

So, while I do realize that this doesn't work, and
furthermore I also realize WHY this doesn't and shouldn't
work, I'm wondering if I can get around it without
COMPLETELY changing the design of what I have so far.

Thanks so much,
Mike


Just define virtual functions which access the data members you want.

You shouldn't be defining public data anyway, that's rule 1 of OO design.

john



Back to top
Mike Frayn
Guest





PostPosted: Sun Mar 07, 2004 12:49 pm    Post subject: Re: inheritance ideas Reply with quote

Quote:
By the way, there is no reason to prefix all of
your classes with the letter 'C'. This is a
Microsoft naming convention.

I disagree, I believe there is a reason and that's why I use
it.

Mike



Back to top
Mike Frayn
Guest





PostPosted: Sun Mar 07, 2004 12:49 pm    Post subject: Re: inheritance ideas Reply with quote

Aha! Thanks Dan!

Mike

"Daniel T." <postmaster (AT) eathlink (DOT) net> wrote

Quote:
In article <404a5639$0$227$fa0fcedb (AT) lovejoy (DOT) zen.co.uk>,
"Mike Frayn" <redcrocodile (AT) hotmail (DOT) com> wrote:

Hi there. I'm wondering how to achieve a particular
functionality using virtual functions and class
inheritance... I don't know if its possible, but I
figured
that I would give it a shot.

What I want is to have a base class with particular
member
data and a virtual function that will be overloaded by
the
inheriting class.

class CBaseClass
{
public:
virtual void OverLoadMe(void) {}
int DataFieldA;
};

In the inherited class, I will overload the function but
I
would like to introduce new member data:

class CInheritedClassA : public CBaseClass
{
void OverLoadMe(void) { //happy now? }
float DataFieldB;
};

Now if I defined a variable like so:

CBaseClass* MyClass = new CInheritedClassA;

I would like to be able to access DataFieldB. Of
course, I
can't, however the reason I'm looking for such
functionality
is so that I can house a group of inheritedclassA-Z
instances in one list without having to have separate
lists
for separate types.

So, while I do realize that this doesn't work, and
furthermore I also realize WHY this doesn't and
shouldn't
work, I'm wondering if I can get around it without
COMPLETELY changing the design of what I have so far.

void dealWithDataFieldB( CBaseClass* bc ) {
CInheritedClassA* ic = dynamic_cast< CInheritedClassA
( bc );
assert( ic );
ic->DataFieldB = 5;
}

Note the assert. I would only recommend using this in
cases where you
know that the object in question is going to be a
CInheritedClassA
object *every* time this piece of code is run. IMHO of
course.




Back to top
Mike Frayn
Guest





PostPosted: Sun Mar 07, 2004 12:52 pm    Post subject: Re: inheritance ideas Reply with quote

Quote:
Just define virtual functions which access the data
members you want.


Hmmm, but then I may as well have placed all the data
members in the base class. No? Seems to me that they'd
both look equally unattractive.

Quote:
You shouldn't be defining public data anyway, that's rule
1 of OO design.


I should have figured I'd receive grief for that. It was
irrelevant to my example so I didn't bother.

Mike



Back to top
John Harrison
Guest





PostPosted: Sun Mar 07, 2004 12:57 pm    Post subject: Re: inheritance ideas Reply with quote


"Mike Frayn" <redcrocodile (AT) hotmail (DOT) com> wrote

Quote:
Just define virtual functions which access the data
members you want.

Hmmm, but then I may as well have placed all the data
members in the base class. No? Seems to me that they'd
both look equally unattractive.

If all derived classes (now and in the future) share the data members, then
put them into the base class. If they don't then don't. I don't really see
the problem.

There's nothing to stop you putting an error catching virtual method in the
base class.

class Base
{
public:
virtual int getA() const { throw "no A exists in Base"; }
};

class Derived : public Base
{
public:
virtual int getA() const { return a; }
private:
int a;
};

john



Back to top
Mike Frayn
Guest





PostPosted: Sun Mar 07, 2004 1:01 pm    Post subject: Re: inheritance ideas Reply with quote

Okay John, you seem to be approaching this as if I already
understand what the right answer is. What's the problem?
The problem is that I don't understand :)

Uhm, my point was simply that if I'm going to add a virtual
function for every data member that is added in a derived
class, then I'll have a bunch of virtual functions in the
base class, some of which get used in derivations from it
and some that don't. That seems to be the same, to me, as
simply defining all the member variables in the base class
and then only using some of them in each particular derived
class.

Mike

"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote in
message news:c2f67o$1s1trj$1 (AT) ID-196037 (DOT) news.uni-berlin.de...
Quote:

"Mike Frayn" <redcrocodile (AT) hotmail (DOT) com> wrote in message
news:404b1b10$0$225$fa0fcedb (AT) lovejoy (DOT) zen.co.uk...
Just define virtual functions which access the data
members you want.

Hmmm, but then I may as well have placed all the data
members in the base class. No? Seems to me that they'd
both look equally unattractive.

If all derived classes (now and in the future) share the
data members, then
put them into the base class. If they don't then don't. I
don't really see
the problem.

There's nothing to stop you putting an error catching
virtual method in the
base class.

class Base
{
public:
virtual int getA() const { throw "no A exists in
Base"; }
};

class Derived : public Base
{
public:
virtual int getA() const { return a; }
private:
int a;
};

john





Back to top
John Harrison
Guest





PostPosted: Sun Mar 07, 2004 1:10 pm    Post subject: Re: inheritance ideas Reply with quote


"Mike Frayn" <redcrocodile (AT) hotmail (DOT) com> wrote

Quote:
Okay John, you seem to be approaching this as if I already
understand what the right answer is. What's the problem?
The problem is that I don't understand :)

Uhm, my point was simply that if I'm going to add a virtual
function for every data member that is added in a derived
class, then I'll have a bunch of virtual functions in the
base class, some of which get used in derivations from it
and some that don't. That seems to be the same, to me, as
simply defining all the member variables in the base class
and then only using some of them in each particular derived
class.

Mike


OK well now I understand. Two advantages of using virtual functions are

1) You get error checking, if a derived class tries to access a variable it
doesn't have then the base class virtual function can detect this (as in my
last post)
2) You save space, only the derived classes that have member variables will
actually have them, instead of all derived classes having them by virtue of
there being in the base class.

I think your approach to the design of a inheritance tree is a little
backward. You say 'if I'm going to add a virtual for every data member that
is added in a derived class' but really when you are designing this you
should be thinking of *all* the functionality you need in the base class.
Derived classes do not add functionality to this, they implement the base
class functionality in different specialised ways. At least that what the
books say, real world is sometimes a little more complex.

john



Back to top
Daniel T.
Guest





PostPosted: Sun Mar 07, 2004 6:19 pm    Post subject: Re: inheritance ideas Reply with quote

"Mike Frayn" <redcrocodile (AT) hotmail (DOT) com> wrote:

Quote:
Just define virtual functions which access the data
members you want.

Hmmm, but then I may as well have placed all the data
members in the base class. No? Seems to me that they'd
both look equally unattractive.

Putting *any* member-data in a base class is unattractive AFAIC.

Quote:
You shouldn't be defining public data anyway, that's rule
1 of OO design.

I should have figured I'd receive grief for that. It was
irrelevant to my example so I didn't bother.

Yea, I figured you were just trying to simplify the example. In the
future use member functions instead:

class Base {
public:
void fn1();
};

class Derived: public Base {
public:
void fn2();
};

void problem( Base& b ) {
// note: the same problem exists... how do we
// access fn2 when all we have a a Base&?
dynamic_cast<Derived>(b).fn2();
// The above will throw if b isn't a a Derived object.
}

The above is poor programming practice, but it does solve the problem
you are having.

It is poor programming practice because the code that calls problem()
(or the programmer writing that code) must *know* that the object is
actually a Derived object and not some other class derived from Base (or
a Base object itself,) yet that knowledge won't be exposed in the code,
because a Base pointer/reference is being thrown around, instead of a
Derived pointer/reference.

Back to top
Daniel T.
Guest





PostPosted: Sun Mar 07, 2004 6:24 pm    Post subject: Re: inheritance ideas Reply with quote

"Mike Frayn" <redcrocodile (AT) hotmail (DOT) com> wrote:

Quote:
By the way, there is no reason to prefix all of
your classes with the letter 'C'. This is a
Microsoft naming convention.

I disagree, I believe there is a reason and that's why I use
it.

Good for you! It's good to know why we do things. I think you would
agree though that there is no 'language' reason to use the 'C' prefix.
Personally, I think the prefix adds unnecessary complexity to the bit of
code presented, even if it is useful to you in your project as a whole.
Just MHO of course.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.