Searching for specific element in Composite pattern

Suppose I have a simple composite pattern structure:

  • abstract class User

  • leaf class PersonalUser

  • composite class GroupUser with a std::vector<User*> users container as its member variable

and a method bool GroupUser::findUser(User* u) which returns true if the user u is found in the users container (which naturally consists of other PersonalUser or GroupUser objects)

Now, I'd like to define that function recursively of course, so I need to go through each User in users vector, and compare with u, but I won't know if the User is a PersonalUser or GroupUser, so my question is:

Do I have to define a virtual function char User::returnType() which will tell me which type of User it is, or is there a better/smarter way to go down the tree and look for the User?

p.s. there is of course a method like bool areEqual(User*, User*) by which we can compare users :)


You can add the FindUser member function as a virtual to the abstract class User, and make it return true if the user being searched for is the current user (Personal or Group).

In GroupUser you can override FindUser and delegate any call to all the contained Users if the User being searched for isn't the current GroupUser.

Need Your Help

Are there any drawbacks to having 1 solution per project

visual-studio projects-and-solutions

We are working on a big application, comprising around 100 projects (40 views, 40 controllers/models, 20 Utilities libraries). We have outsourced the bulk of the work and the deliverables come in f...

One or Two Primary Keys in Many-to-Many Table?

database data-structures indexing schema

I have the following tables in my database that have a many-to-many relationship, which is expressed by a connecting table that has foreign keys to the primary keys of each of the main tables:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.