help with STL container remove_if on a class

This is a discussion on help with STL container remove_if on a class within the C++ Programming forums, part of the General Programming Boards category; It gives it access to private members and encapsulates it as part of MyClass? Well, you already have a public ...

  1. #16
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    It gives it access to private members and encapsulates it as part of MyClass?
    Well, you already have a public get() function to retrieve num, so you don't need direct access to the private members of MyClass. And, it seems to me that making class Pred a public member of MyClass creates a hole in MyClass's encapsulation: now anyone can create an object of type Pred and they will gain direct access to the private members of MyClass. That allows them to bypass the interface you defined for MyClass, namely your setNum() and setChar() functions.

    In this case, that doesn't really mean much since your set() functions allow you to assign any values to the members num and ch, but what if your set() functions were defined such that you could only assign non-zero integers to num and only lower case chars to ch? In that case, someone could create a Pred object and use it to assign negative integers to num and assign upper case chars to ch. So, in principle I think having class Pred as a public member of class MyClass is a bad idea.
    Last edited by 7stud; 01-30-2006 at 11:23 PM.

  2. #17
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The constructor is called only once, but the destructor called twice.
    I probably create a single instance of Pred in main when I call remove_if. It then copies it and uses that one copy to compare each element. When it's done, it destroys them.
    My book says that that is a key point about predicates and remove_if(). remove_if() will copy the predicate--and maybe more than once. That actually has some important implications as to what functors(i.e. objects that overload the () operator, e.g. your Pred class) can be used as a predicate in STL functions. Functors that change their internal state(e.g. one of their member variables) cannot be used with predicatable results because remove_if() copies the predicate in its original state. After remove_if() finds an element to remove, it uses a copy of the predicate in its orignal state to continue along the list.

  3. #18
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    Making Pred private instead of public gives the same results. If it's commonly frowned upon to use nested classes, then I'll remember that when actually coding something.

  4. #19
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by Syneris
    Making Pred private instead of public gives the same results. If it's commonly frowned upon to use nested classes, then I'll remember that when actually coding something.
    There wouldn't be nested classes unless they had some usefulness. I was just pointing out that making the nested class public didn't make much sense to me.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    I am also still pretty new to class design, so I would like to ask: wouldnt it be better to ditch the getter/setter functions, and instead have member functions that perform specific tasks related to what the class represents?

    In this case, Syneris' example class seems to keep the num and ch member variables pretty much separate, as if it is nothing more than a shell with getters/setters around its member variables. I think it might as well be a struct or leave the member variables public.

    However, if we ignore ch (since it doesnt seem to be used anyway), and an int was convertible to MyClass through say a constructor that accepts an int, and an appropriate operator== existed, then we should be able to do:
    Code:
    ClassList.remove_if(std::bind2nd(std::equal_to<MyClass>(), 1));
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to get the size of an STL container object?!!!
    By sam-j89 in forum C++ Programming
    Replies: 7
    Last Post: 05-12-2009, 02:56 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  5. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21