CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Pointer Object

  1. #1
    Join Date
    Oct 2001
    Posts
    745

    Pointer Object

    I Posted this post earlier,but I guess I didnt express really what I wanted.(So I was getting answers which was not helpful for me).

    For Eg;
    Iam Having a
    Class A
    {
    inline CycleBuffer& GetCycleBuffer(){return *m_pCycleBuffer;}
    Public:
    DesignDocumentData* m_pDesignDocumentData;
    LdMapDocumentData* m_pLdMapDocumentData;

    MapDocumentData* m_pMapDocumentData;

    NgDocData m_ngDocData;
    I_NgDocDataRead* m_pNgDocData;
    CyclicBuffer *m_pCyclicBuffer;


    };

    IN a class called say ,"C"
    I have created the class
    A *ptr = new A;
    and I have assigned each variable with some value.

    Now Iam in class B & I want to access some values which are there in class A;
    So in
    Class B
    {
    B(){ *ptr = NULL;}
    GetDATA(/* Want to acces data using the A *ptr; */);

    Private:

    A *ptr;
    };

    So if I create a different object with "new" operator,I guess I wont be able to access the datas stored earlier & I dont want to make the members static in class A.
    I want to have the same object instance which was created in class C.

    How can I do it?could anyone show it with a code snippet.

    Thanks in advance

  2. #2
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880
    Simple in the case you know your instance of C where you create your instance of B. Then you just have to do something like:

    B.ptr = C.ptr;

    If you don't have a way to know C where B is created, then you can create a static variable in the class A that contains the list of all instances of A:

    Code:
    class A
    {
        ...
        public:
        static vector<A*> arrA;
        ...
    };
    Then in the construtor of A, you add this to the vector. In the destructor, you remove it.

    Then, with your class B, you can look for the instance of A you need and use it:
    Code:
    B->ptr = A::arrA[0];
    This is the kind of thing you can use. Even if you don't want to nake all members of A static, you can just create some static members for specific purposes.
    Elrond
    A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
    -- George Steiner

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured