Inherited class function calls from a pointer assigned to the base class 
Author Message
 Inherited class function calls from a pointer assigned to the base class

Where to begin, hmm, ...  Let's start at the bottom.


>  HIGHLY recommend NEVER buying "The C++ Programming Language" by
>  Bjarne Stoustrup.  It looks thorough, but isn't.

Gotta disagree.  It may not be the easiest read or the best book for
learning C++, but it's THE book for resolving bets (unless you need the
latest draft standard...)

The primary advantage to using inheritance is to factor out
commonalities among classes.  Your base class should have only those
attributes and member functions common to all its derived classes.  If
the only similarity between all your Dummy-derived classes is that they
can be contained in a list, and User was the only class among
Dummy-derivatives with a UserName, it wouldn't make sense to declare a
virtual function GetUserName in Dummy.  However, it then also wouldn't
make sense to try to call GetUserName through a pointer to Dummy!  And
as you've discovered, the compiler won't let you if you try.  

If User is derived from Dummy, you can use a User anywhere a Dummy is
needed.  Think about inheritance as an "is-a" relationship:  A User is-a
Dummy, but a Dummy is not necessarily a User.  In your code, you're
always creating a User object.  Why not just declare "User * newuser"
instead of making it a Dummy*?  If Insert() takes a Dummy*, a User* will
do just as well.

You might also want to consider template lists instead of using a class
that requires that all list elements be derived from Dummy.

I should also point out that your function leaks memory in the case
where the name is already in the list:  you're returning from the
function without putting newuser in the list nor deleting it.

Lastly, what book had the shape/myshape example you cited?  You might
want to review it again.  It's the example everyone learns, and the base
class always has (pure) virtual functions declaring common behaviors.


>  I am writing a program that uses a couple of inherited classes.  I have a
>  base List class which is a linked list of Dummy classes, which contain
>  nothing.  I am trying to make a class that is inherited from the list
>  class, that is a list composed of a class inherited from the Dummy class.
>   The class inherited from Dummy has various functions that Dummy does not
> have (obviously, since dummy has no members).  here is where I am getting
> problems:

> int Userlist::AddUser(char* name)
>  {
>             Dummy *newuser = new User;

>                 newuser->SetName(name);

>                 Reset();
>         while (GetCurrent() != NULL)
>         {

> // line A       if(strcmp(GetCurrent()->GetUserName(), name) == 0)
>                         return 0;

> //line B                if(strcmp(GetCurrent()->GetUserName(), name) > 0)
>                 {
>                 Insert(*newuser);
>                 return 1;
>                 }
>         }
>         Insert(*newuser);
>         return 1;
> }

>  this function is
>  part of a list of users at an isp.  it creates a pointer to a Dummy, and
>  assigns a new User (a class derived from Dummy) to it.  this is a
>  perfectly normal thing to do with inherited classes, and it is precisely
>  why i created a linked list of Dummy classes with no members-- so that i
>  wouldnt have to change the code every time i wanted to make a list of
>  something else, i could just inherit the new class from Dummy.

>  the problem is that when i try to access a function from the User class
> (lines A and B-- current is a pointer to a Dummy that is assigned a
>  pointer to a User.  GetCurrent() returns that pointer), it gives an error
>  message saying that the function GetUserName() isnt from the Dummy class.

>  Well, of course it isnt.  but it is a member of the User class.  and
>  since the current pointer will always be a Dummy pointer that has had a
>  User pointer assigned to it (perfectly normal practice)  there shouldnt
>  be a problem since current will always have that function as a member of
>  what it is pointing to.

>  I can use a sloppy fix, casting the current pointer as a User*, but I
>  will have to do that EVERY time I try to access a function from the User
>  class when a Dummy* is pointing to it.  or i could put virtual functions
>  into the Dummy class, but that defeats the whole purpose of the class--
>  preventing me from changing the Dummy code every time i use the list to
>  hold a different class.  I have this royally crappy C++ book that doesnt
>  help me, in fact it shows an example of the same thing, and doesnt say
>  anything is wrong with it:

>  int main()
>  {
>      shape* p1 = new myshape;

>      p3->move(10, -10);

>      return 0;
>  }

>  move() is a member of myshape, NOT shape.  shape doesnt have a virtual
>  function called move or anything.  Either this book was written by people
>  as inept as they are unhelpful, or i am doing something wrong, or MSVC++
> 5.0
>  (which i am using) is being picky.

>  I would GREATLY appreciate any ideas on what I might be doing wrong, or
>  how it can work easier.

-- Aaron
Aaron J Margosis


Thu, 11 Nov 1999 03:00:00 GMT  
 Inherited class function calls from a pointer assigned to the base class

First I would like to thank you very much for your help.  I hadn't noticed
that memory leak, and the suggestion to simply declare newuser as a User*
worked (I had assumed it wouldn't probably in some mixup of the is-a
relationship.  I havent coded in a year and am refreshing my memory).  The
example was from the mentioned book, however I re-read the whole chapter
instead of skimming it and found that the shape class DID have a virtual
move function.  

        Thanks again, I appreciate your help very much.

                        Rob Tomlinson

Sat, 13 Nov 1999 03:00:00 GMT  
 [ 2 post ] 

 Relevant Pages 

1. VC++ class inheriting C# class loses base functions

2. problems creating a object of a inherited class through base class

3. how to convert base class pointer to sub class pointer

4. Compiler error on call to base class function

5. Calling a base class virtual function

6. Calling Functions of the Base Class?

7. How to prevent derived classes from changing a base class function

8. virtual function - calling derived version from base class

9. Calling overloaded function in base from another overload in a derived class

10. Call virtual function in base class

11. Calling a base classes member function

12. How to make call to base class function == a compile error (restated)


Powered by phpBB® Forum Software