vector<CMyClass>

Hi.
If I want to use a vector<> for a type CMyClass, CMyClass has to overload some operators (=, ==, <).
Can anyone give me an example of how this is done. I'm looking for the simplest implementation.
Here's CMyClass's definition:
CMyClass
{
private:
    int m_nID;
public:
    int GetID() { return m_nID; };
};

I want to use:
vector<CMyClass> v1;

Can anyone complete this code?
Thanks.

LVL 4
AssafLavieAsked:
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.

ZoppoCommented:
Hi AssafLavie,

somehow like this:

CMyClass
{
private:
 int m_nID;
public:
 int GetID() { return m_nID; };
 CMyClass& operator = (CMyClass& src)
 {
  if ( this != &src )
   m_nID = src.m_nID;
 }
 BOOL operator == (CMyClass& src)
 {
  if ( m_nID == src.m_nID )
   return TRUE;
  return FALSE;
 }
 BOOL operator < (CMyClass& src)
 {
  if ( m_nID < src.m_nID )
   return TRUE;
  return FALSE;
 }
 // perhaps you need a copy ctor ...
 CMyClass( CMyClass& src )
 {
  (*this) = src;
 }
};

hope that helps,

ZOPPO
0
jasonclarkeCommented:
Depends what you want to do with it.  To just put it in a vector nothing extra is required.

Here is some code that does some stuff with a vector of CMyClass.  (the operator== is only required for the find algorithm):

#include <vector>
#include <algorithm>

using namespace std;

class CMyClass
{
private:
    int m_nID;
public:
    CMyClass(int id) : m_nID(id) {}
    int GetID() { return m_nID; };

    bool operator==(const CMyClass& rhs)
    { return m_nID == rhs.m_nID; }
};



void main()
{
    vector<CMyClass> v1;

    CMyClass mc1(12);
    CMyClass mc2(8);

    v1.push_back(mc1);
    v1.push_back(mc2);

    v1[0].GetID(); // Calls getID on mc1

    // Iterate over the collection using vector iterator
    vector<CMyClass>::iterator it = v1.begin();
    while (it != v1.end())
    {
        // Do something with value
        (*it).GetID();
        ++it;
    }

    // Iterate over the collection using vector indexing
    for (int i=0; i<v1.size(); i++)
    {
        v1[i].GetID();
    }

    // Call an STL algorithm
    vector<CMyClass>::iterator j = std::find(v1.begin(), v1.end(), 8);
    if (j != v1.end())
    {
        // Found it!
        (*j).GetID();
    }
}
0
AssafLavieAuthor Commented:
Thanks for your comments. I turns out I only needed a copy ctor and a = operator.
I am having some trouble with the operator=:
CMyClass
{
....
CMyClass& operator=(CMyClass& src)
{
....
return this; // This won't compile.
}

I need to return a reference to the modified object, but &this or this do not compile. I'm getting something like:
"Cannot convert from 'class CMyClass *const ' to 'class CMyClass &'" (for "return this").
Why is that?
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

ZoppoCommented:
Oh, ups, sorry, I missed it:

CMyClass& operator = (CMyClass& src)
{
 if ( this != &src )
  m_nID = src.m_nID;
 return (*this);
}

ZOPPO
0
KangaRooCommented:
It should be

   ...
   return *this; // the asterix!
0
AssafLavieAuthor Commented:
Thanks! That helped.
But the vector is still giving my trouble.
The copiler complains: "CMyClass: no copy ctor available".
This is because the parameter to the copy ctor method is of type "CMyClass&" and not "const class CMyClass&" (as the vector demands I guess).

I tried defining the copy ctor and the operator with "const class..." types - but then, the compiler won't let me access the public methods of the src parameter saying: "'GetID' : cannot convert 'this' pointer from 'const class CMyClass' to 'class CMyClass &'".

Uh!
Any clues?
0
AssafLavieAuthor Commented:
Thanks! That helped.
But the vector is still giving my trouble.
The copiler complains: "CMyClass: no copy ctor available".
This is because the parameter to the copy ctor method is of type "CMyClass&" and not "const class CMyClass&" (as the vector demands I guess).

I tried defining the copy ctor and the operator with "const class..." types - but then, the compiler won't let me access the public methods of the src parameter saying: "'GetID' : cannot convert 'this' pointer from 'const class CMyClass' to 'class CMyClass &'".

Uh!
Any clues?
0
ZoppoCommented:
try to declare the copy constructor like this:

CMyClass {
....
 CMyClass( CMyClass const& src )
 {
 ...
 }
....
};

hope that helps,

ZOPPO
0
AssafLavieAuthor Commented:
Well that took care of the vector's complaints, but I still san't compile because I get: "cannot convert 'this' pointer from 'const class CMyClass' to 'class MyClass &" whenever I try to access the public methods of the src parameter.
0
ZoppoCommented:
This is code I've tested for compiliance:

--------------------------------------------------
class CMyClass
{
private:
 int m_nID;
public:
 int GetID() { return m_nID; };
 CMyClass& operator = (CMyClass const& src)
 {
  if ( this != &src )
   m_nID = src.m_nID;
  return (*this);
 }
 BOOL operator == (CMyClass const& src )
 {
  if ( m_nID == src.m_nID )
   return TRUE;
  return FALSE;
 }
 BOOL operator < (CMyClass const& src)
 {
  if ( m_nID < src.m_nID )
   return TRUE;
  return FALSE;
 }
 CMyClass( CMyClass const& src )
 {
  (*this) = src;
 }
 CMyClass( int id )
 {
  m_nID = id;
 }
};

#include <vector>
#include <algorithm>
using namespace std;

int main(int , char* [])
{
//      CTestClass cl;

  vector<CMyClass> v1;
 
  CMyClass mc1(12);
  CMyClass mc2(8);
 
  v1.push_back(mc1);
  v1.push_back(mc2);
....
}
--------------------------------------------------

ZOPPO
0
jasonclarkeCommented:
Thats because the public methods of the source parameter are probably not const.  

You have a couple of choices with a copy constructor for this:

i) make the used methods const, if they are used in a copy constructor, they probably should be.

ii) bypass the methods and use the internal representation directly.  You can do this in a copy constructor with no problem, so instead of

   m_nID = src.GetID()

use:

   m_nID = src.m_nID

or whatever.  (There is no violation of encapsulation since you are in the same class).

0
AssafLavieAuthor Commented:
Well that took care of the vector's complaints, but I still san't compile because I get: "cannot convert 'this' pointer from 'const class CMyClass' to 'class MyClass &" whenever I try to access the public methods of the src parameter.
0
nietodCommented:
>> "cannot convert 'this' pointer from
>> 'const class CMyClass' to 'class MyClass &" 
>> whenever I try to access the public
>> methods of the src parameter.
In the copy constructor and the operator = procedure (and possibly other places) you are passing a constant MyClass object as a parameter.  When an object is constant, you can only access its constant member procedures, for example

class X
{
public:
     int NonConstProc() { return 1; };
     int  ConstProc() const { return 2; };
};

function(const X&AnX)
{
    AxX.NonConstProc(); // Error.
    AnX.ConstProc(); // Okay.
};

So you should declare any procedure that does no change an object as a constant procedure.  That is, procedures that return information about an ojbect, but don't change the object should be constant.
0

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
AssafLavieAuthor Commented:
Thanks alot.
That last suggestion hit the spot.
I should say I simply did not know that there is a difference between
const int a()
and
const int a() const
....
Mind elaborating on that a bit?
0
jasonclarkeCommented:
Zoppo, to declare the argument to the copy constructor:

 CMyClass const& src

makes no difference, the other form:

const CMyClass& src

means the same and is a lot more common in use.  The problem is that a non-const method is being called.  If you change your assignment operator to:
 
  if ( this != &src )
   m_nID = src.GetID();
  return (*this);

you will get the same problem.

It can be fixed by defining GetID as:

int GetID() const { return m_nID; };

or by writing it as you had it originally.

 

0
nietodCommented:
Just to keep the record straight, I did not answer the question.  I didn't think I deserved the points.  AssafLavie, accepted my comment as an answer.

>> I should say I simply did not know that
>> there is a difference between
>> const int a()
>> and
>> const int a() const

>> Mind elaborating on that a bit?

The first function returns a constant int, that is an int that can't e changed.  (Actually the value it returns could not be changed for other reasons anyways, so the constant is redundant.)

The send function is a a syntax error, unless it is a member function.  Only a member function can be declared with that "const" at the end.  This indicates that the member function treats the object that it works on as constant. So the function cannot change any of its data members  (unless they are mutable) and cannot call any other member functions that might change data members (other non-constant functions. )

for example

class X
{
  int i;
public:
   NonConstFun(int j)
   {
        i = j; // Okay, not constant.
   };
   ConstFun(int j) const
   {
        i = j; // ERROR.  can't change data of constant object.
       NonConstFun(j); // ERROR can't call non-constant member.
   };
};  
0
nietodCommented:
Note that this is a very basic element of C++'s OOP design.  If you don't know this, you probably don't know a lot of other basic things too--and that can be dangerous.  You probalby should invest in some basic C++ texts.  (Usually more than one.)
0
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
Editors IDEs

From novice to tech pro — start learning today.