Created
January 20, 2014 03:06
-
-
Save svenoaks/8514257 to your computer and use it in GitHub Desktop.
Passing an object with pointers by value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
class passed | |
{ | |
char *str; | |
public: | |
passed() | |
{ | |
str = new char[1]; | |
} | |
void setChar(char c) | |
{ | |
str[0] = c; | |
} | |
char getChar() const | |
{ | |
return str[0]; | |
} | |
~passed() | |
{ | |
//delete[] str; | |
} | |
/* | |
passed(const passed& other) | |
{ | |
str = new char[1]; | |
str[0] = other.getChar(); | |
} | |
*/ | |
void print() | |
{ | |
cout << "The value of char[0] is: " << str[0] << endl; | |
} | |
}; | |
void doModify(passed p) | |
{ | |
//p.print(); | |
p.setChar('b'); | |
} | |
int main() | |
{ | |
passed p{}; | |
p.setChar('a'); | |
p.print(); | |
doModify(p); | |
p.print(); | |
system("pause"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment