我知道在C++ 11中我们现在可以using用来写类型别名,比如typedefs:
typedef int MyInt;
Run Code Online (Sandbox Code Playgroud)
从我的理解,相当于:
using MyInt = int;
Run Code Online (Sandbox Code Playgroud)
这种新语法来自于努力表达" template typedef":
template< class T > using MyType = AnotherType< T, MyAllocatorType >;
Run Code Online (Sandbox Code Playgroud)
但是,对于前两个非模板示例,标准中是否还有其他细微差别?例如,typedefs以"弱"方式进行别名.也就是说,它不会创建新类型,而只会创建新名称(这些名称之间隐含的转换).
它是否与using生成新类型相同或是否生成新类型?有什么不同吗?
我是C编程的初学者,但我想知道typedef在定义结构时使用与使用结构之间有什么区别typedef.在我看来,实际上没有区别,他们实现了同样的目标.
struct myStruct{
int one;
int two;
};
Run Code Online (Sandbox Code Playgroud)
与
typedef struct{
int one;
int two;
}myStruct;
Run Code Online (Sandbox Code Playgroud) 我见过许多程序,包括如下所示的结构
typedef struct
{
int i;
char k;
} elem;
elem user;
Run Code Online (Sandbox Code Playgroud)
为什么经常这么需要?任何具体原因或适用范围?
我在现有代码库中有以下(工作)代码,用于C和C++之间共享的包含文件,在MSVC(2010)和Windows DDK上编译:
struct X {
USHORT x;
} typedef X, *PX;
Run Code Online (Sandbox Code Playgroud)
和:
enum MY_ENUM {
enum_item_1,
enum_item_2
} typedef MY_ENUM;
Run Code Online (Sandbox Code Playgroud)
据我所知,正确的定义应如下所示:
typedef struct {
USHORT x;
} X, *PX;
Run Code Online (Sandbox Code Playgroud)
有下面的表格是否有任何目的?我错过了什么吗?
我收到了一些C++代码,其中包含如下定义的各种结构:
typedef struct _someStruct_ {
std::string someString;
std::vector<std::string> someVectorOfStrings;
int someOtherStuff;
~_someStruct_()
{
someString.clear();
someVectorOfStrings.clear();
}
} someStruct;
Run Code Online (Sandbox Code Playgroud)
这里的析构函数是完全冗余的 - 如果结构是由默认的析构函数破坏的,那么任何字符串,向量等都不会被破坏吗?
如果我编写了代码,我就不会想到在这里添加一个显式的析构函数 - 我只是让编译器继续使用它.
据我所知,你可能需要在结构中创建自己的析构函数的唯一时间是结构的任何成员是否包含指向可能需要清理的数据的指针,或者是否有一些额外的功能(例如用于调试,记录时)需要一个结构被删除).
我在这里遗漏了什么 - 有没有理由在析构函数中明确清除字符串和向量?我怀疑发送给我的是一个C程序员(参见typedef),他试图将一些C代码转换成C++.
我想创建一个名为pos(从位置)的typedef结构来存储坐标x和y.我试图为这个结构重载一些运算符,但它不编译.
typedef struct {
int x;
int y;
inline pos operator=(pos a) {
x=a.x;
y=a.y;
return a;
}
inline pos operator+(pos a) {
return {a.x+x,a.y+y};
}
inline bool operator==(pos a) {
if (a.x==x && a.y== y)
return true;
else
return false;
}
} pos;
Run Code Online (Sandbox Code Playgroud)
我也想知道这个之间的区别:
inline bool operator==(pos a) {
if(a.x==x && a.y== y)
return true;
else
return false;
}
Run Code Online (Sandbox Code Playgroud)
还有这个:
bool operator==(pos a) const {
if(a.x==x && a.y== y)
return true;
else
return false;
}
Run Code Online (Sandbox Code Playgroud) 引自这里,
在C中,有两种不同的类型名称空间:struct/union/enum标记名称的名称空间和typedef名称的名称空间.
name.c
$ cat name.c
#include<stdio.h>
typedef long long long2;
int long2 () {
return 4;
}
int main() {
printf("hello, world!");
return 0;
}
$ gcc name.c -o name
name.c:4: error: 'long2' redeclared as different kind of symbol
name.c:3: error: previous declaration of 'long2' was here
$
Run Code Online (Sandbox Code Playgroud)
name2.c
$ cat name2.c
#include<stdio.h>
int four() {
return 4;
}
struct dummy {
int member;
};
int main() {
struct dummy four;
}
$ gcc name2.c -o name2 …Run Code Online (Sandbox Code Playgroud) 在C++中,可以创建一个结构:
struct MyStruct
{
...
}
Run Code Online (Sandbox Code Playgroud)
并且还可以执行以下操作:
typedef struct
{
...
} MyStruct;
Run Code Online (Sandbox Code Playgroud)
然而就我所知,两者之间没有可辨别的区别.哪个更好?如果没有差异,为什么两种方式都存在?在风格或可读性上,一个比另一个好吗?
#include <iostream>
int C;
class C {
private:
int i[2];
public:
static int f() {
return sizeof(C);
}
};
int f()
{
return sizeof(C); // why the C can't be the class type C.
}
int main()
{
std::cout << "C::f() = " <<C::f() << ","
<< " ::f() = " <<::f() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码返回: C::f() = 8, ::f() = 4
我的问题是为什么C全局函数中的标识符f解析为int具有名称C 的类型的对象,而不是类类型C?是否sizeof有特定的名称查找规则?
结论:根据我在/sf/answers/42873351/中的内容,通过函数/对象/枚举器名称隐藏类名是为了兼容C的问题.为了避免这种无意的隐藏,应该typedef是类强制编译错误.
在研究C中的队列时,我遇到了类似于下面的示例.为什么结构在大括号的开头和之后都命名?为什么在添加相同类型的项时,结构类型在结构内部再次使用?这些事情是多余的还是有意义的?
typedef void* vpoint_t;
typedef struct queue_item_t{
vpoint_t void_item;
struct queue_item_t* next;
} queue_item_t;
Run Code Online (Sandbox Code Playgroud)