Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between explicit and implicit assignment in C++

int value = 5; // this type of assignment is called an explicit assignment
int value(5); // this type of assignment is called an implicit assignment

What is the difference between those, if any, and in what cases do explicit and implicit assignment differ and how?


http://weblogs.asp.net/kennykerr/archive/2004/08/31/Explicit-Constructors.aspx

EDIT: I actually just found this article, which makes the whole thing a lot clearer... and it brings up another question, should you (in general) mark constructors taking a single parameter of a primitive type - numeric/bool/string - as explicit and leave the rest as they are (of course keeping watch for gotchas such as constructors like (int, SomeType = SomeType())?

like image 992
Jake Petroules Avatar asked Jun 16 '10 23:06

Jake Petroules


People also ask

What is the difference between implicit and explicit type conversion in C?

Implicit conversion is the conversion in which a derived class is converted into a base class like int into a float type. Explicit conversion is the conversion that may cause data loss. Explicit conversion converts the base class into the derived class.

What are implicit and explicit variable declaration?

Explicit variable declaration means that the type of the variable is declared before or when the variable is set. Implicit variable declaration means the type of the variable is assumed by the operators, but any data can be put in it.

What is an example of implicit?

The definition of implicit refers to something that is suggested or implied but not ever clearly said. An example of implicit is when your wife gives you a dirty look when you drop your socks on the floor. Implied indirectly, without being directly expressed.

What is the difference between implicit type conversion and implicit type conversion?

An implicit type conversion is automatically performed by the compiler when differing data types are intermixed in an expression. An explicit type conversion is user-defined conversion that forces an expression to be of specific type. An implicit type conversion is performed without programmer's intervention.


2 Answers

Neither of these is an assignment of any kind -- they're both initialization. The first uses copy initialization, and the second direct initialization. (FWIW, I'm pretty sure I've never heard the terms "explicit assignment" or "implicit assignment" before).

Edit: (Mostly in response to Nathan's comment):

Here's a corrected version of the code from your comment:

#include <iostream>

struct Foo { 
    Foo() { 
        std::cout << "Foo::ctor()" << std::endl; 
    } 
    Foo(Foo const& copy) { 
        std::cout << "Foo::cctor()" << std::endl; 
    } 
    Foo& operator=(Foo const& copy) { 
        std::cout << "foo::assign()" << std::endl; 
        return *this; 
    } 
};

int main(int, const char**) { 
    Foo f; 
    Foo b(f); 
    Foo x = b;
    return 0; 
}

The result from running this should be:

Foo::ctor()
Foo::cctor()
Foo::cctor()

If you run it and get an foo::assign(), throw your compiler away and get one that works (oh, and let us know what compiler it is that's so badly broken)!

like image 78
Jerry Coffin Avatar answered Oct 20 '22 07:10

Jerry Coffin


They differ if a class has a constructor marked 'explicit'. Then, one of these does not work.

Otherwise, no difference.

like image 35
Pavel Radzivilovsky Avatar answered Oct 20 '22 05:10

Pavel Radzivilovsky