标签: casting

const_cast 会浪费额外的内存吗?

我们先看一下例子。

#include <iostream>
int main()
{
    const int constant = 1;
    const int* const_p = &constant;
    int* modifier = const_cast<int*>(const_p);
    *modifier = 100;
    std::cout << "constant: " << constant << ", *const_p=" << *const_p;
    //Output: constant: 1, *const_p=100
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我不知道它在内存架构上是如何实现的。看来编译器在堆栈中占用了额外的内存空间,以便我们可以跟踪值为constant的“原始” 1,以及堆栈中值为 的新内存位置100。是吗?那么,const_cast确实会消耗额外的内存,这是初学者可能没有想到的吗?

c++ casting

-3
推荐指数
1
解决办法
96
查看次数

Typedef reinterpret_cast

我在弄清楚语法时遇到了一些麻烦typedef reinterpret_cast.有人可以帮忙吗?

编辑:

我想做什么.我总是犹豫不决,因为人们似乎总是陷入其他所有事情,除了问题实际上是什么,因为你可以看到我的上一篇文章导致一大堆什么都没有.

我试图想出一种为指针分配mem地址0的方法.我使用它作为安全捕获等.我认为typedef cast会对此有所帮助.请不要建议我使用NULL.

谢谢

c++ casting typedef

-4
推荐指数
1
解决办法
893
查看次数

Java类转换抛出异常

我有一个超级A,B,C和D类扩展A.

class A {}
class B extends A {}
class C extends A {}
class D extends A {}
Run Code Online (Sandbox Code Playgroud)

然后我有一个这样的列表:

[B.class, C.class, D.class]
Run Code Online (Sandbox Code Playgroud)

我从列表中取一个随机项并实例化它:

Object obj = list.get(i).newInstance()
Run Code Online (Sandbox Code Playgroud)

现在我需要将obj传递给一个接受A类对象作为参数的方法.

A a = (A) obj 
Run Code Online (Sandbox Code Playgroud)

导致java抛出异常(InstantiationException或IllegalAccessException,我不知道因为android studio不会在catch中的断点处停止,并且任何尝试从catch中保存有关异常的信息都会导致null ...).

我理解为什么我不能动态指定新的对象类型然后将obj强制转换为该类型,但如果它们都共享公共父类型,我不明白为什么它不起作用.

任何人都可以告诉我如何解决我目前正在做的事情,或以类似的方式实现同​​样的事情?

谢谢!/最大

java android casting class

-4
推荐指数
1
解决办法
1088
查看次数

可以显示为字符串的类型

我需要收集C#中可以转换为字符串的所有类型.到目前为止我的清单包含:

  • 字节
  • 为sbyte
  • 烧焦
  • INT
  • UINT
  • ULONG
  • USHORT

请帮我收集更多.

c# types casting

-4
推荐指数
1
解决办法
50
查看次数

为什么sizeof()运算符在C中的输出不同

我在测试你的c中看到了以下例子(作者:Yashvant Kanetkar).

在下面的示例中,sizeof()给出输出8.

#include<stdio.h>

double d;

int main()
{
(int)(float)(char)d;
printf("%d\n",sizeof(d));
}
Run Code Online (Sandbox Code Playgroud)

但在第二个例子中,sizeof()给出了输出4.

#include<stdio.h>

double d;

int main()
{
printf("%d\n",sizeof((int)(float)(char)d));
}
Run Code Online (Sandbox Code Playgroud)

为什么两个输出不同?书中没有解释.

c casting sizeof

-4
推荐指数
1
解决办法
113
查看次数

从object转换为int会丢失精度C++

在我的TimeCode.h中,我有以下内容:

inline TimeCode::operator int() const;
Run Code Online (Sandbox Code Playgroud)

每当我将TimeCode对象转换为int时,应该能够执行哪个.

但当我做类似的事情时:

(int) firstTimeCode > (int) scndTimeCode
Run Code Online (Sandbox Code Playgroud)

编译器向我抛出以下错误:

cast from 'TimeCode*' to 'int' loses precision [-fpermissive]
Run Code Online (Sandbox Code Playgroud)

有谁知道问题是什么以及如何解决?非常感谢你提前!

c++ casting

-4
推荐指数
1
解决办法
260
查看次数

从任意投射到双快速

我有一个简单的问题,我有这本字典:

let dict: [String: Any] = ["Area": "100", "YessorNo" : true]
Run Code Online (Sandbox Code Playgroud)

而且,对于键area,我想将其值转换为double,如下所示:

let a = dict["Area"] as? Double
Run Code Online (Sandbox Code Playgroud)

当我打印时a,我得到一个nil,为什么?值100虽然是a string但不是数字?为什么我不能将其转换为double

dictionary casting swift

-4
推荐指数
1
解决办法
3669
查看次数

变量铸造性能

哪个代码执行得更快,原因何在

((Form)controls.Owner).Text = langfile.ReadString(
       FormName, ((Form)controls.Owner).Name, ((Form)controls.Owner).Text);
Run Code Online (Sandbox Code Playgroud)

要么

Form form = (Form)controls.Owner;
form.Text = langfile.ReadString(FormName, form.Name, form.Text);
Run Code Online (Sandbox Code Playgroud)

.net c# performance casting

-5
推荐指数
1
解决办法
113
查看次数

无法将类型'System.Collections.Generic.List <string []>'隐式转换为'string []'

下面是我的代码,它给出了一个错误: 无法将类型'System.Collections.Generic.List'隐式转换为'string []'

我试了好几次来解决这个错误.但我没有这样做.如果有任何身体可以建议什么可能是解决方案..谢谢:)

public GetContentResponse GetContent(abcServiceClient client, QueryPresentationElementIDsResponse querypresentationelementresponse)
        {
            GetContentRequest GetContentRequest = new GetContentRequest();
            GetContentResponse contentresponse = new GetContentResponse();
            querypresentationelementresponse = presentationElementId(client);
            List<string[]> chunks = new List<string[]>();
            for (int i = 0; i < querypresentationelementresponse.IDs.Length; i += 25)
            {
                chunks.Add(querypresentationelementresponse.IDs.Skip(i).Take(25).ToArray());
                contentresponse = client.GetContent(new GetContentRequest()
                {
                    IDs = chunks // here i get this error
                });
            }

            return contentresponse;
        }
Run Code Online (Sandbox Code Playgroud)

c# asp.net web-services casting

-5
推荐指数
1
解决办法
1万
查看次数

c ++为什么我不能使用static_cast将char*转换为int?

number = static_cast<int>(argv[1]);
Run Code Online (Sandbox Code Playgroud)

错误:使用static_cast从char*转换为int不允许.

我试过在谷歌上找出原因,我似乎无法找到它.另外,我不想得到ascii值,因为argv [1]是一个数字.

例如./prog 15

cout <<数字; //想要打印15

c++ int static casting

-5
推荐指数
1
解决办法
3703
查看次数

标签 统计

casting ×10

c++ ×4

c# ×3

.net ×1

android ×1

asp.net ×1

c ×1

class ×1

dictionary ×1

int ×1

java ×1

performance ×1

sizeof ×1

static ×1

swift ×1

typedef ×1

types ×1

web-services ×1