www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/01/19/09:47:51

From: ian_dunbar AT my-dejanews DOT com
Newsgroups: comp.os.msdos.djgpp
Subject: Re: virtual problems (yes, the same ones)
Date: Tue, 19 Jan 1999 14:33:24 GMT
Organization: Deja News - The Leader in Internet Discussion
Lines: 44
Message-ID: <78253k$vh6$1@nnrp1.dejanews.com>
References: <781qjq$3l6$1 AT fafnir DOT cf DOT ac DOT uk>
NNTP-Posting-Host: 193.120.210.2
X-Article-Creation-Date: Tue Jan 19 14:33:24 1999 GMT
X-Http-User-Agent: Mozilla/4.05 [en] (WinNT; I)
X-Http-Proxy: 1.0 ns.euristix.ie:80 (Squid/1.NOVM.20), 1.0 x12.dejanews.com:80 (Squid/1.1.22) for client unknown, 193.120.210.2
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

In article <781qjq$3l6$1 AT fafnir DOT cf DOT ac DOT uk>,
  "mpt" <M DOT P DOT Tidball AT cs DOT cf DOT ac DOT uk> wrote:
> Here it is again, folks, now satisfying the definition problem in the base
> class (love those curly braces).
>
> The definitions in the derived classes are not being picked up at run time.
> Why not?

[ snip ]

>  vector < Thing > thingVec;

My guess is that your problem is in the above line. I'm not all that familiar
with the STL, but I'd say that the implementation of vector actually copies
anything that you push_back () using either the BASE CLASS copy constructor,
or maybe the BASE CLASS operator = (). So what is stored in thingVec[0] is a
"Thing" not a "Thing_a". The base class members of thingVec[0] will be copied
from "a", but it will have none of the extra members (including virtual
functions) that are in "a".

What you probably want to do is to declare thingVec like this:

    vector <Thing *> thingVec;

And then go:

    thingVec.push_back (&a);
    thingVec.push_back (&b);

and obviously access the vetor members like a pointer:

    int number = thingVec[ 0 ]->number();

Another, more complex approach would be to have some sort of container class
for a single "Thing" that when copied would call a "virtual Thing *copy ()"
function that would just return a new instance of whatever type the object
happened to be. You could override the operator-> in the container to allow
access to the contained "Thing".

Hope that helps.
Ian.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019