pointy pointy

This is a discussion on pointy pointy within the C++ Programming forums, part of the General Programming Boards category; I'm trying to understand passing a pointer to a function so the function modifies the stored value. Example Code: rval ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    128

    pointy pointy

    I'm trying to understand passing a pointer to a function so the function modifies the stored value.

    Example
    Code:
    rval = myfunction (rval)
    	
    std::string& rmyfunction(std::string& strText)
    {
    //stuff goes on
    return strText;
    }
    BUT if I'm passing a pointer the fn, can't I write

    Code:
    myfunction (rval)
    	
    void myfunction(std::string& strText)
    {
    //stuff goes on
    }

  2. #2
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by FoodDude
    I'm trying to understand passing a pointer to a function so the function modifies the stored value.

    Example
    Code:
    rval = myfunction (rval)
    	
    std::string& rmyfunction(std::string& strText)
    {
    //stuff goes on
    return strText;
    }
    BUT if I'm passing a pointer the fn, can't I write

    Code:
    myfunction (rval)
    	
    void myfunction(std::string& strText)
    {
    //stuff goes on
    }
    Yes, because its a reference to the object you are actually modifying that object.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    cool...thought I was loosing my mind.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    BTW: that isn't a "pointer" -- its a reference.
    Code:
    std::string& rmyfunction(std::string& strText) <<< referencde
    {
    }
    
    
    std::string* rmyfunction(std::string* strText) <<< pointer
    {
    }

Popular pages Recent additions subscribe to a feed

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