473,587 Members | 2,533 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how can I reference a member function using a SET itor

11 New Member
Please pardon my ignorance. I'm writing a C++ STL program after not writing code for 7 years, I'm trying to invoke key_word.inc() using an iterator for a set containing key_word_T. The following is a pared down version.

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <set>
  3. #include <string>
  4. using namespace std;
  5.  
  6. class key_word
  7. {
  8. public:
  9.   string word  (void) const         { return (wordX);     }
  10.   void   inc   (void)               { ++countX;           }
  11. private:
  12.   string wordX;
  13.   int countX;
  14. }; /* class key_word */
  15.  
  16. typedef class key_word key_word_T;
  17.  
  18. class key_word_sort
  19. {
  20. public:
  21.   bool operator () (const key_word_T& leftA, const key_word_T& rightA) const
  22.   {
  23.     return (leftA.word() < rightA.word());
  24.   } /* operator () */
  25. }; /* class key_word_sort */
  26.  
  27. typedef class key_word_sort key_word_sort_T;
  28.  
  29. set <key_word_T, key_word_sort_T> global_keys; // all keywords
  30. set <key_word_T, key_word_sort_T> local_keys;  // this document's keywords
  31. pair<set<key_word_T, key_word_sort_T>::iterator, bool> global_result;
  32. pair<set<key_word_T, key_word_sort_T>::iterator, bool>  local_result;
  33. set<key_word_T, key_word_sort_T>::iterator global_itor;
  34. set<key_word_T, key_word_sort_T>::iterator  local_itor;
  35.  
  36. string one_word;
  37.  
  38. int main (int, char**)
  39. {
  40.   one_word = "$FOOBAR";
  41.  
  42.   global_result = global_keys.insert (one_word); // compiles in full code, not in example
  43.   key_word_T local_key(*global_result.first); 
  44.   local_result =  local_keys.insert (local_key);
  45.   global_itor = global_result.first;
  46.   (*global_itor).inc();  // want to invoke key_word.inc() on global_keys copy
  47.  
  48.  
  49.   return (EXIT_SUCCESS);
  50. } /* main () */
Nov 6 '09 #1
4 1887
Banfa
9,065 Recognized Expert Moderator Expert
@jfwfmt
Your typedefs are unnecessary in C++ you can use the class name as the type name without the class keyword (same goes for structs unions and enums) so you can declare your variables as

Expand|Select|Wrap|Line Numbers
  1. set <key_word, key_word_sort> global_keys; // all keywords
  2. set <key_word, key_word_sort> local_keys;  // this document's keywords
  3. pair<set<key_word, key_word_sort>::iterator, bool> global_result;
  4. pair<set<key_word, key_word_sort>::iterator, bool>  local_result;
  5. set<key_word, key_word_sort>::iterator global_itor;
  6. set<key_word, key_word_sort>::iterator  local_itor;
  7.  
Talking of variables that is an awful lot of global variables you have there, I would consider that bad practice in C let alone C++.

(*global_itor). inc(); // want to invoke key_word.inc() on global_keys copy
The iterator is a pointer to your type so this should work although I would write it as

global_itor->inc(); // want to invoke key_word.inc() on global_keys copy
but the 2 syntaxes have the same meaning.

Did it produce any compiler diagnostics?
Nov 6 '09 #2
jfwfmt
11 New Member
Thanks for the style suggestions

Expand|Select|Wrap|Line Numbers
  1. global_itor->inc();
and
Expand|Select|Wrap|Line Numbers
  1. (*global_itor).inc();
produce the identical error message (g++)

error:passing 'const key_word' as 'this' argument of 'void key_word::inc() ' discards qualifiers
Nov 6 '09 #3
newb16
687 Contributor
It looks like dereferenced iterator is a const reference, so you can call only 'const' members with it as 'this'. I could not find proof of it in online references, but it is easily explained - if you could modify set members in some other way then insertion/deletion, it may violate their order, make them non-unique, etc.
Nov 6 '09 #4
jfwfmt
11 New Member
Thanks, your hint about const ness was the clue. It turns out that STL implementations differ on the const of set objects. GNU makes them const.

Meyers shows a workaround for non-key data members of the objects in the set, using a cast to alter the const ness of the reference

Expand|Select|Wrap|Line Numbers
  1. const_cast<key_word_T&>(*global_itor).inc();
is the fix

/s/ Jim WIlliams
Nov 7 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1848
by: jimjim | last post by:
Hello, My question concerns as to how a pointer is passed by reference as a function argument. The following is from code taken from the MICO implementation of the CORBA specification. in include files: typedef Context *Context_ptr; typedef ObjOut<Context> Context_out;
8
1861
by: wASP | last post by:
Hi, I'm having a problem referencing the elements within an object after a method of that object (a member function) has been activated with an onsubmit handler: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - <script language="javascript" type="text/javascript"> function...
11
1451
by: Richard Lewis Haggard | last post by:
Is it possible to put a reference to an object inside of a class? If so, what is the syntax? The reason I want to do this is so that I can make a copy of the original object, make modifications to the copy and then, if the modifications are acceptable, reload the original object with the contents of the modified copy object. If I were doing...
6
2650
by: Dilip | last post by:
I have a vector of class object pointers that have 2 internal states (USED or EMPTY). USED is just a way of indicating the element in that slot has valid state. EMPTY means the element is available for re-use. I was trying to write some code to locate an used element using find_if with a custom predicate derived from unary_function. That...
8
2393
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference (or better a const reference). for eg, const PointRange* points = cc.points(ptAligned);
5
2300
by: SunnyDrake | last post by:
HI! I wrting some program part of it is XML config parser which contains some commands(for flexibility of engenie). how do i more simple(if it possible not via System.Reflection or System.CodeDom.CodeCastExpression) __problem typecast #1 Desc:i do needed checks but data/commands in XML is dynamic and i don't wanna fix C# code again and...
29
3636
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a string to a integer number. bool Convert(const string& str, int* pData); bool Convert(const string& str, int& data);
2
1720
by: Howard | last post by:
Hi, I've got a program whose main object contains an array of "cells", each of which contains an array of "sub-cells", each of which contains an array of "sub-sub-cells". The cells, subcells and subsubcells are three user-defined types, each of which contains some program logic as well as some drawing capabilities. Since all cells (and...
26
4399
by: Turin | last post by:
Dear all; As far as I understand the idea behind getter methods, it is used to make sure that private memers of a class is returned appropriately to the calling object. However, if all I am interested in when making a member private is to disallow the modification of the value of that member (read-only member), then how about doing the...
0
8349
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5719
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.