Thread: -> operator ?

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Austin TX yall
    Posts
    42
    Rep Power
    12

    -> operator ?


    Code:
    class linknode{
    public:
    	int number;
    	linknode * prev;
    	linknode * next;
    	linknode(){};
    	linknode(int, linknode*, linknode*){};
    	~linknode(){};
    };
    
    void insert_front(int number){ //receives a number(int)
    		temp= new linknode(number, 0,0);  //instantiates a new linknode with the number 
    		temp->next = head;
    		cout<< number;			//outputs the right number
    		cout << temp->number;   //outputs the wrong number(junk)
    		
    		temp->prev = 0;
    		head = temp;
    	};
    This is a snippet from a program i'm working on. The problem is in the comments on the cout <<temp->number; line. Could anyone clear this up for me. the "->" operator is the same as the dereferenced temp and the number value declared in the linknode class. Am i mistaken? Why isthe output of
    Code:
     cout<<number;
    and
    Code:
     cout<<temp->number;
    different?
    Thanks,

    Jonathan Cobb
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    I don't see any code where you set temp->number = number.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1511
    I think it's supposed to happen in the linknode constructor. Care to post that for us?
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Austin TX yall
    Posts
    42
    Rep Power
    12
    Code:
    temp= new linknode(number, 0,0);  //instantiates a new linknode with the number
    The line above assigns the temp pointer a new linknode with the number variable passed to the linknode constructor(below).
    Code:
    linknode(int, linknode*, linknode*){};
    Thanks,

    Jonathan Cobb
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,210
    Rep Power
    2222
    Originally posted by jonnyfive
    Code:
    temp= new linknode(number, 0,0);  //instantiates a new linknode with the number
    The line above assigns the temp pointer a new linknode with the number variable passed to the linknode constructor(below).
    Code:
    linknode(int, linknode*, linknode*){};
    Well, there you have it. The constructor does nothing (note the empty braces testifying that it ain't got no body).

    Within the body of the constructor, it must take those arguments passed in to it and assign them to the appropriate properties.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Austin TX yall
    Posts
    42
    Rep Power
    12
    LOL! I need a good slap upside the head.
    Thanks.
    Thanks,

    Jonathan Cobb

IMN logo majestic logo threadwatch logo seochat tools logo