Thread: Has anyone ever successfully used a "static pointer?"

  1. #1
    Registered User adonisv's Avatar
    Join Date
    Feb 2003
    Posts
    14

    Lightbulb Has anyone ever successfully used a "static pointer?"

    Ive declared it...
    /////////////////////////////////////////////////////////////////////////////
    // RSacmanSetupTree window


    class RSacmanSetupTree : public CTreeCtrl
    {
    // Construction
    public:
    RSacmanSetupTree();
    public:
    static RSacmanSetupTree* m_spSacmanSetupTree;

    // Attributes


    I've created a function that returns it....
    // This function returns a reference to a canal manager object.
    RSacmanSetupTree& RSacmanSetupTree::GetSetupTreePtr()
    {
    return *m_spSacmanSetupTree;

    //~maa~20011210
    }


    I've initialized it in the cpp file right above the constructor

    RSacmanSetupTree * RSacmanSetupTree::m_spSacmanSetupTree= NULL;

    But I can't use it! Shouldn't a pointer take and address or do I need to overload an operator??

    ////////////////////////////////////////////////////////////////////////////
    // RAddSystemDlg message handlers

    BOOL RAddSystemDlg::OnInitDialog()
    {
    CDialog::OnInitDialog();

    // TODO: Add extra initialization here
    this->m_spSacmanSetupTree = RSacmanSetupTree::GetSetupTreePtr();

    return TRUE; // return TRUE unless you set the focus to a control
    // EXCEPTION: OCX Property Pages should return FALSE
    }




    --------------------Configuration: SacmanSetup - Win32 Debug--------------------
    Compiling...
    RAddSystemDlg.cpp
    D:\Data\VSS\CodeResearch\SacmanSetup\RAddSystemDlg .cpp51) : (error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class RSacmanSetupTree' (or there is no acceptable conversion)
    RSacmanSetupTree.cpp

    Generating Code...
    Error executing cl.exe.

    SacmanSetup.exe - 1 error(s), 0 warning(s
    Last edited by adonisv; 02-21-2003 at 07:08 PM.
    Logic, Reason and Creativity...

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    What are you trying to do? You are assigning to the pointer the dereferenced pointer (actually a reference, but whatever)...

    IE:

    static YourType* m_pYourType;

    // later
    m_pYourType = *m_pYourType; // Huh?
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User adonisv's Avatar
    Join Date
    Feb 2003
    Posts
    14
    Isn't my return function handling that???

    / This function returns a reference to a canal manager object.
    RSacmanSetupTree& RSacmanSetupTree::GetSetupTreePtr()
    {
    return *m_spSacmanSetupTree;

    //~maa~20011210
    }
    Logic, Reason and Creativity...

  4. #4
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    RSacmanSetupTree& RSacmanSetupTree::GetSetupTreePtr()
    {
    return *m_spSacmanSetupTree;
    }

    In this function you are returning a reference to an object, not a pointer to an object.


    this->m_spSacmanSetupTree = RSacmanSetupTree::GetSetupTreePtr();

    Here you try to assign a reference to a pointer type (m_spSacManSetupTree is a pointer).

    Besides this, why are you assigning a pointer to itself through a function call?
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  5. #5
    Registered User adonisv's Avatar
    Join Date
    Feb 2003
    Posts
    14
    My pointer is in another class RAddSystemDlg. I have a dialog with a tree control in it and I want to have other dialogs that allow the user to add children to the tree control through those dialogs. So I only want one instance of the tree control class. I figured I could have a static pointer to the tree control that could be accessed by each of the dialogs...

    /////////////////////////////////////////////////////////////////////////////
    // RAddSystemDlg message handlers

    BOOL RAddSystemDlg::OnInitDialog()
    {
    CDialog::OnInitDialog();

    // TODO: Add extra initialization here
    this->m_pSacmanSetupTree = NULL;
    this->m_pSacmanSetupTree = RSacmanSetupTree::GetSetupTreePtr();

    return TRUE; // return TRUE unless you set the focus to a control
    // EXCEPTION: OCX Property Pages should return FALSE
    }

    Isn't a pointer supposed to take a reference? I mean it POINTS to an object. So I want to return the address of that object so my pointer of the type of that object, in a different class, can point to it...right?
    Logic, Reason and Creativity...

  6. #6
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    code tags ppl!

  7. #7
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    A reference and a pointer are not the same. This is probably what you want, well sort of:

    Code:
    class SomeClass {
    
        public:
        // Get a reference to the instance of SomeClass
        staic SomeClass& Instance() 
        {
            // allocate it if it hasnt been allocated already
            if ( !m_pSomeClass ) {
                m_pSomeClass = new SomeClass();
            }
            return *m_pSomeClass;
        }
    
    };
    
    // The pointer to your only instance
    static SomeClass* m_pSomeClassInstance = NULL;
    
    class SomeOtherClass {
    
        void SomeMemberFunction()
        {
           //   This is wrong: can't assign reference to pointer
          //    SomeClass* pClass = SomeClass::Instance();
    
    
             // Assign reference to reference instead
             SomeClass& refSomeClass = SomeClass::Instance();
        }
    };
    What you are asking for is a singleton class, though. This design (above) is not really a singleton, because a singleton is supposed to enforce the idea that multiple objects cannot be constructed, even by accident. So the constructor must be private, and the copy constructor, and the assignment operator, etc. And there are some multithreading issues, and the list goes on and on. I avoided all that just to show you the reference problem. But if you are interested in a correct implementation of a singleton, you should be able to find one without much trouble on google. Search for design pattern and C++ and singleton.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  8. #8
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    See now isnt that so much neater! Thanks, happy coding.

  9. #9
    Registered User adonisv's Avatar
    Join Date
    Feb 2003
    Posts
    14

    Talking Doh!

    !
    Logic, Reason and Creativity...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocation from "static array"
    By Petike in forum C Programming
    Replies: 6
    Last Post: 10-12-2008, 03:17 AM
  2. Ask about error when declare "static inline int"
    By ooosawaddee3 in forum C Programming
    Replies: 3
    Last Post: 05-25-2002, 05:03 AM
  3. Test pointer if successfully freed.
    By UniMord in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 10:09 AM
  4. inputting line of text vs. integers in STACK
    By sballew in forum C Programming
    Replies: 17
    Last Post: 11-27-2001, 11:23 PM