Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Class A initialisation within class B containing pointer to class A
- #include <iostream>
- #include <vector>
- class A {
- public:
- A(const int pInt) {mInt = pInt;}
- void print() {std::cout << mInt << std::endl;}
- private:
- int mInt; //some data
- };
- class B {
- public:
- B() {mP1 = new A(1);} //initialise to 1
- ~B() {delete mP1;}
- A& access() {return *mP1;} //return reference to the data
- private:
- A* mP1; //pointer to some data
- };
- int main() {
- B vB;
- vB.access().print(); //this works.
- B *vBptr;
- vBptr->access().print(); //Segmentation fault!
- std::vector<B*> vVec;
- vVec.resize(1);
- vVec[0]->access().print(); //Segmentation fault!
- }
- B *vBptr;
- B *vBptr = new B();
- vVec.resize(1);
- vVec.resize(1,new B());
- B *vBptr = new B;
- vBptr->access().print();
- for ( int i = 0 ; i < vVec.size() ; i++ )
- vVec[i] = new B;
- delete vBptr;
- for ( int i = 0 ; i < vVec.size() ; i++ )
- delete vVec[i];
- B *vBptr; // bad -- uninitialized
- B *vBptr = new B; // proper
- B vB;
- vB.access().print(); //this works.
- B *vBptr = new B;//allocate memory for vBptr
- vBptr->access().print();
- delete vBptr;//clean up of vBptr
- std::vector<B*> vVec;
- vVec.push_back(new B);
- vVec[0]->access().print();
- delete vVec[0];
- class B {
- public:
- B():mP1(new A(1)) {} //initialise to 1
- ~B() {} //no extra managing necessary
- A& access() {return *mP1;} //return reference to the data
- private:
- std::shared_ptr<A> mP1; //pointer to some data
- };
- int main() {
- B vB;
- vB.access().print(); //this works.
- std::shared_ptr<B> vBptr(new B);
- vBptr->access().print();
- std::vector<std::shared_ptr<B> > vVec;
- vVec.push_back(std::shared_ptr<B>(new B));
- vVec[0]->access().print();
- }
- B *vBptr;
- vBptr->access().print(); //Segmentation fault!
- std::vector<B*> vVec;
- vVec.resize(1);
- vVec[0]->access().print(); //Segmentation fault!
- B vB;
- vB.access().print();
- B *vBptr = &vB; // or B *vBptr = new B;
- vBptr->access().print();
- std::vector<B*> vVec;
- vVec.push_back(&vB); // vVec.push_back(new B);
- vVec[0]->access().print();
- class B {
- public:
- // B() {}
- // ~B() {}
- A& access() {return a;}
- private:
- A a;};
- #include <iostream>
- #include <vector>
- #include "boost/smart_ptr.hpp"
- class A {
- public:
- A(const int pInt) {mInt = pInt;}
- void print() const {std::cout << mInt << std::endl;}
- void set(const int pInt) {mInt = pInt;}
- private:
- int mInt; //some data
- };
- class B {
- public:
- B() {} //leave A pointer as null
- ~B() { } //delete handled by shared_ptr
- A& access() {return *mP1;} //return reference to the data
- boost::shared_ptr<A>& access_A_ptr() {return mP1;} //return the pointer for assignment
- private:
- boost::shared_ptr<A> mP1;
- };
- int main() {
- std::vector< boost::shared_ptr<A> > vVecA; //data to be shared
- for (unsigned int i = 0; i < 5; i++) {
- boost::shared_ptr<A> vAptr(new A(i));
- vVecA.push_back(vAptr);
- vVecA[i]->print();
- }
- vVecA[2]->set(123);
- for (unsigned int i = 0; i < vVecA.size(); i++) {
- vVecA[i]->print(); //changes to the underlying objects are reflected
- }
- boost::shared_ptr<B> vBptr(new B); //make an empty B
- vBptr->access_A_ptr() = vVecA[2]; //assignment of shared pointer
- vBptr->access().print();
- std::cout << "use count of vVecA[1] = " << vVecA[1].use_count() << std::endl; // = 1
- std::cout << "use count of vVecA[2] = " << vVecA[2].use_count() << std::endl; // = 2
- std::vector< boost::shared_ptr<B> > vVecB;
- //vVecB.resize(vVecA.size(), boost::shared_ptr<B> (new B) ); //resize and init to a SINGLE B object, NO!!!
- for (unsigned int i = 0; i < vVecA.size(); i++) {
- vVecB.push_back(boost::shared_ptr<B> (new B)); //filling array with separate empty Bs
- vVecB[i]->access_A_ptr() = vVecA[i];
- vVecB[i]->access().print(); // = 0,1,123,3,4
- }
- vVecA[2]->set(2); //changes to A objects reflected in the B objects
- for (unsigned int i = 0; i < vVecB.size(); i++) {
- vVecB[i]->access().print(); // = 0,1,2,3,4
- }
- std::cout << "use count of vVecA[1] = " << vVecA[1].use_count() << std::endl; // = 2
- std::cout << "use count of vVecA[2] = " << vVecA[2].use_count() << std::endl; // = 3
- }
Add Comment
Please, Sign In to add comment