English 中文(简体)
联合国文件都用英文大写字母附加数字编号。
原标题:Symbol clashing of inner and outer structs, C++ vs C
  • 时间:2012-05-17 01:02:07
  •  标签:
  • c++
  • c

So, say I have the following C++ header, testheader.h:

struct mystruct
{
  struct myinnerstruct
  {
        int x;
  } astruct;
};

struct myinnerstruct
{
    int x;
};

和以下C++来源,test.cpp:

#include "testheader.h"
using namespace std;
int main()
{
    return 0;
}

g ++在汇编/链接中没有问题。

现在,如果我有同样的主人,而不是C++来源,则一份C源文件test.c:

#include "testheader.h"
int main()
{
  return 0;
}

我用快乐汇编,我有以下错误:

error: redefinition of struct myinnerstruct

因此,我认为,C版的范围是翻译单位,C++版本的范围是有限的? 谁能证实这种情况,也许会给我一个理由,说明它为何有意义? 我做一些C和C++编码的混合,这给我带来了很大的麻烦。

Any insight is greatly appreciated. Thanks!

最佳回答

在C区,被nes住的建筑实际上并不在其父母的范围之内。 然而,在C++中,它们的确是teststruct:innerstruct 是不同的类别,可到innerstruct。 这样做是为了改进C++编码的资本。 如果没有这一规则,同一名称空间中没有任何两种类型可以界定一个密封的<编码>iterator类别,例如,这种类别将极为坏。

C treats structs in plenty of other very silly ways, so it s no surprise that they did the wrong thing here. However, C does not otherwise allow type scoping and having a sane rule here would have introduced many additional concepts to the language- ultimately, it would have required the introduction of namespaces, which for some reason was never done.

问题回答

在C,内障的定义超出了外部结构,C++的情况并非如此。

This has been explained in this blog: Incompatibilities Between ISO C and ISO C++ among many other things in C vs. C++ with a reference to C99 and c++98 ISO standards.

There are no scopes in C, so everything is in the "global" namespace (in C++ speak)

在C和C++上工作的更清洁的方法是:

struct myinnerstruct
{
    int x;
};

struct mystruct
{
  struct myinnerstruct astruct;
};




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签