Pass object in objects array as function argument

Hello,

this should be a very basic question, but I haven’t found it answered anywhere, so here it goes.

I have a function that accepts a TProfile* as argument (also TGraphErrors* and TEllipse*, but I guess it is equal) and I want to create the object inside the function.

The function is:

void function(TProfile* prof){ prof=new TProfile("a","a",100,0,1); prof->Fill(0.5,2); return; }

This works:

TProfile* prof; function(prof); prof->Draw();

but this doesn’t

TProfile* prof[n]; for(int i=0; i<n; i++){ function(prof[i]); prof->Draw(); }

I mean, the function line runs, but then it doesn’t recognize the profile being created and it breaks on the draw.

I know I can also put TProfile ** or TProfile *[] as argument and loop inside the function, but is there any way in which I wouldn’t have to? In general I am using arbitrarly large dimension arrays (up to 4 until now, but could grow bigger)

Thanks

Either: [code]TProfile* function(void)
{
TProfile *prof = new TProfile(“a”, “a”, 100, 0, 1);
prof->Fill(0.5, 2);
return prof;
}

void trial(void)
{
TProfile prof;
prof = function();
if (prof) prof->Draw();
}[/code] or: [code]void function(TProfile
&prof)
{
prof = new TProfile(“a”, “a”, 100, 0, 1);
prof->Fill(0.5, 2);
return;
}

void trial(void)
{
TProfile *prof = 0;
function(prof);
if (prof) prof->Draw();
}[/code]

Hi Wile,

Solution #2 is exactly what I was looking for.

Thank you very much!