Implicit conversion from user-defined type to primitive type in C++

Matthew James Briggs

I am able to find plenty of information about implicit conversion from, say, an int to a user defined type. i.e. if a constructor takes an int as its parameter and is not prefaced by "explicit" then implicit conversions can occur.

What if I want my class to implicitly convert to an int?

For example, what function needs to be added either inside or outside of SimpleClass such that the main function will compile and output "1" to the console? (see comments)

#include <iostream>

class SimpleClass
{
private:
    int m_int;
public:
    SimpleClass(int value)
    : m_int(value) {}
};

int main(int argc, const char * argv[])
{
    SimpleClass simpleclass(1);
    int i = simpleclass; // does not complile
    std::cout << i << std::endl; // should output "1" to the console
    return 0;
}
Jan Hudec

Implicit conversions can be defined in two ways:

  • non-explicit single-argument constructor.
  • non-explicit conversion function (a.k.a. conversion operator), N3337 12.3.2

The later allows defining conversion from class type to primitive type. Just add

class SimpleClass {
   // ...
   operator int() const;
};

SimpleClass::operator int() const
{
   return m_int;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Implicit conversion from user-defined type to primitive type in C++

From Dev

Implicit conversion from lambda expression to user-defined type

From Dev

c++ User defined conversion - implicit conversion

From Dev

c++ User defined conversion - implicit conversion

From Dev

Implicit integer type conversion in C

From Dev

At most how many user-defined conversion operator can be implicitly applied during an implicit type conversion?

From Dev

At most how many user-defined conversion operator can be implicitly applied during an implicit type conversion?

From Dev

implicit conversion of vector from one type to another c++

From Dev

C++ User-defined type conversion failure

From Dev

Implicit conversion from enumeration type with Xcode 6

From Dev

C++ constructor implicit type conversion

From Dev

explicit/implicit type conversion c++

From Dev

C++ implicit conversion of pointer type

From Dev

explicit/implicit type conversion c++

From Dev

c++ implicit type conversion string -> int?

From Dev

Implicit conversion for generic type?

From Dev

Swift implicit conversion of type

From Dev

Implicit conversion of function type

From Dev

Implicit type conversion with structs

From Dev

Implicit type conversion with structs

From Dev

Scala: Implicit Conversion From Generic Type to Second Generic Type

From Dev

Retrieve schema of User Defined Table Type from C#

From Dev

Retrieve schema of User Defined Table Type from C#

From Dev

How to enable implicit type conversion from non-const to const in C++?

From Dev

C++ Why use an implicit conversion from (std::string) to (void) type?

From Dev

Implicit conversion from data type varchar to varbinary(max) is not allowed c#

From Dev

Visual C++ - call conversion operator on a primitive type explicitly

From Dev

Implicit type conversion for lambda expression

From Dev

Implicit conversion in template type deduction

Related Related

  1. 1

    Implicit conversion from user-defined type to primitive type in C++

  2. 2

    Implicit conversion from lambda expression to user-defined type

  3. 3

    c++ User defined conversion - implicit conversion

  4. 4

    c++ User defined conversion - implicit conversion

  5. 5

    Implicit integer type conversion in C

  6. 6

    At most how many user-defined conversion operator can be implicitly applied during an implicit type conversion?

  7. 7

    At most how many user-defined conversion operator can be implicitly applied during an implicit type conversion?

  8. 8

    implicit conversion of vector from one type to another c++

  9. 9

    C++ User-defined type conversion failure

  10. 10

    Implicit conversion from enumeration type with Xcode 6

  11. 11

    C++ constructor implicit type conversion

  12. 12

    explicit/implicit type conversion c++

  13. 13

    C++ implicit conversion of pointer type

  14. 14

    explicit/implicit type conversion c++

  15. 15

    c++ implicit type conversion string -> int?

  16. 16

    Implicit conversion for generic type?

  17. 17

    Swift implicit conversion of type

  18. 18

    Implicit conversion of function type

  19. 19

    Implicit type conversion with structs

  20. 20

    Implicit type conversion with structs

  21. 21

    Scala: Implicit Conversion From Generic Type to Second Generic Type

  22. 22

    Retrieve schema of User Defined Table Type from C#

  23. 23

    Retrieve schema of User Defined Table Type from C#

  24. 24

    How to enable implicit type conversion from non-const to const in C++?

  25. 25

    C++ Why use an implicit conversion from (std::string) to (void) type?

  26. 26

    Implicit conversion from data type varchar to varbinary(max) is not allowed c#

  27. 27

    Visual C++ - call conversion operator on a primitive type explicitly

  28. 28

    Implicit type conversion for lambda expression

  29. 29

    Implicit conversion in template type deduction

HotTag

Archive