conversion-operator

C++ Implicit Conversion Operators Precedence

自闭症网瘾萝莉.ら 提交于 2019-12-01 03:21:29
EDIT: Following Mike Seymour's comment, I replaced operator std::string () const; with operator char * () const; and changed the implementation accordingly. This allows implicit casting, but, for some reason, the unsigned long int operator has precedence over the char * operator, which just does not feel right... Also, I don't want to expose nasty C stuff like char * outside the class, when I have std::string. I have a hunch that my CustomizedInt class needs to inherit from some stuff in order to support the feature that I desire. Could anybody please elaborate Mike's comment regarding std:

C++ Implicit Conversion Operators Precedence

主宰稳场 提交于 2019-11-30 23:51:47
问题 EDIT: Following Mike Seymour's comment, I replaced operator std::string () const; with operator char * () const; and changed the implementation accordingly. This allows implicit casting, but, for some reason, the unsigned long int operator has precedence over the char * operator, which just does not feel right... Also, I don't want to expose nasty C stuff like char * outside the class, when I have std::string. I have a hunch that my CustomizedInt class needs to inherit from some stuff in

Operator = Overload with Const Variable in C++

丶灬走出姿态 提交于 2019-11-30 14:14:17
问题 I was wondering if you guys could help me. Here are my .h: Class Doctor { const string name; public: Doctor(); Doctor(string name); Doctor & Doctor::operator=(const Doctor &doc); } and my main: int main(){ Doctor d1 = Doctor("peter"); Doctor d2 = Doctor(); d2 = d1; } I want to do the operator= function. Can anyone help me? Notice the const member on Doctor. ************EDIT:********* My main problem is that I want another class to have an attribute which is a Doctor like a Pacient has a

explicit copy constructor or implicit parameter by value

不打扰是莪最后的温柔 提交于 2019-11-30 02:29:47
I recently read (and unfortunately forgot where), that the best way to write operator= is like this: foo &operator=(foo other) { swap(*this, other); return *this; } instead of this: foo &operator=(const foo &other) { foo copy(other); swap(*this, copy); return *this; } The idea is that if operator= is called with an rvalue, the first version can optimize away construction of a copy. So when called with a rvalue, the first version is faster and when called with an lvalue the two are equivalent. I'm curious as to what other people think about this? Would people avoid the first version because of

Distinguishing between user-defined conversion sequences by the initial standard conversion sequence

核能气质少年 提交于 2019-11-29 16:12:32
问题 The standard appears to provide two rules for distinguishing between implicit conversion sequences that involve user-defined conversion operators: c++11 13.3.3 Best viable function [over.match.best] [...] a viable function F1 is defined to be a better function than another viable function F2 if [...] the context is an initialization by user-defined conversion (see 8.5, 13.3.1.5, and 13.3.1.6) and the standard conversion sequence from the return type of F1 to the destination type (i.e., the type

Type Conversion/Casting Confusion in C++

橙三吉。 提交于 2019-11-29 03:46:03
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. Steve Jessop 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 converted to "unsigned int". The result is the unsigned int value that is equal to i modulo UINT_MAX

What is an “operator int” function?

痞子三分冷 提交于 2019-11-28 04:41:56
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++; } }; 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 compile: INT i(1234); int i_2 = i; // this will implicitly call INT::operator int() Without the convert

Conversion Operators in C++

﹥>﹥吖頭↗ 提交于 2019-11-28 04:32:06
Please help me understand how exactly the conversion operators in C++ work. I have a simple example here which I am trying to understand, though it is not very clear how the conversion actually happens by the compiler. class Example{ public: Example(); Example(int val); operator unsigned int(); ~Example(){} private: int itsVal; }; Example::Example():itsVal(0){} Example::Example(int val):itsVal(val){} Example::operator unsigned int (){ return (itsVal); } int main(){ int theInt = 5; Example exObject = theInt; // here Example ctr(5); int theInt1 = ctr; // here return 0; } You can walk through

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

六眼飞鱼酱① 提交于 2019-11-27 23:10:43
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? } I'm going to guess that it will not, which is unfortunate, but I thought I'd ask, since I couldn't find

C++ Conversion operator for converting to function pointer

不打扰是莪最后的温柔 提交于 2019-11-27 21:10:37
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 Bar instance to a int(*)(int, int) function pointer). Here's what I initially tried: class Bar { private