Why a DHT sensor need to be declare global/static

Hey everyone,

I want to work with a DHT sensor, I'm using the Adafruit Library and there is something I can't figure out.
In my project I use a factory pattern to init the DHT sensor, and then pointers to control it.

In the factory I instantiate a new DHT like this :

Factory::addNewDHT(....){

DHT* temp = new DHT(pinNumber,DHT11);
temp->begin();
return temp;
}

Then in main I was not able to readtemperature using pointer->readTemperature();

I read somewhere (but without explication) that it should be static declaration, I tried and it works :).

Does anyone knows why it is like this and how to figure out that this component required a static declaration and some other not?

thank's you all in advance.
Max.

Dub63:
In my project I use a factory pattern to init the DHT sensor, and then pointers to control it.

I have no idea what a "factory pattern" is and your code is incomplete. But, I'm gonna guess that you're trying to return a local variable that goes out of scope and existence as soon as the function exits. Making it global or local/static prevents that.

EDIT:
I was incorrect. See below.

and your code is incomplete.

In other words, you need to post ALL of your code.

Returning a value from the new operator IS valid, so you must be doing something else wrong in the code you didn't post.

PaulS:
Returning a value from the new operator IS valid,

Dooh..... You're, of course, correct. I'm just used to seeing newbies trying to return pointers to local variables.