Link to home
Start Free TrialLog in
Avatar of codeBuilder
codeBuilder

asked on

any reference returning example

COuld you please fill the below function's body in any appropriate way?
Node is any class.

Node &  f ( Node& x)
{

?
}

IN addition , How will i call it ?

Thanks in advance.
Avatar of codeBuilder
codeBuilder

ASKER

I wrote as below

Node &  f ( Node& x)
{
    Node nd = new Node();
    return nd;
}

but it gives WARNING :
 warning: reference to local variable ‘nd’ returned [enabled by default]

WHEN i run it, segmentation fault occurs . Why this warning occured?What did i do wrong? What should i do ?
SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why does it create memory leaks? Can't i prevent it for the above small code(which you give recently) with little tricks?

In addition , i do such a thing like below:
for(i=0;i<10,i++)
{
Node* p = new Node();
}

Open in new window


Does it have any problem to do such a thing?Where is the problem ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah , i understand but what should i do when i need to create pointer for each loop ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@sarabande
To do what you said , should i do as the following definition in the Node class or smthing else:

Node{
..
Node * next = Node;
..
}


@pepr

Although i know C++ for 2 years , I still couldn't learn the difference between the reference & and pointer * . I have always been confused about them . I know its basic definitons. However , when i see them in function's paramaters , or function's returning types , i am absolutely puzzled. For example when i see like :
Node& (const *Node , const Network&)

i shocked and don't know what it means ? I need to see and understand vital examples which is simple but includes all of these keywords
@codeBuilder: I highly recommend "Thinking in C++" by Bruce Eckel (http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html). You can buy the printed version of the two volumes, or you can download the HTML version for free. (There is no on-line HTML version--at least I do not know about any--but you simply download the zip archive, unzip it and launch the .../Volume1/HTML/Frames.html.)

It is nicely written and it is likely you find the answers with a kind of drill/exhaustive examples. Try the  "Complicated declarations & definitions" (your local disk... ThinkingInCpp/Volume1/HTML/Chapter03.html#Heading176 -- I may have older copy, the Heading176 may have moved slightly). When following his explanations, you will understand even the complex cases like (excerpt):
As an aside, once you figure out how the C and C++ declaration syntax works you can create much more complicated items. For instance:

    //: C03:ComplicatedDefinitions.cpp

    /* 1. */     void * (*(*fp1)(int))[10];

    /* 2. */     float (*(*fp2)(int,int,float))(int);

    /* 3. */     typedef double (*(*(*fp3)())[10])();
                 fp3 a;

    /* 4. */     int (*(*f4())[10])();

    int main() {} ///:~

Walk through each one and use the right-left guideline to figure it out. Number 1 says “fp1 is a pointer to a function that takes an integer argument and returns a pointer to an array of 10 void pointers.”

Number 2 says “fp2 is a pointer to a function that takes three arguments (int, int, and float) and returns a pointer to a function that takes an integer argument and returns a float.”

If you are creating a lot of complicated definitions, you might want to use a typedef. Number 3 shows how a typedef saves typing the complicated description every time. It says “An fp3 is a pointer to a function that takes no arguments and returns a pointer to an array of 10 pointers to functions that take no arguments and return doubles.” Then it says “a is one of these fp3 types.” typedef is generally useful for building complicated descriptions from simple ones.

Number 4 is a function declaration instead of a variable definition. It says “f4 is a function that returns a pointer to an array of 10 pointers to functions that return integers.”

You will rarely if ever need such complicated declarations and definitions as these. However, if you go through the exercise of figuring them out you will not even be mildly disturbed with the slightly complicated ones you may encounter in real life.

There is a nice explanation somewhere in the book about how to read the function definitions, but I did not find it now. (You will do ;)
To do what you said , should i do as the following definition in the Node class or smthing else:

generally, a Node class object points to another (the next or previous) Node. such "pointing" can only made by pointer in c++. therefore, it makes not so much sense to pass Node objects 'by reference' to  a function when only the pointer was needed. so at least at the node level you would pass a Node object by pointer rather than by reference. a new node would not point to a next pointer but would initialize the next member pointer to NULL. so typically you would use a member function like
Node * Node::append(Node * pn)  
{ 
    next = pn; 
    if (pn != NULL)
        pn->prev = this; 
    return this; 
}

Open in new window

to link two nodes together.

Sara
I still couldn't learn the difference between the reference & and pointer *

Technically, both a reference variable and a pointer variable need the memory space 4 bytes (32-bit system) or 8 bytes (64-bit system). The address of the target memory block is stored inside. The target block of memory can store anything -- from a simple built-in type value (say char) to any complex object (data that belongs to the instance of the class).

When accessing the target. When accessing the target object via a pointer variable, you have to explicitly dereference the value using *. When accessing the same object via the reference variable, the dereferencing is automatic. The reference variable behaves as if it was a variable that stores the object itself; there always is one more indirection hidden inside.

Syntactically, working with a reference variable looks exactly the same as working with the variable that stores the object. For working via pointer, you need special syntax.

References are a bit more abstract than pointers. With a pointer, you actually use the stored address that is used explicitly when dereferencing it. The value can also be NULL. With a reference variable, the address is a bit more wraped inside. The stored value cannot be NULL.