justCurious
justCurious

Reputation: 977

How to keep alive an object for later use in another function?

I have a base class called Mijloc_transport and 2 derived classes called Masina and Tramvai. I must do an menu type program that can create different kind of objects from the derived classes and show them. I'm using the UPCASTING method and when I press 1 the if(tasta=="1") creates an object and I can see what type it is using: tabp[0]->show();. It seems that when the program enters again in the do loop (using the main label), the object created as 1 is destroyed and I can't see tabp[0]->show(); because the reference is deleted. What shall I do to keep the objects alive so I can put them in tabp?

Masina b(30);
p=&b;
tabp[i]=p;

so later I can display them....10x for your help

#include "include.hpp"
#include <ctype.h>
#include <cstdlib>

int main(void)
{    

    int i=0;

    Mijloc_transport *tabp[MAX];
    Mijloc_transport *p;

    int tasta;
main:
    do
    {
        //cout<<"\n\n\n\n\n";
        cout<<endl<<"apasa orice tasta pentru a reveni la meniu "<<endl; getch();
        system("cls");
        cout<<"Tasteaza numarul optiunii dorite din meniu:"<<endl; 
        cout<<"1. creaza o masina cu 30 cai putere"<<endl;      //create an Masina derived Oject
        cout<<"2. creaza un tramvai cu 20 de locuri"<<endl;//create an Tramvai derived Oject
        cout<<"3. afiseaza memoria"<<endl;                       //print the objects
        cout<<"4. iesire"<<endl;
        tasta=cin.get();
    }
    while(tasta!='1' && tasta!='2'&& tasta!='3'&& tasta!='4');


    if(tasta=='1')
    {
        Masina b(30);
        p=&b;
        tabp[i]=p;

        tabp[0]->show();

        cout<<endl<<"apasa orice tasta pentru a reveni la meniu "<<endl;
        getch();

        //i++;            //TREBUIE INCREMENTAT cand ai mai mult de un obiect
        goto main;
    }

    if(tasta=='3')
    {
        //afiseaza elementele din memorie
        //for(int j=0;j<=i;j++)                      //J<=i
        //tabp[j]->show();

        tabp[0]->show();
        cout<<endl<<"apasa orice tasta pentru a reveni la meniu "<<endl;
        getch();

        return 1;
    }
}

Upvotes: 0

Views: 3034

Answers (4)

ds27680
ds27680

Reputation: 1993

Instead of:

Masina b(30);
p=&b;
tabp[i]=p;

do:

tabp[i] = new Masina(30);

and do not forget to do:

for ( unsigned int i = 0; i < MAX; ++i )
{
    delete tabp[i];
}

When you no longer need tabp (before existing main).

Upvotes: 1

log0
log0

Reputation: 10947

tabp[i] = new Masina(30);

but please... Each time you are using goto (especially like this) God kill a kitten.

Upvotes: 1

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81734

Instead of creating the object on the stack, like this:

Masina b(30);

you must create it dynamically, like this:

Masina *b = new Masina(30);

and furthermore, you should declare Masina *b somewhere outside the loop, so that it persists longer than the time you're in the loop.

Upvotes: 3

Jordan
Jordan

Reputation: 5058

Its hard to read your code, but based on your title I am guessing you want to create an object on the heap using the new keyword. This will allow you to create an object on the heap (as opposed to the stack) which will stay alive after a function exits, be warned you must clean up the memory associated with the allocation of new.

Upvotes: 1

Related Questions