class First{
public:
int i;
First(int z) : i(z) {};
void makeobject(){ /7function returns nothing, just"WORKS"
First* fobj2;
fobj2=new First(11);
delete fobj2;
//return fobj2.i; //i do not want the function to send it...
}
};
int main(){
First f(8);
cout<<f.i<<endl;
First*po; //let's say this is ok, let it for later...
po=new First(7);
cout<<f.i<<endl;
cout<<po->i<<endl;
delete po;
cout<<?
return 0;
}
Lines 9-14 essentially accomplish nothing. You create a pointer to an object of First (fobj2). fobj2.i is initialized to 11, then you delete fobj2. The object and the value 11 are gone.
What is it that you're trying to do?
cout<<fobj2->i<<endl;
That depends where you put it. If you put that cout after line 11, you're fine. You can't do that at line 30, because fobj2 no longer exists.
Because it is a local pointer within makeobject. fobj2 goes out of scope when makeobject exits.
Did I not write its declaration on the right place now?
Since you haven't explained what you're trying to do, I don't know what to recommend.
Do you want the fobj2 pointer to persist?
Your snippet on ideone changes the fobj2 pointer to a public member of first.
Lines 53-54 are incorrect. fobj2 is a member of First. Therefore you have to reference it through an instance of First. e.g.