Mail Archives: djgpp/2002/06/14/13:50:22

From: Nick <jp_prongs AT yahoo DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Vector of Classes issue in GCC 3.1
Date: Fri, 14 Jun 2002 10:13:13 -0400
Organization: MindSpring Enterprises
Lines: 29
Message-ID: <>
References: <3cf6c05e_1 AT news DOT iprimus DOT com DOT au> <3cf6d9d1$1_1 AT news DOT iprimus DOT com DOT au> <5P2K8.30975$4f4 DOT 1197748 AT news000 DOT worldonline DOT dk> <5576bd2c DOT 0206111609 DOT 2facd787 AT posting DOT google DOT com> <3D078123 DOT 98D37812 AT yahoo DOT com> <3d07d086_1 AT news DOT iprimus DOT com DOT au>
NNTP-Posting-Host: a5.f7.9f.2d
Mime-Version: 1.0
X-Server-Date: 14 Jun 2002 14:13:19 GMT
X-Mailer: Mozilla 4.75 [en] (Win98; U)
X-Accept-Language: en
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Jason wrote:
> So, when I push an instance of my class onto a vector container, is it just
> creating a pointer to a global class instance pointer?  If I create a series
> of class instances manually (ie, CBoard Board1; CBoard Board2; ; ; CBoard
> Board x) a totally independant class group is created, and there is no
> dramas.  I might have to look a bit further into what goes on when I push a
> class instance onto a vector container (as this is what I want, without the
> confusing code!)  I just want to be able to have a run-time-defined number
> of a particular class, where I can access member functions of each class
> instance independantly like an array.
> Jase.

No, vectors do not store pointers unless you push pointers. If you push on a raw instance, ie:

class foo;
vector<foo> v;
foo f;

a new foo will be created (using the copy constructor, foo(foo&), based on f), and pushed on the
vector (not a pointer - the entire object). If f contains a pointer to a memory area, and foo's copy
constructor does not create a new memory area and copy to there, then both objects (f and the one in
the vector) will have the same pointer internally. When one is destroyed, and it frees that area,
the other will be referencing a freed memory region - very bad. Just give your objects proper copy
constructors, and it should work fine.

- Raw text -

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