Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 128317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:36:59+00:00 2026-05-11T05:36:59+00:00

As a novice C++ programmer there are some constructs that look still very obscure

  • 0

As a novice C++ programmer there are some constructs that look still very obscure to me, one of these is const. You can use it in so many places and with so many different effects that is nearly impossible for a beginner to come out alive. Will some C++ guru explain once forever the various uses and whether and/or why not to use them?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T05:36:59+00:00Added an answer on May 11, 2026 at 5:36 am

    Trying to collect some uses:

    Binding some temporary to reference-to-const, to lengthen its lifetime. The reference can be a base – and the destructor of it doesn’t need to be virtual – the right destructor is still called:

    ScopeGuard const& guard = MakeGuard(&cleanUpFunction); 

    Explanation, using code:

    struct ScopeGuard {      ~ScopeGuard() { } // not virtual };  template<typename T> struct Derived : ScopeGuard {      T t;      Derived(T t):t(t) { }     ~Derived() {         t(); // call function     } };  template<typename T> Derived<T> MakeGuard(T t) { return Derived<T>(t); } 

    This trick is used in Alexandrescu’s ScopeGuard utility class. Once the temporary goes out of scope, the destructor of Derived is called correctly. The above code misses some small details, but that’s the big deal with it.


    Use const to tell others methods won’t change the logical state of this object.

    struct SmartPtr {     int getCopies() const { return mCopiesMade; } }; 

    Use const for copy-on-write classes, to make the compiler help you to decide when and when not you need to copy.

    struct MyString {     char * getData() { /* copy: caller might write */ return mData; }     char const* getData() const { return mData; } }; 

    Explanation: You might want to share data when you copy something as long as the data of the originally and the copie’d object remain the same. Once one of the object changes data, you however need now two versions: One for the original, and one for the copy. That is, you copy on a write to either object, so that they now both have their own version.

    Using code:

    int main() {     string const a = '1234';     string const b = a;     // outputs the same address for COW strings     cout << (void*)&a[0] << ', ' << (void*)&b[0]; } 

    The above snippet prints the same address on my GCC, because the used C++ library implements a copy-on-write std::string. Both strings, even though they are distinct objects, share the same memory for their string data. Making b non-const will prefer the non-const version of the operator[] and GCC will create a copy of the backing memory buffer, because we could change it and it must not affect the data of a!

    int main() {     string const a = '1234';     string b = a;     // outputs different addresses!     cout << (void*)&a[0] << ', ' << (void*)&b[0]; } 

    For the copy-constructor to make copies from const objects and temporaries:

    struct MyClass {     MyClass(MyClass const& that) { /* make copy of that */ } }; 

    For making constants that trivially can’t change

    double const PI = 3.1415; 

    For passing arbitrary objects by reference instead of by value – to prevent possibly expensive or impossible by-value passing

    void PrintIt(Object const& obj) {     // ... } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a very novice OCaml programmer so please forgive me if this is a
I am a novice programmer who is trying to teach myself to code, specifically
I am a novice-intermediate programmer taking a stab at AJAX. While reading up on
I'm a Python novice, trying to use pyCurl. The project I am working on
Is there a way to make a list that holds functions? What I'm trying
Let's say I have a class that exposes one property. Is it considered to
As developer, I am often interested in new language feature that can make your
As a novice developer who is getting into the rhythm of my first professional
As a novice, I've spent time learning a smattering of C and a fair
I am a AS3 novice learning PureMVC and want to write code following best

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.