Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between C++ object construction methods

The different construction syntaxes in C++ have always confused me a bit. In another question, it was suggested to try initializing a string like so

std::string foo{ '\0' };

This works and produces the intended result: a string of length 1 containing only the null character. In testing the code, I accidentally typed

std::string foo('\0');

This compiles fine (no warnings even with -Wall), but terminates at runtime with

terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_M_construct null not valid
Aborted (core dumped)

Now, as far as I can tell, there is no constructor for std::string which takes a single character as an argument, and this hypothesis is further confirmed when I attempt to pass the character indirectly.

char b = '\0';
std::string a(b);

This produces a nice, lengthy compile error. As does this

std::string a('z');

So my question is: what allows std::string a('\0'); to compile, and what makes it different from std::string a{ '\0' };?


Footnote: Compiling using g++ on Ubuntu. This doesn't strike me as a compiler bug, but just in case...

like image 411
Silvio Mayolo Avatar asked Jan 15 '18 00:01

Silvio Mayolo


People also ask

What is the difference between method and object?

an object is an element (or instance) of a class; objects have the behaviors of their class. The object is the actual component of programs, while the class specifies how instances are created and how they behave. method: a method is an action which an object is able to perform.

What is the difference between constructor and method in C?

A Constructor is a block of code that initializes a newly created object. A Method is a collection of statements which returns a value upon its execution. A Constructor can be used to initialize an object.

What is the difference between object and constructor?

A constructor is a function that creates and initializes an object. JavaScript provides a special constructor function called Object() to build the object. The return value of the Object() constructor is assigned to a variable. The variable contains a reference to the new object.

What is the difference between constructor and method in Apex?

Constructor are used to initialize the state of object,where as method is expose the behaviour of object. 2. Constructor must not have return type where as method must have return type.


1 Answers

Character '\0' is implicitly convertible to integer value of 0 thus representing implementation-defined null pointer constant. This:

std::string foo('\0');

calls a constructor overload accepting pointer of type const char* as a parameter and results in undefined behavior. It is equivalent to passing 0 or NULL:

std::string foo(0); // UB
std::string bar(NULL); // UB

The reference for the 4th and 5th constructor overloads states:

The behavior is undefined if s... including the case when s is a null pointer.

The second statement:

std::string foo{'\0'}; // OK

calls a constructor accepting std::initializer_list<char> as a parameter and does not cause UB.

You could call the constructor overload accepting count number of chars instead:

std::string s(1, '\0');
like image 54
Ron Avatar answered Nov 01 '22 21:11

Ron