Skip to content

Instantly share code, notes, and snippets.

@svenoaks
Created January 20, 2014 03:06
Show Gist options
  • Save svenoaks/8514257 to your computer and use it in GitHub Desktop.
Save svenoaks/8514257 to your computer and use it in GitHub Desktop.
Passing an object with pointers by value
#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