conversion-operator

Type Conversion/Casting Confusion in C++

℡╲_俬逩灬. 提交于 2019-11-27 17:51:55
问题 What is Type Conversion and what is Type Casting ? When should I use each of them? Detail: Sorry if this is an obvious question; I'm new to C++, coming from a ruby background and being used to to_s and to_i and the like. 回答1: Conversion is when a value is, um, converted to a different type. The result is a value of the target type, and there are rules for what output value results from what input (of the source type). For example: int i = 3; unsigned int j; j = i; // the value of "i" is

C++ Conversion operator for converting to function pointer

社会主义新天地 提交于 2019-11-27 04:29:28
问题 I'm been grinding my head against an idea that is simple enough in my head, but I can't figure out how to implement in C++. Normally, I can declare a class with a conversion operator like in this simple example: class Foo { private: int _i; public: Foo( int i ) : _i(i) { } operator int( ) const { return i; } }; So now I can write awesome stuff like int i = Foo(3); But in my particular case, I would like to provide an operator for converting an object to a function pointer (e.g. converting a

What is an “operator int” function?

前提是你 提交于 2019-11-27 00:35:19
问题 What is the "operator int" function below? What does it do? class INT { int a; public: INT(int ix = 0) { a = ix; } /* Starting here: */ operator int() { return a; } /* End */ INT operator ++(int) { return a++; } }; 回答1: The bolded code is a conversion operator. (AKA cast operator) It gives you a way to convert from your custom INT type to another type (in this case, int ) without having to call a special conversion function explicitly. For example, with the convert operator, this code will

What is the meaning of “operator bool() const”

↘锁芯ラ 提交于 2019-11-26 21:31:23
For example: operator bool() const { return col != 0; } col is an int. How does operator bool() const work? Member functions of the form operator TypeName() are conversion operators. They allow objects of the class type to be used as if they were of type TypeName and when they are, they are converted to TypeName using the conversion function. In this particular case, operator bool() allows an object of the class type to be used as if it were a bool . For example, if you have an object of the class type named obj , you can use it as if (obj) This will call the operator bool() , return the

Can you catch an exception by the type of a conversion operator?

核能气质少年 提交于 2019-11-26 21:20:35
问题 I don't know how to phrase the question very well in a short subject line, so let me try a longer explanation. Suppose I have these exception classes: class ExceptionTypeA : public std::runtime_error { // stuff }; class ExceptionTypeB : public std::runtime_error { // stuff operator ExceptionTypeA() const; // conversion operator to ExceptionTypeA }; Can I then do this, and have it trigger the catch block? try { throw ExceptionTypeB(); } catch (ExceptionTypeA& a) { // will this be triggered? }

What is the meaning of “operator bool() const”

不羁岁月 提交于 2019-11-26 07:57:04
问题 For example: operator bool() const { return col != 0; } col is an int. How does operator bool() const work? 回答1: Member functions of the form operator TypeName() are conversion operators. They allow objects of the class type to be used as if they were of type TypeName and when they are, they are converted to TypeName using the conversion function. In this particular case, operator bool() allows an object of the class type to be used as if it were a bool . For example, if you have an object of

How do conversion operators work in C++?

半世苍凉 提交于 2019-11-26 05:17:56
问题 Consider this simple example: template <class Type> class smartref { public: smartref() : data(new Type) { } operator Type&(){ return *data; } private: Type* data; }; class person { public: void think() { std::cout << \"I am thinking\"; } }; int main() { smartref<person> p; p.think(); // why does not the compiler try substituting Type&? } How do conversion operators work in C++? (i.e) when does the compiler try substituting the type defined after the conversion operator? 回答1: Some random

Conversion constructor vs. conversion operator: precedence

纵饮孤独 提交于 2019-11-26 03:35:52
问题 Reading some questions here on SO about conversion operators and constructors got me thinking about the interaction between them, namely when there is an \'ambiguous\' call. Consider the following code: class A; class B { public: B(){} B(const A&) //conversion constructor { cout << \"called B\'s conversion constructor\" << endl; } }; class A { public: operator B() //conversion operator { cout << \"called A\'s conversion operator\" << endl; return B(); } }; int main() { B b = A(); //what