Skip to content

Instantly share code, notes, and snippets.

@safiire
Created April 30, 2015 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save safiire/9ea3c2c0144758b14e6f to your computer and use it in GitHub Desktop.
Save safiire/9ea3c2c0144758b14e6f to your computer and use it in GitHub Desktop.
Example of returning a string initialized in the constructor
// Comple with g++ *.cpp -o prog //
#include <iostream>
#include "test.h"
using namespace std;
int main(int argc, char **argv){
Test test;
cout << "My name is " << test.serialize() << endl;
return 0;
}
#include "test.h"
Test::Test() : m_name("saf") {}
string Test::serialize(){
return m_name;
}
#ifndef TEST_H
#define TEST_H
#include <string>
using namespace std;
class Test {
private:
string m_name;
public:
Test();
// Be aware that this returns a copy of the string using
// the default copy constructor
string serialize();
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment