
Inherited class function calls from a pointer assigned to the base class
Where to begin, hmm, ... Let's start at the bottom.
Quote:
> 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.
HTH
Quote:
> 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
http://www.*-*-*.com/