What Does :x(a) Mean in a C++ Constructor?

  • Context: C/C++ 
  • Thread starter Thread starter yungman
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion centers around the syntax used in a C++ constructor, specifically the meaning of the initializer list syntax ":x(a)" in the context of a class definition. Participants explore the concept of initializer lists, constructor syntax, and the implications of having empty function bodies in constructors.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about the syntax ":x(a)" in the constructor and seeks clarification.
  • Another participant suggests that the line may be shorthand for an empty class, indicating uncertainty about their own understanding of C++.
  • Some participants clarify that ":x(a)" is an initializer list that initializes the member variable x with the value of a.
  • Links to external resources explaining initializer lists are provided by participants to aid understanding.
  • There is a mention that the empty braces in the default constructor are necessary for it to have a function body, even if there are no statements inside.

Areas of Agreement / Disagreement

Participants generally agree on the function of the initializer list in the constructor, but there is some initial confusion and differing interpretations of the syntax. The discussion includes both clarification and uncertainty regarding the broader implications of constructor syntax.

Contextual Notes

Some participants express uncertainty about their own knowledge of C++, which may affect their interpretations of the syntax discussed.

Who May Find This Useful

Individuals learning C++ or those seeking clarification on constructor syntax and initializer lists may find this discussion beneficial.

yungman
Messages
5,741
Reaction score
291
I am copying some program from youtube videos. This is the program I copied:
C++:
#include <iostream>
using namespace std;
class Base
{ int x;
  public:  Base() {} Base(int a) :x(a) {  }
           int get(){return x;}
           void set(int a){x=a;}
};
int main()
{    Base b1; Base b2(10);
    b2.get();    b2.set(20);    return 0;
}

I don't know what is :x(a) in line 5 .

I really don't even understand line 5, Base() {} Base(int a) :x(a) { } what is this?

Never learn this, don't even know the name to search online. Please help

Thanks
 
Technology news on Phys.org
Have you tried Googling the line itself?
"public: Base() {} Base(int a) :x(a) { }"

I suspect that it is shorthand for some sort of empty class but my C++ is very rusty.

Code:
public: 
    Base()
    {
    
    } Base(int a) :x(a)
      {
      
      }
 
DaveC426913 said:
I suspect that it is shorthand for some sort of empty class but my C++ is very rusty.
No, it's not. That line merely initializes the x member to the value that a represents. Just as @phyzguy said.
 
  • Informative
Likes   Reactions: DaveC426913
The reason the empty braces are there is that a constructor has to have a function body, even if there are no executable statetments.
 
  • Like
Likes   Reactions: yungman
jtbell said:
The reason the empty braces are there is that a constructor has to have a function body, even if there are no executable statetments.
Just want to say thank you for your thread on ThreeVector. I really learn a lot so far and I am still working on it. I take where you start and run with it to see what should and what should not be done and why.

Thanks
 

Similar threads

  • · Replies 36 ·
2
Replies
36
Views
6K
Replies
89
Views
7K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 40 ·
2
Replies
40
Views
4K
  • · Replies 39 ·
2
Replies
39
Views
5K