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

  • SEARCH
  • Home
  • 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 784727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:45:34+00:00 2026-05-14T20:45:34+00:00

Possible Duplicates: Variables After the Colon in a Constructor C++ constructor syntax question (noob)

  • 0

Possible Duplicates:
Variables After the Colon in a Constructor
C++ constructor syntax question (noob)

I have some C++ code here:

class demo 
{
private:
    unsigned char len, *dat;

public:
    demo(unsigned char le = 5, unsigned char default) : len(le) 
    { 
        dat = new char[len];                                      
        for (int i = 0; i <= le; i++)                             
            dat[i] = default;
    }

    void ~demo(void) 
    {                                            
        delete [] *dat;                                           
    }
};

class newdemo : public demo 
{
private:
    int *dat1;

public:
    newdemo(void) : demo(0, 0)
    {
     *dat1 = 0;                                                   
     return 0;                                                    
    }
};

My question is, what are the : len(le) and : demo(0, 0) called?

Is it something to do with inheritance?

  • 1 1 Answer
  • 0 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. Editorial Team
    Editorial Team
    2026-05-14T20:45:35+00:00Added an answer on May 14, 2026 at 8:45 pm

    As others have said, it’s a member initializer list. You can use it for two things:

    1. Calling base class constructors
    2. Initializing data members before the body of the constructor executes

    For case #1, I assume you understand inheritance (if that’s not the case, let me know in the comments). So you are simply calling the constructor of your base class.

    For case #2, the question may be asked: "Why not just initialize it in the body of the constructor?" The importance of the member initializer lists is particularly evident for const members. For instance, take a look at this situation, where I want to initialize m_val based on the constructor parameter:

    class Demo
    {
        Demo(int& val) 
         {
             m_val = val;
         }
    private:
        const int& m_val;
    };
    

    By the C++ specification, this is illegal. We cannot change the value of a const variable in the constructor, because it is marked as const. So you can use the initializer list:

    class Demo
    {
        Demo(int& val) : m_val(val)
         {
         }
    private:
        const int& m_val;
    };
    

    That is the only time that you can change a const data member. And as Michael noted in the comments section, it is also the only way to initialize a reference that is a data member.

    Outside of using it to initialize const data members, it seems to have been generally accepted as "the way" of initializing members, so it’s clear to other programmers reading your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicates: C++ weird constructor syntax Variables After the Colon in a Constructor What
Possible Duplicate: What is useful about this C syntax? C variable declarations after function
Possible Duplicate: how to get GET and POST variables with JQuery? I have the
Possible Duplicate: Python nested functions variable scoping After much trial and error I have
Possible Duplicate: Is there some ninja trick to make a variable constant after its
Possible Duplicates: “Usual” functions vs function variables in JavaScript What do you call this
Possible Duplicates: Cannot refer to a non-final variable inside an inner class defined in
Possible Duplicate: Creating or assigning variables from a dictionary in Python Hello we have
I have code structured like below. public class Stats { public string URL {
Possible Duplicate: Perl: How do you insert numbers after grouping variable? I have the

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.