The use of const and &

This is a discussion on The use of const and & within the C++ Programming forums, part of the General Programming Boards category; Let's say i create a class: Code: class myClass { ... }; I'm used to passing it like this for ...

  1. #1
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625

    The use of const and &

    Let's say i create a class:
    Code:
    class myClass { ... };
    I'm used to passing it like this for read-only functions:
    Code:
    const myClass& aName
    But i realised that some problems come up, so i figured this:
    Code:
    const myClass& aName // for using its member variables
    const myClass aName // for using it directly
    Could you tell me if there is a benefit of using this method, if it is correct?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I'm unclear on what your distinction between "using its member variables" and "using it directly" is.

    Also, what kind of problems?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Sipher View Post
    Could you tell me if there is a benefit of using this method, if it is correct?
    Use the reference whenever you pass a class to a function. It prevents the class from being copied when it's passed to a function.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by Sipher View Post
    But i realised that some problems come up
    Problems you say? We often like to help with problems. What were those problems?

    Don't ask us what we think of your solution, ask us what our solution would be.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    I'm having a hard time imagining a situation where you'd pass a const instance rather than a const reference. If it's const, then it cannot be changed and the question of whether it should be copied or not is moot.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Code:
    class aClass
    {
       public:
           int a;
           
           const aClass Sum(const aClass& c)
           {
              aClass Result;
        
              Result.a = a + c.a;
    
              return Result;
           }
     
           void SumTo(const aClass c)
           {
              *this = Sum(c);
           }
       
    };
    By saying 'using its member variables' i mean using the Sum member functions
    By saying 'using it directly' i mean using the SumTo

    This is of course a very rough example, but that's the idea.

    The compiler, when i pass an aClass through another struct or class instance, throws an error that 'this' function discards qualifiers. What does it mean?

    Many thanks for the interest guys!

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,989
    For the error, it means you're calling a non-const member function on a const object.
    Ie, consider you have
    const aClass& myclass
    And do
    myclass.Sum(...);

    This would result in an error because the function is not guaranteeing that it won't change the function's state, which is what const is all about.
    Adding a "const" keyword after the function (say Sum), will fix that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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