Prob with set.find() = const

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gipsy boy

    #1

    Prob with set.find() = const

    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?
    --
    - gipsy boy
  • Victor Bazarov

    #2
    Re: Prob with set.find() = const

    gipsy boy wrote:[color=blue]
    > 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);[/color]

    This makes no sense, sorry. Perhaps you should consider posting C++.
    [color=blue]
    >
    > 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));[/color]

    That's not C++ either.
    [color=blue]
    > 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?[/color]

    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*,ptr Acomp> s; // ptrAcomp is a customized comparator
    s.insert(&oba);

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

    V

    Comment

    • gipsy boy

      #3
      Re: Prob with set.find() = const

      Victor Bazarov wrote:[color=blue]
      > gipsy boy wrote:
      >[color=green]
      >> 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);[/color]
      >
      >
      > This makes no sense, sorry. Perhaps you should consider posting C++.
      >[color=green]
      >>
      >> 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));[/color]
      >
      >
      > That's not C++ either.
      >[color=green]
      >> 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?[/color]
      >
      >
      > 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*,ptr Acomp> s; // ptrAcomp is a customized comparator
      > s.insert(&oba);[/color]

      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..

      --
      - gipsy boy

      Comment

      • Victor Bazarov

        #4
        Re: Prob with set.find() = const

        gipsy boy wrote:[color=blue]
        > But how do I retrieve this pointer back then, outside the scope?[/color]

        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.
        [color=blue]
        > Suppose I have an object A, and want to find a pointer to it in the set.[/color]

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

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

        What problem are you trying to solve?

        Comment

        • gipsy boy

          #5
          Re: Prob with set.find() = const

          Victor Bazarov wrote:[color=blue]
          > gipsy boy wrote:
          >[color=green]
          >> But how do I retrieve this pointer back then, outside the scope?[/color]
          >
          >
          > 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.
          >[color=green]
          >> Suppose I have an object A, and want to find a pointer to it in the set.[/color]
          >
          >
          > If you have an object, why do you need to find a pointer to it? Use the
          > damn object itself.[/color]

          No, I mean when I have an object that is findable in the list (for which
          an == comparison returns true)
          [color=blue][color=green]
          >> Do I need to make a customized == operator then for A* pointers? It
          >> just seems so odd that way, like a work-around..[/color]
          >
          >
          > No, pointers can be compared using built-in operator==.
          >
          > What problem are you trying to solve?[/color]

          We're talking about different things.
          There is a

          class App {
          std::list<A> listAs;
          std::list<B> listBs;
          std::set<C,Ccom p> 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
          --
          - gipsy boy

          Comment

          • Victor Bazarov

            #6
            Re: Prob with set.find() = const

            "gipsy boy" <x@x.pi> wrote...[color=blue]
            > Victor Bazarov wrote:[color=green]
            >> What problem are you trying to solve?[/color]
            >
            > We're talking about different things.
            > There is a
            >
            > class App {
            > std::list<A> listAs;
            > std::list<B> listBs;
            > std::set<C,Ccom p> 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?[/color]

            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


            Comment

            Working...