CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2008
    Posts
    21

    Red face What is the overhead for const class/functions in C++

    I am very confused...here is the original question:

    What sort of performance overhead is incurred when using const in C++ programs to create classes and member functions that are considered to be ‘const correct’?

    Really appreciate any help as I am very clear about the concept "overhead" in general.

    Thank you so much!

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: What is the overhead for const class/functions in C++

    The idea is that, at least in some cases, the compiler is better able to optimize const-correct functions. So really there should be negative overhead, if any.

    Of course, the degree of slowdown during compilation resulting from the extra check is probably positive, but likely negligible.

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: What is the overhead for const class/functions in C++

    I only see a performance overhead on the compiler, who has more checks to do. Because a const in-correct program will not build (and run).
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Nov 2003
    Posts
    1,405

    Re: What is the overhead for const class/functions in C++

    Quote Originally Posted by lqakane
    What sort of performance overhead is incurred when using const in C++ programs to create classes and member functions that are considered to be ‘const correct’?

    Really appreciate any help as I am very clear about the concept "overhead" in general.
    "Overhead" usually means what you pay extra for a feature in terms of more memory and/or slower execution.

    There's an old "truth" in programming saying that introducing restrictions is good both for programmers and compilers. Constness is a restriction so it shouldn't have any overhead at all, rather the opposite. Okay there may be more work for the compiler but that doesn't count as overhead of your program because the compiler isn't part of your program.

    Where did you get this question from?
    Last edited by _uj; September 9th, 2008 at 03:11 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured