What is this weird colon-member syntax in the constructor

0 votes
#include <iostream>

class Foo {
public:
  int bar;
  Foo(int num): bar(num) {};
};

int main(void) {
  std::cout << Foo(42).bar << std::endl;
  return 0;
}

What does this weird phrase mean: bar(num)? It appears to initialize the member variable, but this is the first time I've seen this syntax. It appears to be a call to a function/constructor, but for an int? It doesn't make sense to me. Someone might be able to shed some light on this for me. Are there any more arcane language features like this that you won't discover in a standard C++ book?

May 27, 2022 in Others by Nicholas
• 7,760 points
1,110 views

1 answer to this question.

0 votes

Foo(int num): bar(num)

In C++, this is known as a Member Initializer List.

Simply put, it sets the value of your member bar to num.

There is a significant difference between initializing a member with the Member initializer list and assigning a value to it within the function Object() { [native code] } body.

When you use the Member initializer list to initialise fields, the constructors are only called once, and the object is constructed and initialised in a single operation.

If you use assignment, the fields will be initialised with default constructors and then reassigned with actual values (via the assignment operator).

As you can see, there is an extra overhead of creation and assignment in the latter, which may be significant for user defined classes.

Cost of Member Initialization =Object Construction

 Cost of Member Assignment =Object Construction + Assignment
answered May 27, 2022 by Damon
• 4,960 points

Related Questions In Others

+4 votes
0 answers

What is the best way to pass CISSP Exam In first attempt?

Is CISSP Certification Worth. And What is ...READ MORE

Jun 25, 2019 in Others by Eric
• 320 points
1,027 views
+1 vote
1 answer

what is the difference between error and stderr in Node.js?

Error is an object created by Node.js to handle ...READ MORE

answered Jul 4, 2019 in Others by sunshine
• 1,300 points
1,976 views
0 votes
1 answer

What is the difference between POST and PUT in HTTP?

To answer your question, both of them ...READ MORE

answered Feb 11, 2022 in Others by Rahul
• 9,670 points
420 views
0 votes
1 answer

What is the purpose of the "role" attribute in HTML

roles were initially defined by XHTML but ...READ MORE

answered Feb 27, 2022 in Others by narikkadan
• 63,420 points
317 views
0 votes
1 answer

What does the explicit keyword mean?

To resolve the parameters to a function, the compiler is permitted to do one implicit conversion.  This implies that the compiler can utilise constructors with a single argument to convert from one type to another to find the correct type for a parameter. Here's an example class with a constructor ...READ MORE

answered Jun 27, 2022 in C++ by Damon
• 4,960 points
262 views
0 votes
1 answer

The Definitive C++ Book Guide and List

For Beginner (includes those without coding experience) Programming: ...READ MORE

answered Jun 6, 2022 in C++ by pranav
• 2,590 points
503 views
0 votes
1 answer

Why is "using namespace std;" considered bad practice?

This has nothing to do with performan ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
636 views
0 votes
1 answer

What is a smart pointer and when should I use one?

A smart pointer is similar to a ...READ MORE

answered Jun 2, 2022 in C++ by Damon
• 4,960 points
268 views
0 votes
1 answer

How to find out if an item is present in an std::vector?

The most straightforward solution is to count the total number of elements in the vector that have the specified value.  If the count is greater than zero, we've found our element.  This is simple to accomplish with the std::count function. #include <iostream> #include <vector> #include <algorithm> int main() { ...READ MORE

answered May 27, 2022 in Others by Damon
• 4,960 points
11,339 views
0 votes
1 answer

Why do we need virtual functions in C++?

A virtual function is a base class member function that we expect to redefine in derived classes. In the base class, a virtual function is used to ensure that the function is overridden.  This is especially true when a pointer from a base class points to an object from a derived class. For example, consider the code below: class Base ...READ MORE

answered May 27, 2022 in Others by Damon
• 4,960 points
707 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP