Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

References

Status
Not open for further replies.

Nosferatu

Programmer
Jun 9, 2000
412
RO
Hello!

I'm quite new in C++ and even if i have some solid C experience, if find some things confusing.

Here's my problem:
1. I have a list<column> container in my program.
2. I want to insert something.
3. The STL list.insert requires an iterator parameter to be passed to it.
4. I want to insert an element at a specified index.
5. I don't assume that the index is within the range of the list, so when i try to get the iterator at the specified position, i would like to return some NULL pointer if the index is out of range. However, you cannot assign a NULL pointer to a reference (i guess that's because the refs are automatically dereferenced).
The problem is that i dont' want to set any flags telling me that something went wrong, i just want to know if the reference returned is valid or not.

So, i have 2 questions:
Q1: How do i use references in order to simulate some NULL pointer?
Q2: How do i use an iterator to return a POINTER to the object, instead a reference to it?

I hope i'm not deeeeeply confuzed here... But if i am, i would appreciate greatly any help!

Thanks
 
There are varios way to solve your problem and
I append an example. It's not my suggestion because
basically I don't like STL data structure.

class node {
int a;
public:
node( int value ) : a(value) {}
void print() { cout << &quot;value=&quot; << a << '\n'; };
};


int main( void )
{
/* part 1 : node itself for template argument */
list<node> l;
list<node>::iterator i;

l.insert( l.begin(), node(1) );
for( i=l.begin(); i!=l.end(); i++ ) {
node& n = *i;
n.print();
}

/* above loop says that 'i' is invalid when it
is l.end() so you can return l.end() if invalid
index was supplied. */

/* part 2 : node pointer for template argument */
list <node*> lp;
list<node*>::iterator ip;

lp.insert( lp.begin(), new node(1) );
for( ip=lp.begin(); ip!=lp.end(); ip++ ) {
node* n = *ip;
n->print();
}

/* currently ip==lp.end() and *ip is NULL.
You can inspect *ip to know the iterator is valid
And I think it can be answer for your 2nd question. */
cout << *ip << '\n';

for( ip=lp.begin(); ip!=lp.end(); ip++ )
delete *ip;

return 0;
}
Hee S. Chung
heesc@netian.com
 
Thanks for the post Hee.

The thing i am using right now is the list<node> thing, and there is the problem. The thing is that i should return a reference to the object in the list somewhere where the list itself is not known, for example in a derived class (the list would be private in the base class). So, i don't want to test against list.end(), not even in the base class (just a style consideration).

Isn't any way to check if a reference is valid, something simple enough as the testing against a NULL pointer it is to pointers?

Anyway, i think i will change the approach and build a list of pointers instead that building a list of objects. As far as i know, building a list of objects involves recreating the objects via CC, so i think this could be time consuming at some point.
What dou you think about that? Nosferatu
We are what we eat...
 
1. According to C++ grammar, it is guaranteed that
reference is always a valid object itself. NULL pointer
value can be viewed as an exception of pointer values
but it is different for reference.

I think error can be reported by return value but
you cannot return the iterator in this case.

bool insert( list<node>::iterator& i, int index );

to receive an iterator, a reference is supplied
as an argument.

I hope you can find your best way.

2. Your last thought is exact. It is one of the reason
why I don't like STL.
Hee S. Chung
heesc@netian.com
 
Thank you for your guidance.
I think the problem therefore lies in my strongly C preformatted head. I had the feeling that i won't be able to do this with references, but i wanted to be sure of that.

Thank you again. Nosferatu
We are what we eat...
There's no such thing as free meal...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top