Say you have this code:Why wouldn't you just use this:Code:float *Pi = new float;
*Pi = 3.141592;
delete Pi;
Code:float Pi = 3.141592;
Printable View
Say you have this code:Why wouldn't you just use this:Code:float *Pi = new float;
*Pi = 3.141592;
delete Pi;
Code:float Pi = 3.141592;
You could. Using a pointer in that situation is just dumb. A much better example would be:
Now you have, basically, an array of floats. Then you can delete it as above. Another example is:Code:float* floats = new float[1000];
in this case, you are passing a pointer(DWORD), instead of the entire struct, which increases speed. You can also create pointers to functions, which help a lot when doing some things (callback functions). Its all in knowing when to use a pointer. In your example, there is just no reason. But other times, is speeds execution time up, by not haveing to copy data from location to location.Code:myStruct* strct = new myStruct;
SomeFunc(strct);
...
where sizeof(myStruct) > 4
Z.
One thing you missed::)Code:float *i = new float;
delete i;
float *j = new float[100];
delete[] j;
more reasons: the C++ STL library
Advantages:Code:#include <vektor>
using namespace std;
class CParentClass
{ // members
}
class CFirstChildClass : public CParentClass
{ // Blabla
}
class CSecondChildClass : public CParentClass
{ // Whatever
}
void SomeFunc()
{
CParentClass *pFC = new CFirstChildClass;
// ask for user input and fill class members
// insert the pointer into a vektor <CParentClass*> object that is always available
}
void SomeOtherFuncThatIsCalledAtAnEntirelyOtherTime()
{
// you can now access the object
}
1) Polymorphic access: It doesn't matter which child class the contents of the vektor are.
2) You still have exactly the object you created, not a copy as if you were using vektor<CParentClass>
3) If you used CFirstChildClass obj; and inserted &obj into the vektor, obj would be out of scope in SomeOtherFunc... and most probably deleted.
vector not vektor :D :p
You don't need the STL to use virtual inheritance, which is what goes on here:Code:class Parent {
public:
virtual void Func() = 0;
};
class ChildA : public Parent {
public:
void Func() { cout << "Die!" << endl; }
};
class ChildB : public Parent {
public:
void Func() { cout << "Live!" << endl; }
};
void somecode() {
Parent *ptr;
ChildA *thing = new ChildA;
ChildB *other = new ChildB;
ptr = thing;
ptr->Func();
ptr = other;
ptr->Func();
delete thing;
delete other;
}
true, but this way I can at the same time show the "no copy" and the polymorphic advantages.
Okay then ;) I just wouldn't mix templates in if they're still on the "*** do pointers do" stage :)
Not quibbling, though :)
Yeah, maybe write an essay about the difference between heap and stack? :D
Aaaargghhh yeah!
Damn that really messed me up when I was learning. Hmm...perhaps another FAQ entry? :)
It's just that I don't have time for it at the moment.
Heh, that's pretty much me. I know the basics of what they do, but I'm still a little fuzzy on it.Quote:
Originally posted by parksie
Okay then ;) I just wouldn't mix templates in if they're still on the "*** do pointers do" stage :)