小编Sid*_*ang的帖子

将void指针转换为特定类型指针时,哪个转换符号更好,static_cast或reinterpret_cast?

可能重复:
在将void*转换为任何内容时,我应该使用static_cast还是reinterpret_cast

在这个程序中,我有一个void *参数,并希望将其转换为特定类型.但我不知道使用哪种"铸造符号".无论是static_cast还是reinterpret_cast工作.哪一个更好?标准C++推荐哪一个?

typedef struct
{
    int a;
}A, *PA;

int foo(void* a)                // the real type of a is A*
{
    A* pA = static_cast<A*>(a); // or A* pA = reinterpret_cast<A*>(a);?
    return pA->a;
}
Run Code Online (Sandbox Code Playgroud)

这是

A* pA = static_cast<A*>(a);
Run Code Online (Sandbox Code Playgroud)

要么

A* pA = reinterpret_cast<A*>(a);
Run Code Online (Sandbox Code Playgroud)

更合适吗?

c++ casting

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

关于"int const*p"和"const int*p"

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    int i1 = 0;
    int i2 = 10;

    const int *p = &i1;
    int const *p2 = &i1;
    const int const *p3 = &i1;

    p = &i2;
    p2 = &i2;
    p3 = &i2;

    cout << *p << endl
        << *p2 <<endl
        << *p3 <<endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

可以使用VC6.0和VC2010编译代码.但我有一个问题:

const int*p =&i1;

这意味着什么"p"点不能修改,但p不能修改,我是对的吗?所以

p =&i2;

这条线可以遵守,是吗?

这一行:

int const *p2 = &i1;
Run Code Online (Sandbox Code Playgroud)

在我看来,这意味着p2无法修改,而p2点可以改变,我是对的吗?为什么

p2 =&i2;

可以编译?

关于这一行:

const int const*p3 …

c++ const

8
推荐指数
2
解决办法
1万
查看次数

如何查看 ocx 文件的接口?

我想查看“.ocx”文件的接口。像这样:

在此输入图像描述

但对于某些 .ocx,我只能看到 5 个函数,如下所示:

在此输入图像描述

问题是:如何才能看到这些ocx文件的接口。我已经尝试过这个:

A.a
) 我想注册它并在 Visual Studio 中查看它。但是当我注册它时,出现错误“LoadLibrary(path:\filename.ocx) failed”。像这样:

在此输入图像描述

b) 然后我用“Dependency Walker”打开ocx,发现文件依赖没有DLL文件。 在此输入图像描述

c) 如何注册?

B. 我使用“Dll Export Viewer”,现在我可以看到函数的名称,但仍然无法获取函数的参数。如何获取函数的参数?

windows com ocx visual-c++

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

如何使用wstring和wcout在Xcode中输出中文单词?

我尝试在Xcode 4.2中运行代码:

int main(int argc, const char * argv[])
{
    locale loc("chs");
    locale::global(loc);

    wstring text(L"??");
    wcout << text << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到错误"Thread 1:signal SIGABRT".

你能告诉我为什么会发生错误,或者如何使用wstringwcout输出中文单词?

c++ macos xcode

2
推荐指数
1
解决办法
1698
查看次数

返回指向矢量数据的指针的最佳方法是什么?const还是非const?

#include <vector>

using std::vector;

class A
{

public:
    A()
    {
        buf.push_back(65);
        buf.push_back(66);
        buf.push_back(67);
    }

    ~A(){}

    const char *  getA() const
    {
        // why never run here?
        return &buf[0];
    }

    const char * getA()
    {
        return &buf[0];
    }

    char *  getB() const
    {
        // why compile error?
        return &buf[0];
    }

    char * getB()
    {
        return &buf[0];
    }

private:
    vector<char> buf;
};

int main()
{
    A a;
    const char * pc = a.getA();
    const char * const cp = a.getA();
    char * …
Run Code Online (Sandbox Code Playgroud)

c++ stl visual-studio-2010

1
推荐指数
1
解决办法
212
查看次数

标签 统计

c++ ×4

casting ×1

com ×1

const ×1

macos ×1

ocx ×1

stl ×1

visual-c++ ×1

visual-studio-2010 ×1

windows ×1

xcode ×1