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 4616026
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:55:25+00:00 2026-05-22T01:55:25+00:00

Possible Duplicate: What is this weird colon-member syntax in the constructor? So I was

  • 0

Possible Duplicate:
What is this weird colon-member syntax in the constructor?

So I was looking through some samples in a Direct X 10 book, when I came across this

PeaksAndValleys::PeaksAndValleys()
: mNumRows(0), mNumCols(0), mNumVertices(0), mNumFaces(0),
  md3dDevice(0), mVB(0), mIB(0)
{
}

I understand this is probably supposed to be a constructor……but i’ve never seen one like this? can someone explain? specifically why is there a single : after it’s declared?

  • 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-22T01:55:26+00:00Added an answer on May 22, 2026 at 1:55 am

    This is called an initialization list, and is used for initializing class members. It is useful both as a shorthand and it is required for initializing members that do not have a default constructor. For example:

    #include <iostream>
    using namespace std;
    
    class Foo {
    public:
        Foo(int x) { cout << "Foo(" << x << ")" << endl; }
    };
    
    class Bar : public Foo {
        Foo member;
    public:
        Bar() { /* error */ }
    };
    

    This gives an error because member cannot be default constructed, as Foo does not have a default constructor. Change it to

    Bar(): member(42) {}
    

    and now it works.

    This syntax is also useful for initializing const members of a class, as while they might be default-constructible, you cannot overwrite them in the constructor body.

    class Baz {
        const member;
    public:
        Baz(int x): member(x) {}
    };
    

    The same idea also applies to references, as they also must be initialized directly. Finally, it is used for specifying arguments for the base class constructor.

    class Xyzzy : public Foo {
    public:
        Xyzzy(int y): Foo(y+3) {}
    };
    
    • 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: Cannot use ‘this’ in member initializer? Any ideas why I get an
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: Why ‘this’ is a pointer and not a reference? Is there a
Possible Duplicate: Why is this not allowed in C++? Why is this not allowed
Possible Duplicate: Why does this conversion doesn't work? Hi, i discovered a strange behaviour
Possible Duplicate: jQuery $(this) vs this When using jQuery, whats the difference between this
Possible Duplicate: Reference - What does this symbol mean in PHP? I was wondering
Possible Duplicate: Why is lock(this) {…} bad? In C# to make a critical region
Possible Duplicate: How to convert this code to use string I have a function

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.