Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

New GoF design pattern

Name: Anonymous 2014-04-29 4:31

CONSTIPATED ENCAPSULATION

#include <iostream>

class Anus
{
public:
Anus(int n=123):hax(&num) { num=n; }
void setNum(int n) { num=n; }
const int *const hax;
private:
int num;
} anus;

int main()
{
std::cout << *anus.hax << "\n";

anus.setNum(42);
std::cout << *anus.hax << "\n";

return 0;
}

Name: Anonymous 2014-04-29 12:24

Actually this pattern is pretty cool: your public data member is protected[1] and, at the same time, you avoid stupid calls to getters.

I would change just one stupid thing:
- Anus(int n=123):hax(&num) { num=n; }
+ Anus(int n=123):hax(&num),num(n) {}

___________________
[1] - "protected" at compile time: you always can cast the l-value and modify the content, but reinterpreting pointers is always possible and dangerous.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List