Jump to content
  • Advertisement
Sign in to follow this  
Ksingh30

return Type

This topic is 4803 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok sorry, but this is another lame beginner question. ok I have a class with a bunch of method functions. and with one function its either gonna return a character or a pointer to a node. how do I do that? node is a struct.

Share this post


Link to post
Share on other sites
Advertisement
1) You cannot have an overloaded function that differs only in return type, because then when you called the function, it would be impossible for the compiler to tell which one you are actually calling (since it, basically, looks at function name and passed parameters to determine what function you called).

2) How does the caller know what it's going to receive?

If you want to return the char as a flag that the function was unsuccessful (I have no idea if this is indeed what you want to do), I would suggest returning NULL instead?

A little more information (like some code, or what this function actually does) would be helpful...

Oh, and there is absolutely no need to apologize. That's what this board is for. And, hey, we were all beginners once. [smile]

Share this post


Link to post
Share on other sites
1. It's very, very bad idea, if you have to do this, this mean that you should review your function idea and create 2 different functions one that will return char other that will return pointer to node.

But if you have to, try someting like this:


char someFunction( node_s** retNode )
{
/* do stuff */
*retNode = someNodePointer;
return someChar;
}






Usage:

node_s* myRetValForNode;
char retVal = someFunction( &myRetValForNode );





Hope it helps :D

[Edited]
If you dont understand exacly what is going on with all this ** & etc. just ask :)
[/Edited]

Share this post


Link to post
Share on other sites
I know this is a bad idea, but this is an assignment. ok teh function im talking about returns the first element of a List, it could be just a value say if the list=(ABC) it would return A, but the list could also be ((AB)AC)) in that case it it needs to returns (AB) which is a sub list.

Share this post


Link to post
Share on other sites
Hint:

char someFunction( node_s** retNode )
{
/* do stuff */
if( this_node_has_char )
{
someChar = listNode->someChar;
*retNode = NULL
}
else
{
someChar = 0;
*retNode = listNode->nextNode;
}
return someChar;
}


// Usage
node_s* myRetValForNode;
char retVal = someFunction( &myRetValForNode );
if ( myRetValForNode == NULL )
{
// We have char here
}
else
{
// We have list in list
}



What you trie to do... kind of dictrionary? [smile]

Anyway for your own good, it would be better to write something more about your assignment. You see this sample answers your question, but what if you ask incorrect questions ??
And like SpacedOut wrote.... post some code to help as, help you [evil]

Share this post


Link to post
Share on other sites
ok I think I wasnt clear before. Its a function thats either gonna return a character, or a pointer to a node, which is defined as follows:
struct node
{
char info;
struct node* next;
};

this problem is keeping me back, I cant go on until I get this figured out, and the assignment is due tommorow, if could just get this the rest wont be hard.

[Edited by - Ksingh30 on September 14, 2005 11:15:07 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Ksingh30
ok I think I wasnt clear before. Its a function thats either gonna return a character, or a pointer to a node, which is defined as follows:
struct node
{
char info;
struct node* next;
};

this problem is keeping me back, I cant go on until I get this figured out, and the assignment is due tommorow, if could just get this the rest wont be hard.


Ooook... [looksaround]


node* someFunc( /* your params */ )
{
static node retVal;

// do your code here and fill retVal with values you will need
if( this_node_has_char )
{
retVal.info = char_from_cur_node;
retVal.next = NULL;
}
else
{
retVal.info = 0;
retVal.next = next_node;
}
return &retVal;




In this case you can return pointer to retVal becase it's static, but be aware that if someone will do something like this:

node* n1 = someFunc();
node* n2 = someFunc();

then n1 and n2 will have exacly this same values.

You can try this:


node* someFunc( /* your params */ )
{
static node* retVal = new node;

// do your code here and fill retVal with values you will need
if( this_node_has_char )
{
retVal->info = char_from_cur_node;
retVal->next = NULL;
}
else
{
retVal->info = 0;
retVal->next = next_node;
}
return retVal;




Bat you must remeber to delete returned value:

node* n1 = someFunc();
/* use n1 */
delete n1;


I assume you are expecting something like this:
node* someFunc( /* your params */ );
char* someFunc( /* your params */ );

but like ricekrispyw wrote

Quote:
Original post by ricekrispyw
1) You cannot have an overloaded function that differs only in return type


So just return node struct, and use next field to check is it char or next node.
If next is != NULL then you have node, else info has important char.

Hope this helps [smile]

Share this post


Link to post
Share on other sites
Im sorry not to help with anything .. but I just wanted to say that I never heard of nodes and Im interested in knowing what they are

can u direct me to any tutorials bout this?

thanks a lot

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!