how are functions that use = done?

This is a discussion on how are functions that use = done? within the C++ Programming forums, part of the General Programming Boards category; Hello, I am learning how to use the reSIProcate SIP stack and had a question about something. Code: Auth auth; ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    14

    how are functions that use = done?

    Hello,

    I am learning how to use the reSIProcate SIP stack and had a question about something.

    Code:
    Auth auth;
    auth.scheme() = “Digest”;
    auth.param(p_nonce) = “blah”;  // QuotedDataParam
    auth.param(p_algorithm) = “MD5”;
    How would one make a function that uses the '=' operator like in this example? Normally the = operator is used for variable = (some value or another variable). But in this example its a function = some value. How is that done, or can someone point me to where I can find more info about that?

    thanks

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Here's a simple example:
    Code:
    class Auth
    {
    private:
        int  m_Int;
        double  m_Double;
        std::string  m_Str;
    
    public:
        ...
        Auth& operator=( const Auth&  rhs )
        {
            if ( &rhs != this )
            {
                m_Int = rhs.m_Int;
                m_Double = rhs.m_Double;
                m_Str = rhs.m_Str;
            }
        }
    };
    
    Auth a1;
    ...
    Auth a2;
    a2 = a1;

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    14
    cpjust,

    Thanks for the reply. I know about the =operator when used in that form. In the example I pointed out, the =operator is being used with a function. For example:

    Code:
    a1.some_function() = "somevalue"
    or
    Code:
    a1.some_function(something) = "somevalue"
    Which is how it is being used in the sample code I originally posted.

    thanks

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Is the function returning a reference to some object, whose class has overloaded the = operator?

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    14
    MacGyver, so it could just be that the function is returning a pointer and we are just setting the value of what the pointer is pointing to? I do not know if overloading is being done, would you be able to deduce from the example; if previous sentence is accurate then no overloading needed huh?

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    The methods are returning references, presumably to members of the object. References are valid lvalues, and can be assigned to. So the prototype for such a method may look something like this:
    Code:
    char*& Auth::scheme();
    As others have said, it could also be returning a class that has an overloaded assignment operator that accepts literal strings. Like this:
    Code:
    std::string& Auth::scheme();
    This is an unconventional way to write functions in c++. Usually, you want to pass values that need to be assigned as function parameters to special mutator functions with like setScheme(). Likely, the programmer who wrote that code, is following practices of another programming language.
    Last edited by King Mir; 10-15-2007 at 01:00 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 06:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 12:50 PM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 02:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 06:39 AM

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