question about pointers inside class

This is a discussion on question about pointers inside class within the C++ Programming forums, part of the General Programming Boards category; suppose that we have this simple class Code: class foo{ private: struct node { int x,y; struct node *other; //pointer ...

  1. #1
    nik
    nik is offline
    Registered User
    Join Date
    Nov 2010
    Posts
    44

    question about pointers inside class

    suppose that we have this simple class

    Code:
    class foo{
          
          
          private:
          
          struct node {
                 
                 int x,y;
                 struct node *other; //pointer to another struct node
                 
                 };
                 
                 void change(struct node*);
                 struct node *root; //first node
          
          public:
                 
                 foo();
                 void change(); //changes values of some node
                 
                 void add(int value); //add new value for both x and y, x=y=value
                 
                 
                 void show();   //show values
                 
                 
                 };
    and here is the cpp file

    Code:
    #include "foo.h"
    #include <iostream>
    
    using namespace std;
    
    foo::foo(){
               
               root = NULL;
               
               }
               
    void foo::add(int z){
         
         struct node *newnode = new node;
         
         newnode->x = z;
         newnode->y = z;
         root = newnode;
         
         struct node *othernode = new node; //create a new node and link it to root
         
         othernode->x = 9;
         othernode->y = 9;
         
         root->other = othernode;
       
         
         }
         
    
    void foo::show(){
         
         cout<<root->other->x<<endl; 
         
         }
         
    
    void foo::change(struct node *ptr){ //ptr = node to change
    
         ptr->other = NULL;
         
         
         
         }
    
    void foo::change(){
    
         struct node *n = root;
         
         change(n);
         
         }
    so basically we have a root which has a pointer to other node, and the other node has initial values of 9, then if I call the change() method for n = root and try to change the root->other to be NULL, it will change it.

    but I don't get why this is happening, I pass the pointer by value not by reference to the change method
    Code:
    change(n)
    so why exactly does it change the pointer value of root->other to be NULL?

    would this thing work if I didn't have a class but did this inside a main.cpp file?

    for example here

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    void change(int *y){
         
         y = NULL;
         
         }
         
    int main(void){
        
        int z=10,*y=&z;
        
        change(y);
        
        cout<<y;
        cin.get();
        
        
        return 0;
    }
    the y remains the same..

    thank you in advance

    EDIT: I edited the whole thing... if you can please explain me, I would appreciate it
    Last edited by nik; 12-10-2010 at 02:46 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,207
    A pointer is passed by value, for both your foo::change(struct node *) and change(int *) function. Any changes made to the value of the pointer are therefore local to the functions, and not visible to the caller.

    However, if you make changes to whatever the pointer points at, those changes are visible to the caller.

    So;
    Code:
    void change_struct(struct node *ptr)
    {
         ptr->other = NULL;    /*  this change is visible to the caller */
         ptr = NULL;                /*   This change is not visible to caller */
    }
    
    void change_int(int *y)
    {
         *y = 42;    /*  this change is visible to the caller */
         y = NULL;     /*   This change is not visible to caller */
    }
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    nik
    nik is offline
    Registered User
    Join Date
    Nov 2010
    Posts
    44
    Quote Originally Posted by grumpy View Post
    A pointer is passed by value, for both your foo::change(struct node *) and change(int *) function. Any changes made to the value of the pointer are therefore local to the functions, and not visible to the caller.

    However, if you make changes to whatever the pointer points at, those changes are visible to the caller.

    So;
    Code:
    void change_struct(struct node *ptr)
    {
         ptr->other = NULL;    /*  this change is visible to the caller */
         ptr = NULL;                /*   This change is not visible to caller */
    }
    
    void change_int(int *y)
    {
         *y = 42;    /*  this change is visible to the caller */
         y = NULL;     /*   This change is not visible to caller */
    }
    thanks a lot I understand it now

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Btw, your program is a ticking time bomb. foo::node must either implement a copy constructor and assignment operator or disallow these. Otherwise you may run into unexpected bugs.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  2. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  3. Pointer to class itself
    By Cdumbie in forum C++ Programming
    Replies: 3
    Last Post: 10-31-2002, 11:24 AM
  4. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM
  5. Replies: 4
    Last Post: 09-12-2001, 02:05 PM

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