The new syntax default in C 11

0 votes

I'm not sure why I'd ever do something like this:

struct S { 
    int a; 
    S(int aa) : a(aa) {} 
    S() = default; 
};

Why not simply say:

S() {} // instead of S() = default;

Why introduce a new syntax for that?

Jun 2, 2022 in C++ by Nicholas
• 7,760 points
493 views

1 answer to this question.

0 votes

A defaulted default function Object() { [native code] } is defined as a user-defined default function Object() { [native code] } with an empty compound statement and no initialization list.

I'll give you an example to demonstrate the difference:

#include <iostream>

using namespace std;
class A 
{
public:
    int x;
    A(){}
};

class B 
{
public:
    int x;
    B()=default;
};


int main() 
{ 
    int x = 5;
    new(&x)A(); // Call for empty constructor, which does nothing
    cout << x << endl;
    new(&x)B; // Call for default constructor
    cout << x << endl;
    new(&x)B(); // Call for default constructor + Value initialization
    cout << x << endl;
    return 0; 
} 

Output:

5
5
0

As can be seen, calling the empty A() function Object() { [native code] } does not initialise the members, whereas calling B() does.

answered Jun 7, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

What are the new features in C++17?

C++17 is currently feature complete, therefore major ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
402 views
0 votes
1 answer

What is the best way to use a HashMap in C++?

The ordered and unordered map containers (std::map and std::unordered map) are included in the standard library.  The items in an ordered map are sorted by key, and insert and access are in O (log n).  For ordered maps, the standard library often use red black trees.  However, this is only an implementation detail.  Insert and access are in O in an unordered map (1).  It is simply another term for a hashtable. An illustration using (ordered) std::map: #include <map> #include <iostream> #include <cassert> int main(int argc, char ...READ MORE

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

What is a lambda expression in C++11?

In C++11, what is a lambda expression? A: It's the object of an autogenerated class with overloading operator() const under the hood.  Closure is a type of object that is produced by the compiler.  This 'closure' idea is similar to C++11's bind notion.  Lambdas, on the other hand, usually produce better code.  Full inlining is also possible with calls through closures. Q: When do you think I'd utilise one? A: Define "simple and tiny logic" and request that the compiler generate the code from the preceding question.  You tell the compiler the expressions you wish to be inside the operator ().  The compiler will produce everything else for you. Q: What kind of problem do they tackle that couldn't be solved before they were introduced? A: It's some form of syntactic sugar, like using operators instead of functions for custom add, subtract, and other operations... However, wrapping 1-3 lines of genuine logic to some classes, and so on, saves additional lines of needless code!  Some engineers believe that if the number of lines is reduced, there is a lower likelihood of mistakes (which I agree with). Example of usage auto x = [=](int arg1){printf("%i", ...READ MORE

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

How to use new[ ] and delete[ ] operator in C++

int main(){ char *str; ...READ MORE

answered Jun 20, 2022 in C++ by Damon
• 4,960 points
581 views
0 votes
0 answers

Is hiding implementation detail Encapsulation or Abstraction?

I have seen some people defining abstraction ...READ MORE

May 6, 2022 in Java by narikkadan
• 63,600 points
3,188 views
0 votes
1 answer

Why would anyone use set instead of unordered_set?

Unordered sets must compensate for their O(1) ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
2,967 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
491 views
0 votes
1 answer

C++ auto keyword. Why is it magic?

Auto was a keyword that C++ "inherited" from C and had been around for a long time but was almost never used because there were only two possibilities: it wasn't allowed or it was assumed by default. C++11 introduced the usage of auto to denote an inferred type. Similarly to how template type deduction works for function templates, auto x = initializer deduces the type of x from the type of initializer.  Consider the following function template: template<class T> int whatever(T t) { ...READ MORE

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

Function default argument value depending on argument name in C++ [duplicate]

When the function is called with no argument for the corresponding parameter, the default argument is evaluated.  In a default argument, a parameter must not appear as a potentially evaluated expression.  A function's parameters declared before a default argument are in scope and can obscure the namespace and class member name. It provides the following example: int h(int a, ...READ MORE

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

Are virtual functions the only way to achieve Runtime Polymorphism in C++?

fprintf is a polymorphism function in the C programming language. It can print to a file, stdout, a printer, a socket, or whatever else the system can represent as a stream if you supply it different handles. FILE* file = fopen("output.txt", "w"); ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
491 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