Thread: Using Pointers

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    75

    Using Pointers

    hello


    could I use a pointer to manipulate a value private value of a class I dunno if it's possible just doing a bit of experimenting lately and got the idea or would I still have to use a public accessor?



    short example of what I mean in words:


    I have a class;
    I have a private value in a class of 100;
    I have a pointer to that private value;


    oh another question posp up here

    if it is possible would the pointer have to be made inside the class?, or could I make it anywhere?


    just curios thats all probably better ways to do this but I I always try my own way first




    cheers



    stealth







    cheers


    stealth
    If I knew what You know I wouldnt be here.............. Stealth

    Ecliptic Entertainment
    http://www.ecliptic-entertainment.com/

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    75
    actually I fogot to say that I wanted to take say 10 away from the private value using the pointer except I dont want to create the value 10 inside the class I want to create it in int main


    so that it's something like:




    int main
    {

    int value_to_be_taken_away = 10;


    int new_value = pointer_to_private_value - value_to_be_taken_away;


    }




    just wanted to know if that is possible


    cheers


    stealth
    If I knew what You know I wouldnt be here.............. Stealth

    Ecliptic Entertainment
    http://www.ecliptic-entertainment.com/

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    61
    "could I use a pointer to manipulate a value private value of a class I dunno if it's possible just doing a bit of experimenting lately and got the idea or would I still have to use a public accessor? "

    good question, try this:

    #include <iostream>

    class num
    {
    public:
    num() {x = 100;}
    int* access() {return &x;} // return pointer to x
    void out() {cout << x << endl;}

    private:
    int x;
    };

    int main()
    {
    num myNum;
    int* gotcha = myNum.access(); // snag pointer to x

    myNum.out(); // displays initial val of 100
    *gotcha = 999; // directly manipulate private var
    myNum.out(); // now displays 999

    return 0;
    }

    of course the class designer would have to be screwed up in the first place to have such a return value for a member function. as for your second message, i didn't quite understand (sorry); but hope this helps, none the less.
    Last edited by greenRoom; 10-18-2001 at 04:37 AM.

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You may be able to get away with something like this, depending on the complexity of your class and how your complier alligns the member variables, but it's not good practice and probably wouldn't be portable in all but the simplest of cases -

    Code:
    #include <iostream>
    
    using namespace std;
    
    class a
    {
    private:
    	int b;
    public:
    	a():b(50){}
    	void print(){cout << b;}
    };
    
    int main( ) 
    {
    	a b;
    	*(int*)&b -=10;
    	b.print();
    
    	return 0;
    }
    However, if you have a public pointer to the variable within your class (or function that returns a pointer), there's nothing to stop you changing the variable with the pointer (but I'm not sure why you'd need to, inlined accessors would probably be more efficient).
    zen

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    61
    zen, that was pretty cool.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    75
    ok thanks for the help guys like I said I know it's not the best way to do things I just like testing ideas I get


    thanks again


    stealth
    If I knew what You know I wouldnt be here.............. Stealth

    Ecliptic Entertainment
    http://www.ecliptic-entertainment.com/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM