Re: I am confused with these concepts.
Thanks a lot for these replies.
Yes, I am from Java background and maybe that's the reason I am
confused with these "simple". things.
I thought any user defined classes you must use new to instantialize
it whereas internal data type (like int, long etc ) you do not new it
since it stored in the stack. Whenever you "new" a class. the memory
it occupied is allocated in the heap and you need to delete the class
to free its memory. I never think that the user defined classes can be
stored in the stack.
that's why
(note how you never used 'new' on m_vecMemb, which itself is an instance
of class vector<MembData>). I never use new to create my Vector but I used=
new to create my Class.
When you write, say,
int f() {
int x = 1;
return x;
}
do you also wonder where x is stored at?
this one I know x is stored in the stack and when the function exits,
the memory x occupied is cleared. .
and what is the case if m_vecMemb is a
pointer?
What do you feel would be a problem with this?
After your explaination, I do see a problem at all if m_vecMemb is a
pointer, since the memory it point to is there.
Here, I have another Question.
I always been told that you can not new a class in a function then
return its pointer, since the memory the pointer pointed to will be
cleared when the function exits. That's why sometime I get the "access
violation" error, I think. In this case we need to new the class
outside the function, then pass in the pointer, in the function,
manipulate the data the pointer point to, this is the right way, is
it? like:
CMembDataClass* f(){
CMembDataClass* pMyMemb = new CMembDataClass;
return pMyMemb;
}
this is wrong since
1. no delete to free the memory
2. the returned pointer pointed to a invalid memory, since as the
function exits, the memory allocated to pMyMemb is not available
anymore
void f(CMembDataClass* pMyMemb)
{
pMyMemb->.....
}
What should I do to the Class not the pointer?
CMembDataClass f(){
???
}
Or should I pass in the reference of CMembDataClass as parameter ?
As I said in the first post, I never tried to do this :
CMembDataClass myMemb;
myMemb.Load();
m_membList = myMemb.m_vecMemb;
But I am suprised that it works and now I am more happier because your
guys help, I understand why it worked and I do not need to new a class
to "Instantialize" it.
Again, thanks.
-rockdale
On Jan 26, 11:04 pm, "Igor Tandetnik" <itandet...@mvps.org> wrote:
"rockdale" <rockdale.gr...@gmail.com> wrote in message
news:2b81376f-c141-4672-9c6a-a0a5cc3e70ea@q39g2000hsf.googlegroups.com
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?
Doesn't your favorite C++ textbook explain the difference between
automatic objects and dynamically allocated ones? You should throw it
away then, and get a better one.
Also, sometime, I need to pur Class myClassName at the top of another
class to compile, but sometime I do not need?
Well, if that other class doesn't use myClassName in any way, then of
course myClassName doesn't need to be mentioned.
I have an sample class to represent my members (id and name, to
simplify)
typedef struct {
int ID;
std::string Name;
} MembData;
What do you think you need a typedef for? Why not just
struct 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) :
You sound like instantiating and using 'new' is the same thing. This is
not the case. 'new' is not the only way to create an instance of a class
(note how you never used 'new' on m_vecMemb, which itself is an instance
of class vector<MembData>).
Are you coming from Java background, by any chance?
CMembDataClass* pMyMemb = new CMembDataClass();
pMyMemb->Load();
m_membList = pMyMemb->m_vecMemb;
You also need to do
delete pMyMemb;
when you are done with it. Otherwise, you are leaking resources.
But I also tried:
CMembDataClass myMemb;
myMemb.Load();
m_membList = myMemb.m_vecMemb;
I thought it would give me error like access violation something
And exactly why would you believe such a thing? After all, you don't
appear to be surprised that you can do
MembVec m_membList;
but feel "CMembDataClass myMemb;" is a problem. What, in your opinion,
is the major difference between the two statements?
and if I did not call new to instantialize the MembDataClass, where is
the data stored at?
Read about automatic variables (aka local variables, aka variables with
automatic storage duration) in your C++ textbook.
When you write, say,
int f() {
int x = 1;
return x;
}
do you also wonder where x is stored at? For that matter, when you write
CMembDataClass* pMyMemb = new CMembDataClass();
where is the memory for pMyMemb (the pointer itself, not the pointed-to
object) come from? Think about it.
is it because my MembData is struct, if it is another class, would it
be a different behavior?
In C++, struct and class are almost the same. The only difference is
that members of a struct are public by default, and members of a class
are private by default. In other words,
struct S {...};
is equivalent to
class S {
public:
...
};
The keyword 'struct' remains in C++ mostly for compatiblity with C.
and what is the case if m_vecMemb is a
pointer?
What do you feel would be a problem with this?
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925- Hide quoted text -
- Show quoted text -