DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2004
    Posts
    242

    Pointers casting

    Hello,

    I've got the following code looping over a map :-

    ...
    key = "Key1";
    val = "val1";
    p = months.find(key);

    if(p != months.end()) // found
    {
    for (months_type::const_iterator it = months.begin();
    it != months.end(); ++it)
    {
    if ((it->first == key) && (it->second->counterpartyCode == val))
    {
    Here I want to point the pointer to the correct object and update it accordingly. I tried casting as follows, but this isn't working.

    //CounterpartyDetails* pExisting =
    // (CounterpartyDetails *)(it->second->cashCcy);
    //pExisting->cashCcy += 300;
    //break;

    I know I can do: it->second->cashCcy +=300; but was hoping to find a nice way...
    }
    }

    Can anyone please help ?
    Last edited by ami; 04-23-2008 at 05:58 AM.

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    what is cashCcy?
    if its a CounterpartyDetails, you need:

    CounterpartyDetails* pExisting = &((it->second->cashCcy));


    alternately, if second is a *CounterpartyDetails, you just need

    CounterpartyDetails* pExisting = (it->second);

  3. #3
    Join Date
    May 2004
    Posts
    242
    Thanks for the information.

    I forgot to say that when creating the objects, I'm doing :-

    ...
    CounterpartyDetails *ptr = new CounterpartyDetails();
    ptr->Name = name;
    ptr->cashCcy = ccy;
    ...
    and then insert into a map as follows :-

    myMap.insert(value_type(name, ptr));
    ...
    And again :-

    ...
    ptr = new CounterpartyDetails();
    ptr->Name = name;
    ptr->cashCcy = ccy;
    ...
    and then insert into a map as follows :-

    myMap.insert(value_type(name, ptr));


    The thing is, I keep on creating objects using the same mechanism, i.e. ptr keeps on moving to the next object obviously.

    After creating all objects is completed, how can I change ptr to point to a specific object created, e.g. how would I change ptr to point to the second object created without a loop ?

    Thanks.
    Last edited by ami; 04-23-2008 at 03:31 PM.

  4. #4
    Join Date
    Dec 2003
    Posts
    3,366
    um, however you normally get to that object. Map.extract or whatever function gives you the inserted node back, you take the address of it if its data or just use it if its already a pointer. I guess I don't know what you are confused about.

  5. #5
    Join Date
    May 2007
    Posts
    843
    His question is in a sequence of map after insert ptr. The ptr should point to last element but in case he want change to second element in map.

    Am i correct.


    For instance, you five element in a map but u want point to map.

    Pointer offset. *(ptr - 3);

    I don't know this is correct or not.Please correct if i wrong.

  6. #6
    Join Date
    May 2004
    Posts
    242
    Yes, thank you. That's exaclty my question. If I want to access the second or third object in the map via the pointer how would I do that ? Obviously, after the last insert, the pointer points to the last element.

    Does this make sense ?

  7. #7
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    It runs counter to the concept of a Map to be trying to access "by a pointer" and by pointer arithmetic, because a Map stores Pairs of Key and Element. It is organized to find by searching for the Key and returning the Element. While you can have a sorted map it doesn't need to be. What motivates your design to be able to return something by pointer arithmetic?

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    I agree with nspils. Your design is trying to force maps to do something they weren't design to. Even if that's possible, it's convoluted, inefficient and bug-prone. Maps, as all STL containers, are value sequences. They usually contain objects, not pointers. Maps in particular are made to store pairs, and access them by the pair template, or using map iterators.
    Danny Kalev

  9. #9
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    I'm not exactly sure that I get your problem, but try this:
    Code:
    CounterpartyDetails* pExisting =
     (CounterpartyDetails *)(it->second);

  10. #10
    Join Date
    Dec 2003
    Posts
    3,366
    or pull it out of the map and take its address. But there is no concept of "second" item here, right? Even with the iterator they will not be in the order of insertion (right?). Im not 100% sure, I have not actually used this data structure. But the safe way to go is iterate to the Nth item and take its address.

    If it turns out that you want the items by order of insertion, your best bet is simply going to be something like add a counter to the data type and iterate over the map (counter productive, but perhaps you will access via map 99% of the time but once in a while, you need them in the orig ordering???) or you can maintain a list of pointers to them in the inserted order (ugly to create, but once done, you are good). Here, you iterate the map and extract the item that matches what you just put in and take its address, place into a simple list or vector or whatever you see fit.

Similar Threads

  1. Replies: 0
    Last Post: 09-25-2006, 01:58 AM
  2. Replies: 1
    Last Post: 11-26-2005, 02:44 PM
  3. Casting again
    By FededS in forum C++
    Replies: 7
    Last Post: 03-30-2005, 05:08 PM
  4. How to get pointers to variables and file location
    By Benzsoft in forum VB Classic
    Replies: 1
    Last Post: 02-21-2002, 10:47 AM
  5. Re: Is C# secure ??? (Pointers?)
    By ceemar in forum .NET
    Replies: 4
    Last Post: 01-31-2001, 04:47 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links