Thread: Why pointers? (The answer)

  1. #16
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Thantos:

    I thought keyword const was what kept a function from changing values (aka data).
    Code:
    int x = 4;
    change(x);
     
    Version 1:
    void change(int & z)
    {
       z = 5;  //x now equals 5 as well eventhough reference used rather than pointer
    }
     
    Version 2:
    void change(const int & z)
    {
      z = 5; //error, z is a const reference and value/data cannot change
    }
     
    Version 3:
    void change(int & z) const
    {
       z = 5; //change() is a const function and promises not to change parameters passed in.
    }
     
    Version 3
    void change(const int & z) const
    {
       z = 5;  //okay I'm paranoid and prone to overkill, either of the const should prevent data passed in from changing, both const aren't needed. This is an error twice over.
    }
    Or am I reading your post wrong?

  2. #17
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Exactly a const reference is ok but what the book seemed to imply was that something like:
    Code:
    void foo ( int &a )
    {
      a = 5;
    }
    is less desirable then say
    Code:
    void bar (int *a)
    {
      *a = 5;
    }
    Because of the function call itself:
    Code:
    int b=10;
    foo ( b ); //  No idea that b won't be 10 afterwards
    bar ( &b ); // Now I have an idea that b might be different afterwards
    And for those of you who want to argue ease of use inside the functions I present:
    Code:
    void foobar (int *paraA) 
    {
      int &a = *paraA;
      a = 5;
    }

  3. #18
    Teenage Mutant Ninja Nerd MMD_Lynx's Avatar
    Join Date
    Aug 2004
    Posts
    65

    Pointers win by my standards.

    After all this, I still like pointers more than references. pointers can pretty much do all that references can and more.

    sure you have to use * and & more...who cares?!
    Stupid people are useful. You can make them do all the mindless tasks you are too lazy to do yourself.

    Sphynx cats are just bald and wrinkly, like old people, and we don't reject them.

  4. #19
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by MMD_Lynx
    After all this, I still like pointers more than references. pointers can pretty much do all that references can and more.

    sure you have to use * and & more...who cares?!
    You must be on step 2 then.

    Seriously, though, there are plenty of things that references can do that pointers cannot, and vice versa. Both have their uses. Both have their benefits, and both have their disadvantages.

  5. #20
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I agree with jlou. Besides, the cout << and cin >> chains wouldn't work without references. Now where would we be without those??
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #21
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Thanks Thantos I read the excerpt from the C++ Programing Language a long time ago but was unclear as to the reason for using a pointer instead of a refrence for function parameters. It seems the clarity of using pointers when you intend to alter the variable passed should be used consistantly throughout the programmers code.
    If the value of the prameter passed to the function is not to be changed is there any reason to use a refrence to a constant instead of simply passing by value and using a simple variable.

  7. #22
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    If it's a complex structure, you don't want to have to make a copy each time you call the function; so you use a const reference.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #23
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    @elad, your explanation on the use of const in Version 3 is not accurate.
    The const in this context can only be used by member function of a class to make sure that its member data will not be modified.
    Thus, the following will not compile
    Code:
    void foo(int & z) const
    {
        // z is not modified yet compile error will
        // be issued
    }
    
    int main()
    {
        int n = 7;
        foo( n );
        return 0;
    }
    But the following is the right use of const and will compile
    Code:
    class MyClass
    {
    public:
        MyClass() 
        {
            memberData = 7;
        }
    
        void foo(int &nonMemberData) const 
        {
            // below is legal because th const we have
            // does not affect what being passed to foo()
            nonMemberData = 5;
    
            // if below is uncommented it will cause compile error 
            // memberData = 8;
        }
    
    private: 
        int memberData;
    };
    
    int main()
    {
        int n = 10;
        MyClass myClass;
        myClass.foo( n );
        return 0;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  9. #24
    Teenage Mutant Ninja Nerd MMD_Lynx's Avatar
    Join Date
    Aug 2004
    Posts
    65
    Quote Originally Posted by Hunter2
    I agree with jlou. Besides, the cout << and cin >> chains wouldn't work without references. Now where would we be without those??
    well, i do prefer printf() a little more over cout.
    Stupid people are useful. You can make them do all the mindless tasks you are too lazy to do yourself.

    Sphynx cats are just bald and wrinkly, like old people, and we don't reject them.

  10. #25
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    printf() is fine, I suppose. But then there are also those of us who DO use cout/cin. Where would WE be without references?

    P.S. I still like references better, just 'cause I'm lazy and find the * button incredibly hard to reach... and I run into problems with running out of names for things when I need to have a pointer, then a reference to the dereferenced pointer, for several variables....
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #26
    Teenage Mutant Ninja Nerd MMD_Lynx's Avatar
    Join Date
    Aug 2004
    Posts
    65
    shoulda posted this in the C programming board. then we wouldn't have this dispute (cuz C doesn't have references)
    Stupid people are useful. You can make them do all the mindless tasks you are too lazy to do yourself.

    Sphynx cats are just bald and wrinkly, like old people, and we don't reject them.

  12. #27
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Thantos
    Have you read his book yet Sang-drax? He explains why he uses pointers instead of references so much. Basically he has said that in his belief that passing a reference should be used only to minimize the amount of data passed to a function that won't change the reference's data. And that a pointer should be used to pass data to a function that might change the data. This is so that when you make the call to a function you'll know if the data being passed in has the possibility of being different after the call.
    Yes I've read his book (although I don't own it) and I think that point could be valid. I think mutable reference arguments have their place though. Here's an example:
    Code:
    template<typename T>
    void swap(T& a, T& b) //Here it would be cumbersome with pointers
    {
    a^=b^=a^=b; //Fun way to swap a and b :)
    }
     
    int a = 23;
    int b = 234;
    swap(a,b); //The intended result is clear.
    Pointers certainly have their place, but if it is possible to use STL containers instead of new/delete, they should be used, which Stroustrup stresses in his examples.

    auto_ptr<T> could be used instead, of course.


    Quote Originally Posted by MMD_Lynx
    shoulda posted this in the C programming board. then we wouldn't have this dispute (cuz C doesn't have references)
    Yes, C programmers that have moved to C++ really like overusing pointers. Step 3 doesn't exist in C.
    Last edited by Sang-drax; 09-02-2004 at 01:32 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  13. #28
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by Sang-drax
    Yes I've read his book (although I don't own it) and I think that point could be valid. I think mutable reference arguments have their place though. Here's an example:
    Code:
    template<typename T>
    void swap(T& a, T& b) //Here it would be cumbersome with pointers
    {
    a^=b^=a^=b; //Fun way to swap a and b :)
    }
     
    int a = 23;
    int b = 234;
    swap(a,b); //The intended result is clear.
    In fact, Stroustrup qualifies his advice by saying that non-const reference arguments should only be used when the name of the function makes it clear that they will be modified. In this case, the name "swap" makes it obvious, so at least in this case you all agree.

  14. #29
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    You might note that you can only use that swap function on numbers. It doesn't work with strings or character arrays.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  15. #30
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by pianorain
    You might note that you can only use that swap function on numbers. It doesn't work with strings or character arrays.
    Yes, it is certainly not the recommended way to swap anything, unless you're programming small chips with extremly little memory, in which case you'll probably want to use assembler anyway.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

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, 09:32 AM
  2. delete and delete[]
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2003, 04:40 AM
  3. Pointers on pointers to pointers please...
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 11:24 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  5. Pointers pointers pointers....
    By G'n'R in forum C Programming
    Replies: 11
    Last Post: 11-02-2001, 02:02 AM