D
Doug Haber
Hi All,
I'm a c++ newbie and I was hoping you could help me out with what I think is
a common problem (actually a few problems.). I want to get a pointer that I
can pass around to do some stuff, so I'm trying to write a function that
looks like:
HRESULT getThingy(Thingy* thingy)
Question 1: I assume the common way to do this is to return an HRESULT and
pass in a pointer as opposed to creating a class, or the java way, which
would look like:
Thingy getThingy()
Question 2: I use a bunch of pointers in my function, and I know I need to
clean them up but due to the structure of the function I'm not sure what's
the clean way to do it. Is there a typical approach? The function looks
something like
HRESULT getThingy(Thingy* thingy) {
A* a;
B* b;
if (SUCCESS(getA(a))){
if (a->doSomething()){
if (SUCCESS(a->getB(b))){
if (!b->foo()){
b->release();
a->release();
return E_FAIL;
}
if(SUCCESS(b->getThingy(thingy))){
b->release();
a->release();
return S_OK;
}
}
}
}
b->release();
a->release();
return E_FAIL;
}
The b->release() and a->release() all over the place seems pretty nasty.
Is there a standard way to handle this kind of issue?
Thanks!
-Doug
I'm a c++ newbie and I was hoping you could help me out with what I think is
a common problem (actually a few problems.). I want to get a pointer that I
can pass around to do some stuff, so I'm trying to write a function that
looks like:
HRESULT getThingy(Thingy* thingy)
Question 1: I assume the common way to do this is to return an HRESULT and
pass in a pointer as opposed to creating a class, or the java way, which
would look like:
Thingy getThingy()
Question 2: I use a bunch of pointers in my function, and I know I need to
clean them up but due to the structure of the function I'm not sure what's
the clean way to do it. Is there a typical approach? The function looks
something like
HRESULT getThingy(Thingy* thingy) {
A* a;
B* b;
if (SUCCESS(getA(a))){
if (a->doSomething()){
if (SUCCESS(a->getB(b))){
if (!b->foo()){
b->release();
a->release();
return E_FAIL;
}
if(SUCCESS(b->getThingy(thingy))){
b->release();
a->release();
return S_OK;
}
}
}
}
b->release();
a->release();
return E_FAIL;
}
The b->release() and a->release() all over the place seems pretty nasty.
Is there a standard way to handle this kind of issue?
Thanks!
-Doug