小编sun*_*t07的帖子

C和C++中的全局变量有什么区别?

我测试了以下代码:

在文件ac/a.cpp中

int a;
Run Code Online (Sandbox Code Playgroud)

在文件bc/b.cpp中

int a;
int main() { return 0; }
Run Code Online (Sandbox Code Playgroud)

当我使用gcc*.c -o test编译源文件时,它会成功.

但是当我使用g ++*.c -o test编译源文件时,它会失败:

ccIJdJPe.o:b.cpp:(.bss+0x0): multiple definition of 'a'
ccOSsV4n.o:a.cpp:(.bss+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

我真的很困惑.C和C++中的全局变量之间有什么区别吗?

c c++ gcc global-variables

27
推荐指数
2
解决办法
2291
查看次数

如何在C++中使用reinterpret_cast?

我知道C++中的reinterpret_cast可以这样使用:

float a = 0;
int b = *reinterpret_cast<int*>(&a);
Run Code Online (Sandbox Code Playgroud)

但是为什么不能直接施展呢?

float a = 0;
int b = reinterpret_cast<int>(a);

error: invalid cast from type 'float' to type 'int'
Run Code Online (Sandbox Code Playgroud)

c++

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

在模板中执行参数依赖名称查找时的奇怪行为

最近,我正在研究模板类中名称的众所周知的"两阶段名称查找"的确切含义.虽然我已经阅读了很多关于此的文章,但我仍然无法了解这一切.现在我对下面显示的代码感到困惑:

template<typename T>
class A
{
public:
    void f(T, T){};
};

namespace ns
{
    typedef int TT;
    void f(int, int){};
};

template<typename T>
class B : public A<T>
{
public:
    void g()
    {

        //f(T(), T()); // it's fine for error here
        typedef ns::TT TTT;
        f(TTT(), T()); // why this issued an error?
        f(ns::TT(), T()); // and this?
    }
};

/* I also think it's OK to move ns here */
// namespace ns
// {
//  typedef int TT;
// …
Run Code Online (Sandbox Code Playgroud)

c++ templates name-lookup

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

Windows中最流行的C++ gui框架是什么?

如今有很多Windows应用程序.几年前,MFC可能是最流行的创建Windows应用程序的gui框架.但我认为MFC太老了,没有一个好的OO设计.所以在这里我想知道在Windows中是否有任何现代的,设计良好且广泛使用的C++ gui框架?

c c++ windows user-interface

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