compare a struct with standard list

Hi Experts,

I have the following struct in c++ :

typedef struct
    std::string     type;
    std::string     name;
    std::list<std::string> contacts
    bool            connected;
} clientDetail;

I have 2 questions :
1- how to initialize the struct to null;

2 - I need to create a method that compares two client Detail structs, how should I do that ? Do I need to allocate dynamic memory for the list ?


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Actually, there is no one-and-the-only "null" for that case. Also NULL has a bit low-level meaning (basically "put all related memory bytes to zero").

In your specific example, the string type is automatically initialized to an empty string, the list to an empty list. The only problem is with bool that is a kind of lower-level object. It should have one of false/true values. You can fill it with zero somehow to initialize it to false. However, it is better to initialize it explicitly to false instead. Using the C++11 standard, you can modify your last element definition to bool connected { false }; (and do not forget to add the semicolon to the previous line).

A struct is basically treated as class with public elements. You should probably convert it to the class as it does not help anything to keep it struct.

For the comparison, the two approaches are usually used: 1) write a function that takes the two compared objects (usually constant references to them), 2) write a method of the same class that compares itself with the passed argument. Understanding the second approach, you can add syntactic sugar and overload the wanted operator.
Regarding the conmparison, you could just create an operator for that purpose, i.e.

#include <list>
#include <string>

typedef struct
    std::string     type;
    std::string     name;
    std::list<std::string> contacts;
    bool            connected;
} clientDetail;

bool operator==(const clientDetail& l, const clientDetail& r) {

  if (l.type != r.type) return false;

  if ( != return false;

  if (l.connected != r.connected) return false;

  // compare contact list. You can skip that if this is not required
  if (l.contacts.size() != r.contacts.size()) return false;

  std::list<std::string>::const_iterator i, j;

  i = l.contacts.begin();
  j = r.contacts.begin();

  while( i != l.contacts.end() && j != r.contacts.end()) {

    if (*i != *j) return false;

    ++i; ++j;

  return true;

void main () {

  clientDetail a, b;

  // ...

  bool c = a == b; 

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
To add, it is possible to compare the lists using std::equal standard algorithm. This way, the above code part for the lists could be rewritten like this:
    if (l.contacts.size() == r.contacts.size())
        return std::equal (l.contacts.begin(), l.contacts.end(), r.contacts.begin());
        return false;

Open in new window

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

bachra04Author Commented:
I assume it will be the same for overloading the assignment operator ?
bachra04Author Commented:
I mean do  I need to overload assignment operator or the default = operator works fine between two standard lists.
Um, nor sure what you mean - overloading the assignment opeator is only nessecary for assignments, not for comparisons. But, in  a bigger picture, it'd be similar.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.