sharing a static varable across classes

This is a discussion on sharing a static varable across classes within the C++ Programming forums, part of the General Programming Boards category; Hello. I have simple class called Board.hpp and Board.cpp. I have also a DNBoard.hpp and DNBoard.cpp. I have a static ...

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    sharing a static varable across classes

    Hello.

    I have simple class called Board.hpp and Board.cpp.

    I have also a DNBoard.hpp and DNBoard.cpp. I have a static variable in Board.hpp and I set that variable in Board.cpp. I need a static as the variable should have class scope.

    However, when I try and access that variable from DNBoard.cpp I get a negative value and not the value that I set in the Board.cpp.

    Board.hpp
    Code:
    class Board
    {
    public:
    static int bHandle(){return _bHandle;}
    
    private:
    static int _bHandle;
    };
    Board.cpp
    Code:
    int Board::_bHandle = 0; //initialize
    
    void OpenLibrary()
    {
         Board::_bHandle = 101023; //Value I want to set and access in DNBoard.cpp
    DNBoard.hpp
    Code:
    #include "Board.hpp"
    
    class DNBoard : public Board
    {
      // some stuff here
    };
    DNBoard.cpp
    Code:
    void Service()
    {
        int handle = Board::bHandle(); //Give an incorrect value here
    }
    Last edited by steve1_rm; 01-30-2009 at 11:52 PM. Reason: added enclosing brace

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Assuming you've friended OpenLibrary or something that actually allows that access to a private variable to happen, then Service sees 0 before OpenLibrary is called and 101023 after. Since the example you posted works, there's not much more we can say. If you want to post an example that doesn't work, then I guess we can look at that.

  3. #3
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    90
    hello dude...
    _bHandle is private...
    if you want to access it in its derived class, use protected...

    I guess your code like this:
    Code:
    #include <stdio.h>;
    
    class Board
    {
    public:
        static int bHandle(){return _bHandle;}
        void OpenLibrary();
    
    protected:
        static int _bHandle;
    };
    
    class DNBoard : public Board
    {
    public:
        void Service();
    };
    
    
    int Board::_bHandle = 0; //initialize
    
    void Board::OpenLibrary()
    {
         Board::_bHandle = 101023; //Value I want to set and access in DNBoard.cpp
    }
    
    void DNBoard::Service()
    {
        int handle = Board::bHandle(); //Give an incorrect value here
        printf("%i", Board::_bHandle);
    }
    
    int main()
    {
        Board board;
        board.OpenLibrary();
    
        DNBoard dnboard;
        dnboard.Service();
    
    }
    Am I right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static object never inits
    By krappa in forum C++ Programming
    Replies: 10
    Last Post: 10-30-2008, 12:04 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. Question about Static variables and functions.
    By RealityFusion in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2005, 02:31 PM
  5. Sharing Classes using .DLL files?
    By Laos in forum C++ Programming
    Replies: 2
    Last Post: 08-30-2001, 04:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21