casting

casting member function pointer

感情迁移 提交于 2021-02-07 14:48:36
问题 I need to use a member function pointer that takes in an argument of base class that used in other code. Well, simply I want do to [something] like the example below. This code works fine, but I wonder if such cast is always safe? I cannot do dynamic or static cast here. #include <cstdio> class C { public: C () : c('c') {} virtual ~C() {} const char c; }; class D : public C { public: D () : d('d') {} virtual ~D() {} const char d; }; class A { public: A () {} virtual ~A() {} void f( C& c ) {

casting member function pointer

蓝咒 提交于 2021-02-07 14:47:31
问题 I need to use a member function pointer that takes in an argument of base class that used in other code. Well, simply I want do to [something] like the example below. This code works fine, but I wonder if such cast is always safe? I cannot do dynamic or static cast here. #include <cstdio> class C { public: C () : c('c') {} virtual ~C() {} const char c; }; class D : public C { public: D () : d('d') {} virtual ~D() {} const char d; }; class A { public: A () {} virtual ~A() {} void f( C& c ) {

Braced functional cast to reference type, a hole in the standard or compilers bug?

纵饮孤独 提交于 2021-02-07 12:52:15
问题 According to the standard, a braced functional cast always results in a prvalue, [expr.cast]/2 Otherwise, the expression is a prvalue of the specified type whose result object is direct-initialized with the initializer. Which is hard to interpret when the specified type is a reference type, as it may happen in generic programming. Compiler have adopted specific behavior in this case: #include <type_traits> struct A { A ()=default; A (const A&); }; template <class T, class U> decltype(auto) f

Remove an object from an ArrayList given only one attribute

末鹿安然 提交于 2021-02-07 12:51:36
问题 I have an ArrayList of Items and I want to be able remove one Item from the list by entering only one Item attribute, for example its number (int ItemNumber). I also wanna do the same when I check Item quantities. These are my equals() & contains() methods, do I need to make any changes here? public boolean contains(T anEntry) { boolean found = false; for (int index = 0; !found && (index < numberOfEntries); index++) { if (anEntry.equals(list[index])) found = true; }//end for return found; } /

How can an (int) be converted to (unsigned int) while preserving the original bit pattern?

老子叫甜甜 提交于 2021-02-07 07:12:11
问题 Suppose that we define: short x = -1; unsigned short y = (unsigned short) x; According to the C99 standard: Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type. (ISO/IEC 9899:1999 6.3.1.3/2) So, assuming two bytes for short and a two's complement representation, the bit patterns of these two integers are: x = 1111 1111 1111 1111

How can an (int) be converted to (unsigned int) while preserving the original bit pattern?

為{幸葍}努か 提交于 2021-02-07 07:11:11
问题 Suppose that we define: short x = -1; unsigned short y = (unsigned short) x; According to the C99 standard: Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type. (ISO/IEC 9899:1999 6.3.1.3/2) So, assuming two bytes for short and a two's complement representation, the bit patterns of these two integers are: x = 1111 1111 1111 1111

Function-style cast vs. constructor

吃可爱长大的小学妹 提交于 2021-02-07 05:39:04
问题 I learned that in C++, typedef foo* mytype; (mytype) a // C-style cast and mytype(a) // function-style cast do the same thing. But I notice the function-style cast share the same syntax as a constructor. Aren't there ambiguous cases, where we don't know if it is a cast or a constructor? char s [] = "Hello"; std::string s2 = std::string(s); // here it's a constructor but why wouldn't it be ... std::string s3 = (std::string) s; // ... interpreted as a function-style cast? 回答1: Syntactically, it

Python: Test if value can be converted to an int in a list comprehension

a 夏天 提交于 2021-02-07 04:47:50
问题 Basically I want to do this; return [ row for row in listOfLists if row[x] is int ] But row[x] is a text value that may or may not be convertible to an int I'm aware that this could be done by: try: int(row[x]) except: meh But it'd be nice to do it is a one-liner. Any ideas? 回答1: If you only deal with integers, you can use str.isdigit(): Return true if all characters in the string are digits and there is at least one character, false otherwise. [row for row in listOfLists if row[x].isdigit()]

Inheritance and casting in Java

帅比萌擦擦* 提交于 2021-02-07 03:28:34
问题 I have a question about inheritance and casting in Java. I have the following two example classes and a test class, and I state my question after the classes: public class Automobile { public int var; public Automobile () { var = 1; } public String toString () { return "AUTOMOBILE: " + var; } } public class Porsche extends Automobile { public int var; public Porsche () { var = 2; } public String toString () { return "PORSCHE: " + var; } } public class Test { public static void main (String []

Inheritance and casting in Java

半腔热情 提交于 2021-02-07 03:26:58
问题 I have a question about inheritance and casting in Java. I have the following two example classes and a test class, and I state my question after the classes: public class Automobile { public int var; public Automobile () { var = 1; } public String toString () { return "AUTOMOBILE: " + var; } } public class Porsche extends Automobile { public int var; public Porsche () { var = 2; } public String toString () { return "PORSCHE: " + var; } } public class Test { public static void main (String []