Thread: Global scope and passing pointer

  1. #1
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40

    Global scope and passing pointer

    I'm trying to debug some code and have narrowed it down to my non-understanding of passing pointers. I've reviewed the forums but still haven't figured it out.

    something like this:
    Code:
    objectContainer::objectContainer(devhandle* dev){}
    objectContainer::dosomething1(){
    VME_write(dev, data);
    }
    
    devhandle *dev;
    getVMEhandle(dev);                    // get the device handle using library call
    objectContainer Device1(dev);     // make an object that will contain the handle
    ...
    Device1.dosomething1;
    Device1.dosomething2;
    ...
    But what's happening is that when I step into the objectContainer class functions using gdb and query the contents of "dev" they are changed. And of course when I step back into the global scope they haven't.

    The result is that none of the containerized hardware calls will not work until I can pass the pointer correctly through the several layers of scope.

    Looking for insight,
    Dave++

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    post your code for objectContainer.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40
    Code:
    class objectContainter
       {
       private:
        vme_dev_handle *dev;     // Device Handle                                                                       
        long *Data;
        long *Data1;
    ...
       public:
        int icount;
        objectContainer(vme_dev_handle*);
    ...
    };
    
    objectContainer::objectContainer(vme_dev_handle* dev){
    int count=1;
    ...
    }
    The only thing that the constructor needs to do is grab the handle for use by all its methods.
    My question is, "how to pass the global scope pointer through multiple layers of calls?"

    Thanks

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I don't see your constructor actually storing the pointer in the object.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40

    getting closer

    Quote Originally Posted by CornedBee View Post
    I don't see your constructor actually storing the pointer in the object.
    Wouldn't this happen automatically with the call?

    Code:
    private:
    int a;
    int b;
    ...
    
    object::object(int a){
    a = a;  // unnecessary?
    b = a;  // extra effort?
    }
    As I continue to experiment I'm seeing that with each level of scope I could add another copy of the pointer (bad) or do my best to hold on to the original (goal).

    Dave

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your Andromeda galaxy avatar is nice.

    No, class variables are not automatically assigned values by the constructor. You have to do it yourself. Keep in mind that if a parameter has the same name as a class variable, you have to use this->variable to access the class variable.
    Code:
    class c {
    private:
        int x;
        c(int x);
    };
    
    c::c(int x) {
        this->x = x;
    }
    Also, you might want to use initializers rather than code inside the constructor:
    Code:
    class c {
    private:
        int x;
        c(int x) : x(x);
    };
    
    c::c(int x) {
    
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    class object
    {
    public:
        object(int);
    private:
        int a;
        int b;
    };
    
    object::object(int a)
    {
         a = a;  // assigns local a to local a. the one above this line. wastes time.
         b = a;  // assigns local a to object::b, otherwise known as this->b
    }
    Last edited by robwhit; 06-11-2007 at 03:55 PM. Reason: silly error

  8. #8
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40

    Thumbs up one new leap for ...

    dwks,
    Thanks for the inputs. I was reviewing another thread while waiting for help...
    http://cboard.cprogramming.com/showthread.php?t=90680

    And began to see that their pointer wrapper is what I was attempting. I'll do some more testing and probably get back with results or more questions tomorrow (or later this evening).

    Also, you obviously know some astronomy to know M31 (http://en.wikipedia.org/wiki/Andromeda_Galaxy)

    dave.thanks(10^6);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Incompatible Pointer Type warnings
    By trillianjedi in forum C Programming
    Replies: 3
    Last Post: 06-11-2008, 04:16 PM
  2. passing pointer to a structure in a function
    By steve1_rm in forum C Programming
    Replies: 5
    Last Post: 02-03-2008, 02:48 AM
  3. Problem with "pointer from integer without a cast"
    By firyace in forum C Programming
    Replies: 6
    Last Post: 05-11-2007, 09:48 AM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  5. Passing Pointer to a function
    By Nishant Ghai in forum C Programming
    Replies: 4
    Last Post: 02-27-2003, 06:40 AM