🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: att.lang.c++
Article #1

Help with STL set container - Assigning a set of dervied to a set of base

#1
From: BL0314500-S.RAVIKUMAR(HOH167)NON...
Date: Fri, 12 Apr 1996 20:08
52 lines
1415 bytes
The following program results in a memory fault at the assignment
point. Can someone show me how to fix it. BTW, how to implement set of
uninterpreted pointers in STL.

Thanks.

-- 
ravi
ssr@hoserve.att.com

#include <stl_set.h>
#include <algo.h>
#include <assert.h>
#include <string.h>

struct Base
{
  char _id[3];
  Base(const char* id) { strcpy(_id, id); }
};

struct Derived : public Base
{
  char _name[3];
  Derived(const char* id, const char* name) : Base(id) { strcpy(_name, name); }
};


int
main()
{
  set< Base*, less<Base*> > bs;
  bs.insert(new Base("b1"));
  bs.insert(new Base("b2"));

  set< Derived*, less<Derived*> > ds;
  ds.insert(new Derived("BB", "d1"));
  ds.insert(new Derived("BB", "d2"));

  cout << "Assigning derived set to base set" << endl;

  //  >>>>>>>>> memory fault line >>>>>>>>>>
  set< Base*, less<Base*> > bs1 = (set< Base*, less<Base*> >&)ds;

  cout << "Assignment succeeded" << endl;

  exit(0);
}
--
ravi
ssr@hoserve.att.com
Technical Details
Message-ID: <SSR.96Apr12160838@qc56.ho.att.com>
Path: arc1-ca.pugleaf.net!nntpa.cb.att.com!ssr

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!nntpa.cb.att.com!ssr
From: ssr@qc56.ho.att.com (BL0314500-S.RAVIKUMAR(HOH167)NONE)
Newsgroups: att.lang.c++,comp.lang.c++
Subject: Help with STL set container - Assigning a set of dervied to a set of base
Date: 12 Apr 1996 20:08:38 GMT
Organization: AT&T Bell Laboratories
Distribution: world
Message-ID: <SSR.96Apr12160838@qc56.ho.att.com>
NNTP-Posting-Host: qc56.ho.att.com