void main() { // using a bit of random here. srand(time(nullptr)); //initialize random seed based on time // Where will this object live? Is this a good choice? Why or why not? TestObject anObject = TestObject(30); // Name differences between pointers and references TestObject *anotherObject = new TestObject(2); //error: new returns a pointer-> '*' // When should we use pointers, references and values? why? TestObject &referenceToAnObject = anObject; //error: no & TestObject &referenceToAnotherObject = *anotherObject; //error original adress called with '*' TestObject* pointerToAnObject = &anObject; //error: for pointer,adress of obj. is needed with & TestObject* pointerToAnotherObject = &referenceToAnotherObject; // error fix: for pointer, adress of Obj. is needed with '&' // Testing if the correct strings are being printed. referenceToAnotherObject.printAllStrings(); std::cout << "The Following should be the same as above: " << std::endl; pointerToAnotherObject->printAllStrings(); std::cout << std::endl << "total number of strings:" << std::endl; int& TotalElementNumber = sumTheElements(referenceToAnObject.elementCount(), referenceToAnotherObject.elementCount()); std::cout << TotalElementNumber << std::endl; std::cout << "Do calculations in another scope and display the same value again inside that scope: " << std::endl; { //this isn't really doing anything, just for testing purposes int ignoreMe = 20; ignoreMe *= TotalElementNumber; std::cout << TotalElementNumber << std::endl; } std::cout << "And again outside the scope: " << std::endl; std::cout << TotalElementNumber << std::endl; delete anotherObject; //cant delete objects with stack //delete pointerToAnObject; //delete pointerToAnotherObject; }
void main() { // using a bit of random here. srand(time(nullptr)); //initialize random seed based on time // Where will this object live? Is this a good choice? Why or why not? // This Object will be on the Stack. Not so good, because we have a pointer to it later // its not so bad here, but normally it is bad to have a pointer to something on the stack // because if the scope ends and the pointer continues to exist, it can point to something useless TestObject anObject = TestObject(30); // Name differences between pointers and references // Pointer can be changed, references not after initialization. pointer can be null, refer. not. References can be used like the object and need no dereference TestObject* anotherObject = new TestObject(2); // When should we use pointers, references and values? why? // pointers for optional parameters // references for any none-elemental datatypes // values for small (32/64Bit) datatype and no change //reference is initialized with variable name, not adress TestObject& referenceToAnObject = anObject; // same here, so we have to dereference the pointer "anotherObject" first TestObject& referenceToAnotherObject = *anotherObject; // pointer is initialized with adress, so we have to get ADRESS from "anObject" TestObject* pointerToAnObject = &anObject; // same here, although referenceToAnotherObject is a reference(basically a adress too) // it is used like the object itself, so we need to GET the ADRESS here too TestObject* pointerToAnotherObject = &referenceToAnotherObject; // Testing if the correct strings are being printed. referenceToAnotherObject.printAllStrings(); std::cout << "The Following should be the same as above: " << std::endl; pointerToAnotherObject->printAllStrings(); std::cout << std::endl << "total number of strings:" << std::endl; int& TotalElementNumber = sumTheElements(referenceToAnObject.elementCount(), referenceToAnotherObject.elementCount()); std::cout << TotalElementNumber << std::endl; std::cout << "Do calculations in another scope and display the same value again inside that scope: " << std::endl; { //this isn't really doing anything, just for testing purposes int ignoreMe = 20; ignoreMe *= TotalElementNumber; std::cout << TotalElementNumber << std::endl; } std::cout << "And again outside the scope: " << std::endl; std::cout << TotalElementNumber << std::endl; delete anotherObject; // these pointers have not been initialized with new, so cannot be deleted //delete pointerToAnObject; //delete pointerToAnotherObject; }
int main() { // using a bit of random here. srand(time(nullptr)); //initialize random seed based on time // Where will this object live? Is this a good choice? Why or why not? // stack. the stack/heap choice does not really matter here, because the object itself allocates the bulk of its space on the heap. TestObject anObject = TestObject(30); // Name differences between pointers and references // # References: // * cannot point to unknown space // * cannot be changed after definition // * behave like the variable they reference most of the time // * immutable // # Pointers: // * can point to whatever // * can be changed whenever // * behave like a pointer // * mutable TestObject anotherObject = TestObject(2); // When should we use pointers, references and values? why? // use the value if it is very small, and changes in the function are not required out of it // use a pointer if the object does exist at definition time, or when a parameter may be null // use a reference whenever you can TestObject &referenceToAnObject = anObject; TestObject &referenceToAnotherObject = anotherObject; TestObject* pointerToAnObject = &anObject; TestObject* pointerToAnotherObject = &referenceToAnotherObject; // Testing if the correct strings are being printed. referenceToAnotherObject.printAllStrings(); std::cout << "The Following should be the same as above: " << std::endl; pointerToAnotherObject->printAllStrings(); std::cout << std::endl << "total number of strings:" << std::endl; const int& TotalElementNumber = sumTheElements(referenceToAnObject.elementCount(), referenceToAnotherObject.elementCount()); std::cout << TotalElementNumber << std::endl; std::cout << "Do calculations in another scope and display the same value again inside that scope: " << std::endl; { //this isn't really doing anything, just for testing purposes int ignoreMe = 20; ignoreMe *= TotalElementNumber; std::cout << TotalElementNumber << std::endl; } std::cout << "And again outside the scope: " << std::endl; std::cout << TotalElementNumber << std::endl; // everything is on the stack, nothing needs to be deleted //delete pointerToAnObject; //delete pointerToAnotherObject; return 0; }