C++ question...
Hi all,
I''m a newbie in C++ so be patient!!
I created a class and it''s ok. Now when I create an instance of this class, what is the difference between:
CLASS c = CLASS;
and
CLASS* c = new CLASS;
I noticed, that using the first example, I access the members:
c.FunctionA
whilst with the second method, I access with:
c->FunctionA
Ivan
The first time, you created an object, while the second time you created a pointer.
The main difference is that a pointer can be NULL :
MyClass c1; // an object is created
MyClass* c2; // no object is created
now you can "assign" a class to c2, MyClass or a child class of MyClass.
c2=new MyClass(...);
And don''t forget to destroy c2 in order to desallocate the memory!
delete c2;
------------------------
- Seby -
www.dfhi-bomber.fr.st
The main difference is that a pointer can be NULL :
MyClass c1; // an object is created
MyClass* c2; // no object is created
now you can "assign" a class to c2, MyClass or a child class of MyClass.
c2=new MyClass(...);
And don''t forget to destroy c2 in order to desallocate the memory!
delete c2;
------------------------
- Seby -
www.dfhi-bomber.fr.st
If you have a scope (the space between any pair of braces "{" and "}" ), and you have an object ("CLASS c;"), then the destructor will be called when the program gets to the end of the scope ("}").
If you have a pointer, then that is not the case. You will have to manually delete the data. Depending on what you want to do, you will want one or the other.
e.g.:
That was the basic answer. To get more in depth let's look at another example:
func3 uses what is known as pass by value. When something is passed by value, a temporary copy is made (using a "Copy Constructor") and that temporary copy is deleted when the function exists (leaves the scope of the function -the "}"). The default copy constructor uses a memcpy (C function that does verbatim copy of a block of memory) which means all pointers are copies of the original's pointers (pointing at the same memory).
So now you have two pointers pointing at the same data. The second one is automatically cleaned up, and deletes the memory. The original has no idea the other pointer deleted what its pointing to and it will happily try to use that memory. Using a pointer to memory that isn't valid, like this causes a "segfault" (segmentation fault).
How I would fix this simple situation would be:
Work with a bit, experiment, and if you have any further questions, ask away. I would give it something like two hours of playing with before asking questions. This will give you a chance to FIOFYF.
gl hf
[edited by - flangazor on September 4, 2003 5:06:49 AM]
If you have a pointer, then that is not the case. You will have to manually delete the data. Depending on what you want to do, you will want one or the other.
e.g.:
void func(){CLASS c;CLASS* p = new CLASS;//...} // c.~CLASS() called automatically//memory leak! p->~CLASS() is never called, and you lost p, so you will not be able to find the data again!void func2(){CLASS c;CLASS* p = new CLASS;//...delete p; // might have been more useful to use non pointer since this delete would have been automatically called...}// c.~CLASS() called automatically
That was the basic answer. To get more in depth let's look at another example:
class CLASS{public: CLASS(): p(new poo){} ~CLASS(){delete p;}private: poo* p;};void func3(CLASS c){ //...}// c.~CLASS called automatically! c.p is deleted.CLASS c;func3(c);// errorCLASS* p = new CLASS;func3(p); // p is no longer valid due to the implicit deletion!
func3 uses what is known as pass by value. When something is passed by value, a temporary copy is made (using a "Copy Constructor") and that temporary copy is deleted when the function exists (leaves the scope of the function -the "}"). The default copy constructor uses a memcpy (C function that does verbatim copy of a block of memory) which means all pointers are copies of the original's pointers (pointing at the same memory).
So now you have two pointers pointing at the same data. The second one is automatically cleaned up, and deletes the memory. The original has no idea the other pointer deleted what its pointing to and it will happily try to use that memory. Using a pointer to memory that isn't valid, like this causes a "segfault" (segmentation fault).
How I would fix this simple situation would be:
void func4(const CLASS &c){// we can only call public const member functions here.// c is pretty much guaranteed to not be changed}//c is not automatically deleted. hooray!//orvoid func5(CLASS &c){// we can call any public member functions here.// the state of c is not guaranteed to be the same afterwards.}// c is not automatically deleted
Work with a bit, experiment, and if you have any further questions, ask away. I would give it something like two hours of playing with before asking questions. This will give you a chance to FIOFYF.
gl hf
[edited by - flangazor on September 4, 2003 5:06:49 AM]
CLASS c = CLASS;
the object is in the stack
CLASS* c = new CLASS;
the object is on the heap and must be deleted (to avoid memory leaks)
the object is in the stack
CLASS* c = new CLASS;
the object is on the heap and must be deleted (to avoid memory leaks)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement