NULL

M

Mohitz

How do you write a function in C++ which returns a class object in
some cases and in others, returns something like a NULL pointer so
that i can know in the callee function that the object doesnt exist??

ClassName A()
{
ClassName a;
if (condition)
return a;
else
 
G

Guest

How do you write a function in C++ which returns a class object in
some cases and in others, returns something like a NULL pointer so
that i can know in the callee function that the object doesnt exist??

ClassName A()
{
ClassName a;
if (condition)
return a;
else

Return a pointer:

class A;

A* func(int i)
{
if (i = 0)
return new A();
else
return 0;
}
 
R

Rolf Magnus

Mohitz said:
How do you write a function in C++ which returns a class object in
some cases and in others, returns something like a NULL pointer so
that i can know in the callee function that the object doesnt exist??

ClassName A()
{
ClassName a;
if (condition)
return a;
else
throw some_exception();
 
M

Mohitz

Ya, the failure of the condition indicates a parse error. I want to
just print an intelligent
error message when that happens and want the program to exit. What's
the best way to do that?

Also, I am now trying to return a pointer but the program exits with a
segmentation fault. I am sorry
i am not quite good with pointers. Here is the code i am using. Is
something wrong with it?

ClassName * funcName(string name)
{
deque<ClassName> tempDeque = someGlobalClassNameDeque;
deque<ClassName>::iterator tempDequeIterate;
tempDequeIterate = tempDeque.begin();
while (tempDequeIterate != tempDeque.end())
{
ClassName * tempClassName = new ClassName();
ClassName temp = *tempDequeIterate;
*tempClassName = temp;
if (name == temp->name)
return tempClassName;
tempDequeIterate++;
}
return NULL;
}

int main()
{
ClassName * p = funcName(someString);
if (p == NULL)
{
parseError(someString + " undefined.");
}
else
{
toRet = anotherFunc(*p);
}
return toRet;
}

Any suggestions/comments would be appreciated.

Thank you,
Mohit
 
B

BobR

Mohitz said:
Ya, the failure of the condition indicates a parse error. I want to
just print an intelligent
error message when that happens and want the program to exit. What's
the best way to do that?

Also, I am now trying to return a pointer but the program exits with a
segmentation fault. I am sorry
i am not quite good with pointers. Here is the code i am using. Is
something wrong with it?

ClassName* funcName(string name){
deque<ClassName> tempDeque = someGlobalClassNameDeque;
deque<ClassName>::iterator tempDequeIterate;
tempDequeIterate = tempDeque.begin();
while (tempDequeIterate != tempDeque.end()){
ClassName * tempClassName = new ClassName();
ClassName temp = *tempDequeIterate;
*tempClassName = temp;
if (name == temp->name)
return tempClassName;
tempDequeIterate++;
}
return NULL;
}

int main(){

std::string someString("Testing 1 2 3");
try{
ClassName *p = funcName(someString);
if (p == NULL){
parseError(someString + " undefined.");

someString += " undefined.";
throw someString;
}
else{
toRet = anotherFunc(*p);
}

} // try
catch( std::string const &st ){
std::cout<< st <<std::endl;
}
catch( const std::eek:ut_of_range &Oor ){
std::cout<<"out_of_range caught "<<Oor.what()<<std::endl;
}
catch( std::exception const &se){
std::cout<<se.what()<<std::endl;
}
catch( ... ){
std::cout<<"caught a cold or something!"<<std::endl;
}
return toRet;
}

Any suggestions/comments would be appreciated.
Thank you, Mohit

Please, do not top-post.
 
J

jalina

Mohitz a écrit :
How do you write a function in C++ which returns a class object in
some cases and in others, returns something like a NULL pointer so
that i can know in the callee function that the object doesnt exist??

ClassName A()
{
ClassName a;
if (condition)
return a;
else
Make a dummy instance of yur class and return it.

if (&A() != &ClassName::Dummy)
{
// ...

}
 
J

Jim Langston

Mohitz said:
Ya, the failure of the condition indicates a parse error. I want to
just print an intelligent
error message when that happens and want the program to exit. What's
the best way to do that?

Also, I am now trying to return a pointer but the program exits with a
segmentation fault. I am sorry
i am not quite good with pointers. Here is the code i am using. Is
something wrong with it?

Nothing is wrong with this code, per se (see my comments inline about
calling new and not delete), that I can see. Nothing that should cause a
segmentation fault. Is your class assignable? Does it have a custom
assignment operator? It may need one if you are using pointers, references
or use of new in the class constructor or initalizer.

I would quesiton if deques are assignable (your statement: deque<ClassName>
tempDeque = someGlobalClassNameDeque). It is also not required. Just use
someGlobalClassNameDeque in place of tempDequie inside your code.

Also, your method of assigning a newed instance the value from an iterator
is convoluted. You are using an extra temp variable you don't need to.
Instead of:
ClassName temp = *tempDequeIterate;
*tempClassName = temp;
you can just use
*tempClassName = *tempDequeiterate;
as I've shown in my "why don't you do it this way" code.
ClassName * funcName(string name)
{
deque<ClassName> tempDeque = someGlobalClassNameDeque;
deque<ClassName>::iterator tempDequeIterate;
tempDequeIterate = tempDeque.begin();
while (tempDequeIterate != tempDeque.end())
{
ClassName * tempClassName = new ClassName();

Here's new, where's your delete? I wouldn't bother creating a new instance
and returning it unless you have to though.
ClassName temp = *tempDequeIterate;
*tempClassName = temp;
if (name == temp->name)
return tempClassName;

How about instead of those 5 lines above (untested code):

if ( name == tempDequeIterate->name )
{
ClassName* tempClassName = new ClassName();
*tempClassName = *tempDequeIterate;
return tempClassName;
}

That way you only call new once for the object you are going to return and
dont' need to call delete on it in this function
tempDequeIterate++;
}
return NULL;
}

int main()
{
ClassName * p = funcName(someString);
if (p == NULL)
{
parseError(someString + " undefined.");
}
else
{
toRet = anotherFunc(*p);
}
return toRet;
}

Any suggestions/comments would be appreciated.

At this point it is difficult to say what is causing your segmentation
fault. Like I said, it is most likely in your assignment operator, unless I
am missing something obvious. Or maybe it is actually an out of memory
error with all the news and no deletes?

Can you post some code that will compile and produce the error?
 
O

Old Wolf

i am not quite good with pointers. Here is the code i am using. Is
something wrong with it?

ClassName * funcName(string name)
{
deque<ClassName> tempDeque = someGlobalClassNameDeque;
deque<ClassName>::iterator tempDequeIterate;
tempDequeIterate = tempDeque.begin();
while (tempDequeIterate != tempDeque.end())
{
ClassName * tempClassName = new ClassName();
ClassName temp = *tempDequeIterate;
*tempClassName = temp;
if (name == temp->name)
return tempClassName;
tempDequeIterate++;
}
return NULL;
}

Yes, it is revolting. The following code has exactly
the same effect (some var names shortened so that my
code fits in Usenet line limits):

for( deque<ClassName>::const_iterator it = global.begin();
it != global.end(); ++it )
{
if ( name == it->name )
return new ClassName(*it);
}
return NULL;

Having said that, this is a poor method to solve
your problem. In fact even your seemingly simple
invocation of it has an error:
ClassName * p = funcName(someString);
if (p == NULL)
parseError(someString + " undefined.");
else
toRet = anotherFunc(*p);
return toRet;

You never deleted the objected allocated by 'new'.
Also if p were NULL then you return a value that
you never set.

The good solutions to your problem are:
1) throw an exception on failure
2) return a ClassName with a special value
that indicates failure.

With exceptions the above code becomes:

ClassName funcName(string name)
{
for( deque<ClassName>::const_iterator it = global.begin();
it != global.end(); ++it )
{
if ( name == it->name )
return *it;
}
throw std::runtime_error( name + " undefined." );
}
//
// In main function or whatever
//
try {
return anotherFunc( funcName(someString) );
}
catch (std::exception &e) {
parseError( e.what() );
return whatever;
}

Much simpler!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,279
Latest member
LaRoseDermaBottle

Latest Threads

Top