Created
January 21, 2016 00:34
-
-
Save csullivan/433a3fdeaa77b94720b9 to your computer and use it in GitHub Desktop.
A snippet detailing how a class may have a reference member
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> | |
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