Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Widget
- {
- Widget():x(0), y(0), z(0){}
- int x, y, z;
- };
- struct WidgetHolder //Just a simple struct to hold four Widgets.
- {
- WidgetHolder(Widget a, Widget b, Widget c, Widget d): A(a), B(b), C(c), D(d){}
- Widget& A;
- Widget& B;
- Widget& C;
- Widget& D;
- };
- class Test //This class uses four widgets internally, and must provide access to them externally.
- {
- public:
- const WidgetHolder AccessWidgets() const
- {
- //This should return our four widgets, but I don't want anyone messing with them.
- return WidgetHolder(A, B, C, D);
- }
- WidgetHolder AccessWidgets()
- {
- //This should return our four widgets, I don't care if they get changed.
- return WidgetHolder(A, B, C, D);
- }
- private:
- Widget A, B, C, D;
- };
- int main()
- {
- const Test unchangeable;
- unchangeable.AccessWidgets().A.x = 1; //Why does this compile, shouldn't the Widget& be const?
- }
- WidgetHolder(Widget a, Widget b, Widget c, Widget d): A(a), B(b), C(c), D(d){}
- WidgetHolder(Widget &a, Widget &b, Widget &c, Widget &d): A(a), B(b), C(c), D(d){}
- struct ConstWidgetHolder
- {
- ConstWidgetHolder(const Widget &a, const Widget &b, const Widget &c, const Widget &d): A(a), B(b), C(c), D(d){}
- const Widget& A;
- const Widget& B;
- const Widget& C;
- const Widget& D;
- };
- class Test
- {
- public:
- ConstWidgetHolder AccessWidgets() const
- {
- return ConstWidgetHolder(A, B, C, D);
- }
- class vector {
- iterator begin();
- const_iterator begin() const;
- class vector {
- iterator begin();
- const_iterator begin() const;
- #include <iostream>
- class Widget
- {
- int x;
- public:
- Widget(int inX) : x(inX){}
- ~Widget() {
- std::cout << "widget " << static_cast< void*>(this) << " destroyed" << std::endl;
- }
- };
- struct WidgetHolder
- {
- Widget& A;
- public:
- WidgetHolder(Widget a): A(a) {}
- const Widget& a() const {
- std::cout << "widget " << static_cast< void*>(&A) << " used" << std::endl;
- return A;
- }
- };
- int main(char** argv, int argc)
- {
- Widget test(7);
- WidgetHolder holder(test);
- Widget const & test2 = holder.a();
- return 0;
- }
- #include <stdio.h>
- class Test
- {
- public:
- Test(int v){m_v = v;}
- ~Test(){printf("Destruct value = %dn",m_v);}
- int& GetV(){printf ("None Const returning %dn",m_v); return m_v; }
- const int& GetV() const { printf("Const returning %dn",m_v); return m_v;}
- private:
- int m_v;
- };
- void main()
- {
- // A none const object (or reference) calls the none const functions
- // in preference to the const
- Test one(10);
- int& x = one.GetV();
- // We can change the member variable via the reference
- x = 12;
- const Test two(20);
- // This will call the const version
- two.GetV();
- // So the below line will not compile
- // int& xx = two.GetV();
- // Where as this will compile
- const int& xx = two.GetV();
- // And then the below line will not compile
- // xx = 3;
- }
- #include <stdio.h>
- class Test
- {
- public:
- Test(int v){m_v = v;}
- ~Test(){printf("Destruct value = %dn",m_v);}
- int& GetV(){printf ("None Const returning %dn",m_v); return m_v; }
- const int& GetV() const { printf("Const returning %dn",m_v); return m_v;}
- private:
- int m_v;
- };
- void main()
- {
- // A none const object (or reference) calls the none const functions
- // in preference to the const
- Test one(10);
- int& x = one.GetV();
- // We can change the member variable via the reference
- x = 12;
- const Test two(20);
- // This will call the const version
- two.GetV();
- // So the below line will not compile
- // int& xx = two.GetV();
- // Where as this will compile
- const int& xx = two.GetV();
- // And then the below line will not compile
- // xx = 3;
- }
- #include <stdio.h>
- class Test
- {
- public:
- Test(int v){m_v = v;}
- ~Test(){printf("Destruct value = %dn",m_v);}
- int& GetV(){printf ("None Const returning %dn",m_v); return m_v; }
- const int& GetV() const { printf("Const returning %dn",m_v); return m_v;}
- private:
- int m_v;
- };
- void main()
- {
- // A none const object (or reference) calls the none const functions
- // in preference to the const
- Test one(10);
- int& x = one.GetV();
- // We can change the member variable via the reference
- x = 12;
- const Test two(20);
- // This will call the const version
- two.GetV();
- // So the below line will not compile
- // int& xx = two.GetV();
- // Where as this will compile
- const int& xx = two.GetV();
- // And then the below line will not compile
- // xx = 3;
- }
- class WidgetHolder {
- ...
- Widget& GetA();
- const Widget& GetA() const;
- ...
- };
- class Test {
- ...
- WigetHolder& AccessWidgets() { return m_Widgets;}
- const WidgetHolder&AcessWidgets() const { return m_Widgets;}
- private:
- WidgetHolder m_Widgets;
- ...
- };
Add Comment
Please, Sign In to add comment