| Author |
problem with reference variable
|
|
| niraj.kumar.ait@gmail.com 2005-05-30, 7:53 am |
|
tParserSo is a object of Soloader when i print the value of objectPtr
in the soloader function then it has the correct value but it doesnt
return anything to tPrs .In some cases in work but some times it doesnt
work . I m using g++ 3.2.2 version
tParserSo.CreateObject((void *)tPrs)
bool SoLoader::CreateObject(void* & objectPtr){
try{
objectPtr = mCreate();
std::cout<<objectPtr<<std::endl;
return true;
}catch(bad_alloc& exBadAlloc){
return false;
}
TIA
niraj
| |
| phil_gg04@treefic.com 2005-05-30, 7:53 am |
| niraj.kumar.ait@gmail.com wrote:
> tParserSo is a object of Soloader when i print the value of objectPtr
> in the soloader function then it has the correct value but it doesnt
> return anything to tPrs .In some cases in work but some times it doesnt
> work . I m using g++ 3.2.2 version
>
> tParserSo.CreateObject((void *)tPrs)
What is the definition of tPrs?
Get rid of that cast.
Do you get warnings when you compile with -Wall ?
> bool SoLoader::CreateObject(void* & objectPtr){
> try{
> objectPtr = mCreate();
>
> std::cout<<objectPtr<<std::endl;
>
> return true;
> }catch(bad_alloc& exBadAlloc){
> return false;
> }
--Phil.
| |
| niraj.kumar.ait@gmail.com 2005-05-31, 2:48 am |
|
does it matter what is the type of tPrs
because we r typcating it as a void pointer passing its reference
No warning
| |
| phil_gg04@treefic.com 2005-05-31, 7:48 am |
| Niraj,
> tParserSo.CreateObject((void *)tPrs)
> bool SoLoader::CreateObject(void* & objectPtr){
I don't think this does what you think it does. I think that the cast
creates a temporary of type void*, and that is passed to CreateObject.
The temporary is assigned to and then discarded. But I could be wrong,
and I would have expected a warning if that were the case.
I repeat my original suggestion: get rid of the cast. E.g.
void* p;
tParserSo.CreateObject(p);
cout << p << endl;
--Phil.
p.s. comp.lang.c++ might be a better place to ask.
|
|
|
|