1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development C++ const Class & ?

Discussion in 'Software' started by Plugs, 27 Sep 2011.

  1. Plugs

    Plugs Minimodder

    Joined:
    5 Nov 2009
    Posts:
    524
    Likes Received:
    63
    Was attempting to do some small program earlier, and came across the following type that was required for a function, but couldnt seem to get it to work:

    const Class & (more specifically const string &)

    is that a reference to a constant string?

    if i have a string how do i convert it into a const string &
    if i have a const string how do i convert it into a const string &
     
    Last edited: 27 Sep 2011
  2. Dae314

    Dae314 What's a Dremel?

    Joined:
    3 Sep 2010
    Posts:
    988
    Likes Received:
    61
    This maybe?

    I'm not familiar with working with reference types, so I'm not sure how to answer your question, but maybe you can narrow your question/searches now.
     
  3. Cerberus90

    Cerberus90 Car Spannerer

    Joined:
    23 Apr 2009
    Posts:
    7,647
    Likes Received:
    184
    Its a constant reference.

    So instead of passing what could be a potentially large class by value, you can pass the reference to it.
    This speeds up execution. It should only be used if the function is not modifying the data that the reference is pointing to.

    If you've got the string, and you want to pass it in this way :

    void function(const string& a)
    {
    a.func(); //do whatever you need to do with a
    }
     
    Last edited: 28 Sep 2011
  4. Dae314

    Dae314 What's a Dremel?

    Joined:
    3 Sep 2010
    Posts:
    988
    Likes Received:
    61
    What would be the difference between a reference and a pointer, and why would one be used over the other in which situations?
     
  5. glaeken

    glaeken Freeeeeeeze! I'm a cawp!

    Joined:
    1 Jan 2005
    Posts:
    2,041
    Likes Received:
    50
    A reference and a pointer are very similar. They both point to the address of the object. There is a slight cost to dereferencing pointers, but they can be cast to other classes where as a reference cannot. A lot of the times they can be used interchangeably and is just a matter of preference choosing between references and pointers.

    Example:
    Code:
    //classData is a member of MyClass
    
    void MyClass::GetIntRef( int& value ) {
         value = classData;
    }
    
    void MyClass::GetIntPtr( int* value ) {
        *value = classData;
    }
    
    //usages
    MyClass c;
    int x;
    
    c.GetIntRef( x );
    c.GetIntPtr( &x );
    
    The functions are for all intents and purposes equivalent, but using the pointer version it is more obvious that value will be receiving data.


    More info: http://www.parashift.com/c++-faq-lite/references.html
     
    Last edited: 29 Sep 2011
    thehippoz and Dae314 like this.
  6. Dae314

    Dae314 What's a Dremel?

    Joined:
    3 Sep 2010
    Posts:
    988
    Likes Received:
    61
    That was awesome, thanks +rep
     
  7. Plugs

    Plugs Minimodder

    Joined:
    5 Nov 2009
    Posts:
    524
    Likes Received:
    63
    Oops, forgot I had posted this. Thanks for your help. Guess I'll have another attempt at this one day in the future.
     

Share This Page