Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-----------------------
- // foo.h
- //-----------------------
- class foo
- {
- foo();
- ~foo();
- };
- //-----------------------
- // bar.h
- //-----------------------
- class foo; // forward declaration
- class bar
- {
- bar();
- ~bar();
- foo* foo_pointer;
- };
- //-----------------------
- // foo.h
- //-----------------------
- class foo
- {
- foo();
- ~foo();
- };
- //-----------------------
- // bar.h
- //-----------------------
- class foo; // Not enough given the way we declare "foo_object"..
- #include "foo.h" // ..instead this is required
- class bar
- {
- bar();
- ~bar();
- foo foo_object;
- };
- //-----------------------
- // bar.h
- //-----------------------
- #include <memory>
- class foo; // Not enough given the way we declare "foo_object"..
- class bar
- {
- public:
- bar();
- ~bar();
- foo &foo_object() { return *foo_ptr; }
- const foo &foo_object() const { return *foo_ptr; }
- private:
- auto_ptr<foo> foo_ptr;
- };
- #include <iostream>
- using namespace std;
- class Foo;
- template <typename T>
- class DefaultConstuctorPtr
- {
- T *ptr;
- void operator =(const DefaultConstuctorPtr &);
- DefaultConstuctorPtr(const DefaultConstuctorPtr &);
- public:
- DefaultConstuctorPtr() : ptr(new T()) {}
- ~DefaultConstuctorPtr() { delete ptr; }
- T *operator *() { return ptr; }
- const T *operator *() const { return ptr; }
- };
- class Bar
- {
- DefaultConstuctorPtr<Foo> foo_ptr;
- public:
- Bar() {} // The compiler should really need Foo() to be defined here?
- };
- class Foo
- {
- public:
- Foo () { cout << "Constructing foo"; }
- };
- int main()
- {
- Bar bar;
- }
- template<typename Type>
- struct member {
- boost::shared_ptr<Type> ptr;
- member(): ptr(new Type) { }
- };
- struct foo;
- struct bar {
- bar();
- ~bar();
- // automatic management for m
- member<foo> m;
- };
- // bar.h
- class foo;
- class bar {
- foo& foo_;
- public:
- bar();
- ~bar();
- };
- // bar.cc
- bar::bar() : foo_(*new foo)
- {
- // ...
- }
- bar::~bar()
- {
- // ...
- delete &foo_;
- }
- template<class T>
- class myAuto
- {
- private:
- T * obj;
- public:
- myAuto() : obj(new T) { }
- ~myAuto() { delete obj; }
- T& object() { return *obj; }
- T* operator ->() { return obj; }
- };
- // foo.h:
- class foo
- {
- public:
- foo();
- ~foo();
- void some_foo_func();
- };
- //bar.h:
- class foo;
- class bar
- {
- public:
- bar();
- ~bar();
- myAuto<foo> foo_object;
- };
- //main.cc:
- #include "foo.h"
- #include "bar.h"
- int main()
- {
- bar a_bar;
- a_bar.foo_object->some_foo_func();
- return 0;
- }
- //-----------------------
- // foo.h
- //-----------------------
- class foo
- {
- foo();
- ~foo();
- };
- //-----------------------
- // bar.h
- //-----------------------
- class foo;
- class bar
- {
- private:
- struct impl;
- boost::shared_ptr<impl> impl_;
- public:
- bar();
- const foo& get_foo() const;
- };
- //-----------------------
- // bar.cpp
- //-----------------------
- #include "bar.h"
- #include "foo.h"
- struct bar::impl
- {
- foo foo_object;
- ...
- }
- bar::bar() :
- impl_(new impl)
- {
- }
- const foo& bar::get_foo() const
- {
- return impl_->foo_object;
- }
Add Comment
Please, Sign In to add comment