I am confused with these concepts.
Hi, all:
This may be a silly question. But I just do not get it.
Why sometime we need to new a class to call its functions or to get it
public properties. But sometime we just simply declare it and then we
can access its funnctions and properties?
Also, sometime, I need to pur Class myClassName at the top of another
class to compile, but sometime I do not need?
I guess give an example may explain myself more clear.
I have an sample class to represent my members (id and name, to
simplify)
typedef struct {
int ID;
std::string Name;
} MembData;
typedef std::vector<MembData> MembVec;
typedef MembVec::iterator MembItr;
class CMembDataClass
{
public:
CMembDataClass(void);
~CMembDataClass(void);
void Load(); //populate memb's data list into m_vecMemb
bool Remove(MembData aData);
bool Append(MembData aData);
bool Update(MembData aData);
MembVec m_vecMemb;
};
I have another class to use this MembDataClass, let call it
myDisctionary.
I always thought I need to new the MembDataClass (instantialize it to
get the public variable m_vecMemb) :
include "MembDataClass.h"
....
MembVec m_membList;
CMembDataClass* pMyMemb = new CMembDataClass();
pMyMemb->Load();
m_membList = pMyMemb->m_vecMemb;
But I also tried:
CMembDataClass myMemb;
myMemb.Load();
m_membList = myMemb.m_vecMemb;
I thought it would give me error like access violation something, but
it works.
in this case: I need to put class CMembDataClass; in CMyDisctionary.h
and if I did not call new to instantialize the MembDataClass, where is
the data stored at?
is it because my MembData is struct, if it is another class, would it
be a different behavior? and what is the case if m_vecMemb is a
pointer?
thanks in advance\
-rockdale