Skip to content

Instantly share code, notes, and snippets.

@csullivan
Created January 21, 2016 00:34
Show Gist options
  • Save csullivan/433a3fdeaa77b94720b9 to your computer and use it in GitHub Desktop.
Save csullivan/433a3fdeaa77b94720b9 to your computer and use it in GitHub Desktop.
A snippet detailing how a class may have a reference member
#include <iostream>
using namespace std;
class B {
public:
B() {x = 0;}
~B() {;}
int x;
void incr() {x++;}
};
class A {
public:
A () : refB(*new B()) {;}
A(B& b) : refB(b) {;}
const B& refB;
};
int main()
{
B bb;
A aa(bb);
// aa.refB.incr(); //error
A aaa;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment