Like Tree1Likes
  • 1 Post By laserlight

How to make my class globally accessible

This is a discussion on How to make my class globally accessible within the C++ Programming forums, part of the General Programming Boards category; ok i don't know how to deal with this one. I have a class like this: file one : Code: ...

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

    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    176
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,699
    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
    21,717
    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.
    whiteflags likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    176
    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
    21,717
    If you're stuck with bad design, then go for the global variable. You're just making things worse, but you're doomed anyway
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you can make class singleton
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 10: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, 04:05 PM

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