Prob with set.find() = const

G

gipsy boy

I make an object of type A on the heap.
A oba;
I put it in a set S<A> (with a trivial comparator)
Set<A,Acomp> s;
s.insert(A);

Then I need to retrieve the pointer to the entry in that set (I want to
link some objects, without using a 'new' pointer - cause I want oba to
disappear when I exit this scope, I don't want to manually delete anything)

With a list you can easily do this with :

A* = & (*L.find(oba));
Then I can do A->changeMe(); etc..

But with a set, I can't do this, because the iterator is one of const
elements! It says I can't cast const A* to A*

I only want to use a set so the elements are sorted automatically. What
can I do about this?
 
V

Victor Bazarov

gipsy said:
I make an object of type A on the heap.
A oba;
I put it in a set S<A> (with a trivial comparator)
Set<A,Acomp> s;
s.insert(A);

This makes no sense, sorry. Perhaps you should consider posting C++.
Then I need to retrieve the pointer to the entry in that set (I want to
link some objects, without using a 'new' pointer - cause I want oba to
disappear when I exit this scope, I don't want to manually delete anything)

With a list you can easily do this with :

A* = & (*L.find(oba));

That's not C++ either.
Then I can do A->changeMe(); etc..

But with a set, I can't do this, because the iterator is one of const
elements! It says I can't cast const A* to A*

I only want to use a set so the elements are sorted automatically. What
can I do about this?

Store the pointers. In your case, the address you get from the set is
not the address of the original object. It's an address of a copy.

A oba;
std::set<A*,ptrAcomp> s; // ptrAcomp is a customized comparator
s.insert(&oba);

Now the set will contain the [constant] pointers to non-const objects.

V
 
G

gipsy boy

Victor said:
This makes no sense, sorry. Perhaps you should consider posting C++.



That's not C++ either.



Store the pointers. In your case, the address you get from the set is
not the address of the original object. It's an address of a copy.

A oba;
std::set<A*,ptrAcomp> s; // ptrAcomp is a customized comparator
s.insert(&oba);

But how do I retrieve this pointer back then, outside the scope?

Suppose I have an object A, and want to find a pointer to it in the set.
Do I need to make a customized == operator then for A* pointers? It just
seems so odd that way, like a work-around..
 
V

Victor Bazarov

gipsy said:
But how do I retrieve this pointer back then, outside the scope?

I don't know. How did you intend to retrieve the object? Now, instead
of the object retrieve the pointer to an object and dereference it. You
will get the object.
Suppose I have an object A, and want to find a pointer to it in the set.

If you have an object, why do you need to find a pointer to it? Use the
damn object itself.
Do I need to make a customized == operator then for A* pointers? It just
seems so odd that way, like a work-around..

No, pointers can be compared using built-in operator==.

What problem are you trying to solve?
 
G

gipsy boy

Victor said:
I don't know. How did you intend to retrieve the object? Now, instead
of the object retrieve the pointer to an object and dereference it. You
will get the object.



If you have an object, why do you need to find a pointer to it? Use the
damn object itself.

No, I mean when I have an object that is findable in the list (for which
an == comparison returns true)
No, pointers can be compared using built-in operator==.

What problem are you trying to solve?

We're talking about different things.
There is a

class App {
std::list<A> listAs;
std::list<B> listBs;
std::set<C,Ccomp> setCs;
}

class C {
std::list<A*> listAPtrs;
std::list<B*> listBPtrs;
}

Objects of type C carry lists that refer to objects of type A or B that
live in the program.
But, since I only have the lists in my whole app, the objects only live
in those lists. I mean, the only valid pointers are the ones I get by
find(..), the iterator points.
I need the addresses of these entries in those lists to be able to
construct C's correctly right? Am I going wrong about this? (again, I
don't want to use 'new' to create freely available pointers)

How do I solve this correctly?
thanks btw..sorry for the bad syntax
 
V

Victor Bazarov

gipsy boy said:
We're talking about different things.
There is a

class App {
std::list<A> listAs;
std::list<B> listBs;
std::set<C,Ccomp> setCs;
}

class C {
std::list<A*> listAPtrs;
std::list<B*> listBPtrs;
}

Objects of type C carry lists that refer to objects of type A or B that
live in the program.
But, since I only have the lists in my whole app, the objects only live in
those lists. I mean, the only valid pointers are the ones I get by
find(..), the iterator points.
I need the addresses of these entries in those lists to be able to
construct C's correctly right? Am I going wrong about this? (again, I
don't want to use 'new' to create freely available pointers)

How do I solve this correctly?

OK, if your design is to store pointers to existing objects which are
stored elsewhere, that's fine. Make sure none of them disappear without
'C' objects knowing about that, otherwise you'll have dangling pointers.

AFAICS, you're doing it fine.

Now, what exactly do you need with set::find? Are you looking to find
a particular object of type C in 'setCs' that has a pointer to a specific
A or B? Then I'd recommend using 'std::find_if' with a custom predicate.

Victor
 

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
474,197
Messages
2,571,040
Members
47,635
Latest member
SkyePurves

Latest Threads

Top