S
S
Slavka2014-04-19 19:52:03
C++ / C#
Slavka, 2014-04-19 19:52:03

Change object in std::vevtor to another type

What is the point, there is a vector of pointers to the base class std::vector Mas;
this is how it fills up

void __fastcall TForm2::Button1Click(TObject *Sender)
{
  String name=Edit2->Text;

if (RadioGroup2->ItemIndex==0) {
  CPointIndic *tmp =new CPointIndic;
  tmp->setName(name);

  Mas.push_back(tmp);
  Form1->updateList(Form1->List);
}

if (RadioGroup2->ItemIndex==1) {
  CMenuIndic *tmp =new CMenuIndic;
  tmp->setName(name);

  Mas.push_back(tmp);
  Form1->updateList(Form1->List);
}

if (RadioGroup2->ItemIndex==2) {
  CCircleIndic *tmp =new CCircleIndic;
  tmp->setName(name);

  Mas.push_back(tmp);
  Form1->updateList(Form1->List);
}


Close();
}

now you need to change the types of objects
like this, I change it, but it does not want to compile, help me figure out this mechanism
int index = List->ItemIndex;

if (index>=0) {

  String name = Mas[index]->getName();
  int n = StrToInt(InputBox("","",""));

  switch(n)
  {
    case 1 :
    delete Mas[index];
    CPointIndic *tmp = new CPointIndic;
    Mas.insert(index+1,tmp);
    break;
  }


}

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tsarevfs, 2014-04-19
@tsarevfs

Insert, for example, cannot insert by position number.

typedef std::vector<int>::iterator vector_iter;
vector_iter iter = Mas.begin() + (index + 1);
Mas.insert(iter, tmp);

X
XO8964, 2014-04-21
@XO8964

Everything is much easier

CPointIndic *tmp = Mas[index];
delete tmp;
tmp = new CPointIndic;
Mas[index] = tmp;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question