473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
Jul 22 '05 #1
5 2231
gipsy boy wrote:
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
Jul 22 '05 #2
Victor Bazarov wrote:
gipsy boy wrote:
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);


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
Jul 22 '05 #3
gipsy boy wrote:
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?
Jul 22 '05 #4
Victor Bazarov wrote:
gipsy boy wrote:
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.


No, I mean when I have an object that is findable in the list (for which
an == comparison returns true)
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?


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
--
- gipsy boy
Jul 22 '05 #5
"gipsy boy" <x@x.pi> wrote...
Victor Bazarov wrote:
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?


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
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Noah Roberts | last post by:
EmailAccount(std::string key_name, bool editable = true); EmailAccount(std::string account_manager_key, std::string account_key, bool editable = true); EmailAccount testAccount(my_key +...
1
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
6
by: skubik | last post by:
Hi everyone. I'm attempting to write a Javascript that will create a form within a brand-new document in a specific frame of a frameset. The problem is that I can create the form and input...
2
by: Gernot Frisch | last post by:
I'm using #~*&$§ VC6 and have this problem: // good std::deque<A_POINT>::const_iterator it = p.POINTS.begin(); // bad std::deque<A_POINT>::const_reverse_iterator it = p.POINTS.rbegin(); ...
5
by: B | last post by:
Hello, I am trying to migrate a vb6 app to vb.net. In one piece of the vb6 app I have a label where the label backcolor changes every 5 seconds on a timer. So I created an array of colors as...
18
by: ma740988 | last post by:
Trying to get more acclimated with the use of function objects. As part of my test, consider: # include <vector> # include <iostream> # include <algorithm> #include <stdexcept> #include...
2
by: andrewanderson | last post by:
hi can anyone help me with this prog. cant find the prob why it cant display cout<<"This is the display of your transaction"<<endl; ifstream fobj; //declare input file stream ...
2
by: mnacw | last post by:
Can anybody help me to resolve this prob. i have installed Visual Studio 2005 Professional edition. I am working in VB.Net. When I tried to connect to database it is connected but when i make some...
1
by: lumo2000 | last post by:
hello NG, i am trying to do some syscalls and therefore i need to put some text together. its no problem as long i want to cout the text to display, but when i want to use it as parameter for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.