I'm now trying to really get a handel on pointers. I must admit this stuff
has always been confusing to me. The code below produces an unexpected
result when it is run. The last line of output is "HelloClass::memberIntPtr
= -1073747936". I expected it to be "HelloClass::memberIntPtr = 1848", as it
is in the previous invocation of sayHello(). My guess as to what is
happening is that my use of a member variable of type string, and then
assigning to that variable is resulting in and over write of the memory
pointed to by HelloClass::memberPtr.
Is my understanding of what's happening correct? What's the proper remedy for
this situation?
#include <iostream> // cout, cin
#include <string>
using namespace std;
class HelloClass {
public:
HelloClass::HelloClass();
void sayHello();
/** Accessors for HelloClass::message */
void setMessage(string messageStr="This is the default setMessage message.");
string getMessage();
/** Accessors for HelloClass::messagePtr */
void setMessagePtr(string messageStr="This is the default setMessagePtr
message.");
string getMessagePtr();
/** Accessors for HelloClass::memberInt */
void setMemberInt(int memberInt=2112);
int getMemberInt();
/** Accessors for HelloClass::memberIntPtr */
void setMemberIntPtr(int memberIntPtr=1848);
int getMemberIntPtr();
private:
string message;
string *messagePtr;
int memberInt;
int *memberIntPtr;
};
/** Constructor: initialize all member variables. */
HelloClass::HelloClass(){
this->message = "c++bean message. Assigned in constructor.";
this->messagePtr = (new string("c++bean messagePtr. Assigned in
constructor."));
this->memberInt = 42;
int temp = 100760;
this->memberIntPtr = &temp;
}
/** Accessors for HelloClass::message */
void HelloClass::setMessage(string messageStr){
this->message = messageStr;
}
string HelloClass::getMessage(){
return this->message;
}
/** Accessors for HelloClass::messagePtr */
void HelloClass::setMessagePtr(string messageStr){
this->messagePtr = new string(messageStr);
}
string HelloClass::getMessagePtr(){
return *this->messagePtr;
}
/** Accessors for HelloClass::memberInt */
void HelloClass::setMemberInt(int memberInt){
this->memberInt = memberInt;
}
int HelloClass::getMemberInt(){
return this->memberInt;
}
/** Accessors for HelloClass::memberIntPtr */
void HelloClass::setMemberIntPtr(int memberIntPtr){
this->memberIntPtr = &memberIntPtr;
}
int HelloClass::getMemberIntPtr(){
return *this->memberIntPtr;
}
/** Output the properties of HelloClass*/
void HelloClass::sayHello() {
cout << "HelloClass::message = " << this->getMessage() << endl;
cout << "HelloClass::messagePtr = " << this->getMessagePtr() << endl;
cout << "HelloClass::memberInt = " << this->getMemberInt() << endl;
cout << "HelloClass::memberIntPtr = " << this->getMemberIntPtr() << endl;
cout << "___________________________" << endl;
}
main() {
HelloClass h;
h.sayHello();
h.setMessage();
h.setMessagePtr();
h.setMemberInt();
h.setMemberIntPtr();
h.sayHello();
h.setMessage("User defined \"Hello World\" message.");
h.sayHello();
return 0;
}