can you reassign a base class pointer to various derived class objects.
ie
BaseClass *ptrBaseClass=new BaseClass;
*ptrBaseClass=*ptrFirstDerivedClass;
*ptrBaseClass=*ptrSecondDerivedClass;
etc.
can you reassign a base class pointer to various derived class objects.
ie
BaseClass *ptrBaseClass=new BaseClass;
*ptrBaseClass=*ptrFirstDerivedClass;
*ptrBaseClass=*ptrSecondDerivedClass;
etc.
That is called object slicing,not good.Originally posted by curlious
BaseClass *ptrBaseClass=new BaseClass;
*ptrBaseClass=*ptrFirstDerivedClass;
*ptrBaseClass=*ptrSecondDerivedClass;
etc.
That is correct
Code:BaseClass *ptrBaseClass=new BaseClass; ptrBaseClass = ptrFirstDerivedClass; ptrBaseClass = ptrSecondDerivedClass;
Thanks!
I thought about it after I posted and wasn't sure weather *ptr could even be an l-value.
While that is the correct way to make a base class pointer point to a derived class, it is of course a memory leak if used the way shown above, since the BaseClass object created with new will be lost without being destroyed.Code:BaseClass *ptrBaseClass=new BaseClass; ptrBaseClass = ptrFirstDerivedClass; ptrBaseClass = ptrSecondDerivedClass;
Yes. * means "the object pointed at by". For example:Originally posted by curlious
Thanks!
I thought about it after I posted and wasn't sure weather *ptr could even be an l-value.
i has type "int *", *i has type "int".Code:int * i = new int; *i = 25; delete i;
You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.