Tag: 铸造

经常转换与static_cast vs. dynamic_cast

我已经写了C和C ++代码近二十年了,但是这些语言的一个方面我从来没有真正理解。 我显然使用了常规演员,即 MyClass *m = (MyClass *)ptr; 到处都是,但似乎还有两种types的演员,我不知道区别。 以下几行代码有什么区别? MyClass *m = (MyClass *)ptr; MyClass *m = static_cast<MyClass *>(ptr); MyClass *m = dynamic_cast<MyClass *>(ptr);

整数除法:你如何产生一个双精度?

对于这个代码块: int num = 5; int denom = 7; double d = num / denom; d的值是0.0 。 它可以通过强制转换工作: double d = ((double) num) / denom; 但是还有另一种方法来获得正确的double结果吗? 我不喜欢投下原始人,谁知道会发生什么。

如何将一个因子转换为整数\数字而不会丢失信息?

当我把一个因子转换成数字或整数时,我得到了底层的代码,而不是数值。 f <- factor(sample(runif(5), 20, replace = TRUE)) ## [1] 0.0248644019011408 0.0248644019011408 0.179684827337041 ## [4] 0.0284090070053935 0.363644931698218 0.363644931698218 ## [7] 0.179684827337041 0.249704354675487 0.249704354675487 ## [10] 0.0248644019011408 0.249704354675487 0.0284090070053935 ## [13] 0.179684827337041 0.0248644019011408 0.179684827337041 ## [16] 0.363644931698218 0.249704354675487 0.363644931698218 ## [19] 0.179684827337041 0.0284090070053935 ## 5 Levels: 0.0248644019011408 0.0284090070053935 … 0.363644931698218 as.numeric(f) ## [1] 1 1 3 […]

什么时候应该使用static_cast,dynamic_cast,const_cast和reinterpret_cast?

什么是正确的用途: static_cast dynamic_cast const_cast reinterpret_cast C风格演员(type)value 函数式投射type(value) 如何决定在哪些特定情况下使用哪个?

我inputmalloc的结果吗?

在这个问题中 ,有人在评论中提出,我不应该把malloc的结果,即 int *sieve = malloc(sizeof(int) * length); 而不是: int *sieve = (int *) malloc(sizeof(int) * length); 为什么会这样呢?

如何将数组转换为PHP中的对象?

我如何转换这样的数组对象? [128] =>数组 ( [状态] =>图A. Facebook的水平滚动条以1024×768的屏幕分辨率显示。 ) [129] =>数组 ( 在工作的那天,我有一些闲暇时间 ) )

你如何将一个超类型列表投给一个子类型列表?

例如,假设你有两个类: public class TestA {} public class TestB extends TestA{} 我有一个返回一个List<TestA>的方法,我想将该列表中的所有对象都转换为TestB以便最终得到一个List<TestB> 。

将String转换为Java中的double

我想将我的double值更改为String 。 if((e.getSource() == jBook)) { String name = jlbName.getText(); String date = jlbDateProduce.getText(); String time = jr1.getText(); int number = (Integer.parseInt(jtfNoOfTicket.getText().trim())); String total = jlbTotal.getText(); String price = jlbPrice.getText(); //Passing ticketReservation frame = new ticketReservation( name, date, time, price, total, String.valueOf(number)); } 在另外一类中,我需要用这个值来计算总数 。 有没有什么方法可以将String转换为double ?

在C#中从枚举中获取int值

我有一个叫做Questions (复数)的课。 在这个类中有一个名为Question (单数)的枚举,看起来像这样。 public enum Question { Role = 2, ProjectFunding = 3, TotalEmployee = 4, NumberOfServers = 5, TopBusinessConcern = 6 } 在Questions类中我有一个get(int foo)函数,该函数返回该foo一个Questions对象。 有一个简单的方法来从整数值关闭枚举,所以我可以做这样的Questions.Get(Question.Role) ?