Thread: How to make my class globally accessible

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    How to make my class globally accessible

    ok

    i don't know how to deal with this one. I have a class like this:


    file one :
    Code:
    class X : public Y {
    
    public:
      virtual int query(int, int);
      // constructor
      X(int, int);
      
      ~X();
      
    private;
    
      int *M
      
      int *N
    
    }
    file two:
    Code:
    class Y {
    public:
    	// returns index of RMQ[i,j]
    	virtual int query(int, int) = 0;
    };
    And i construct my M and Nby calling :
    Code:
    X Y(a,b);
    and afterwords by calling
    Code:
    result = Y (c,d)
    i get my result. The problem is I need to be able to call result = Y (c,d) from outside my main function and get results but i don't know how to do this.


    So I want to be able to do something like this.


    Code:
    #include <iostream>
    int g;
    
    void func2(int *h){
    *h = g;
    }
    
    void func(int *g){
    *g = 1;
    }
    
    void func1(int f){
    g = f;
    }
    
    int main ()
    {
    func(&g);
    int p = 7;
    func1(p);
    int h ;
    func2(&h);
    std::cout << h << std::endl;
    return 0;
    }
    however i have no clue how to do this with classes.

    thank you

    baxy

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Why do you need globals? pass all as parameters

    Code:
    #include <iostream>
    
    
    void func(int &g){
        g = 1;
    }
    
    void func1(int f, int& g){
        g = f;
    }
    void func2(int &h, int g){
        h = g;
    }
    
    int main ()
    {
        int g;
        func(g);
        int p = 7;
        func1(p,g);
        int h ;
        func2(h,g);
        std::cout << h << std::endl;
        return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    well this is what i am trying to avoid since passing variables would require to modify a large section of code involving ca. 50 functions (func prototypes, recursive calls , callbacks ...) and since these are not simple to read or understand i don't want to do that ...

    i just wanna be able to get my data at a certain point in the code..

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm not clear on what the problem is. It sounds like you want to use X* type parameters. A class pointer works just like a regular pointer, too. So you can do normal construction and pass it in like g(&xObj);

    If you have to construct a global object, the parameters need to be known at compile time. Global objects are completely constructed before main executes.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by baxy
    well this is what i am trying to avoid since passing variables would require to modify a large section of code involving ca. 50 functions (func prototypes, recursive calls , callbacks ...) and since these are not simple to read or understand i don't want to do that ...
    Did you write those 50 functions? The way I see it, if passing variables would require such a drastic change, then I suspect that the reason why these functions are not simple to read or understand is because they make excessive use of global variables to begin with, and thus it is difficult to reason about that part of the program. If so, I suggest that you bite the bullet and refactor that part of the program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    No i did not write those and i have no time to do it all over again that would take me a couple of months. So i need this bypass somehow. There has to be the way... (though i am googling like crazy and not finding anything similar to what i need )

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you're stuck with bad design, then go for the global variable. You're just making things worse, but you're doomed anyway
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you can make class singleton
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 07-20-2012, 01:56 PM
  2. Using a linked list globally and File I/O
    By Leftos in forum C Programming
    Replies: 46
    Last Post: 01-07-2008, 11:21 AM
  3. Why can't I use const globally?
    By keira in forum C Programming
    Replies: 8
    Last Post: 09-15-2007, 04:30 AM
  4. const elements of struct / class not accessible?
    By talz13 in forum C# Programming
    Replies: 2
    Last Post: 03-24-2006, 05:05 PM