简体   繁体   English

C ++:使用void创建变量

[英]C++ : Using void to create variables

I am learning C++ on my own from the internet. 我正在从互联网上学习C ++。 I was wondering if you can create a variable of the void type. 我想知道你是否可以创建一个void类型的变量。 If so , how can you? 如果是这样,你怎么样? Also what will be these variables used for? 这些变量还会用于什么?

This does not work: 这不起作用:

void b;
cout<<b;

Error: 错误:

Size of b is unknown or zero b的大小未知或为零

Thanks :) 谢谢 :)

"I was wondering if you can create a variable of the void type." “我想知道你是否可以创建一个void类型的变量。”

No, the compiler already told you. 不,编译器已经告诉过你了。

If so , how can you?" 如果是的话,你怎么样?“

See above. 往上看。

"Also what will be these variables used for?" “这些变量还会用于什么?”

It won't be useful, because void explicitly designates no type . 没用 ,因为void明确指定没有类型


"so wat is a void pointer used for?" “所以wat是一个用于的空指针?”

As for your comment: 至于你的评论:

It's used to store the address of an object of any type. 它用于存储任何类型对象的地址。 Unless you don't know the exact original type, it's pretty useless as well. 除非你不知道确切的原始类型,否则它也是无用的。

i think my teacher told me it is possible to create a variable of void type . 我想我的老师告诉我可以创建一个void类型的变量。 He told he will teach us later 他告诉他以后会教我们

We can not have void objects, most likely what your teacher meant was that you can have a void expression or a void pointer. 我们不能拥有虚空对象,很可能是老师的意思是你可以拥有一个void表达式或一个void指针。 void expressions are very useful for templates when a function return different types, for example see the question: Returning a void? 当函数返回不同类型时,void表达式对于模板非常有用,例如,请参阅问题: 返回void? which provides the following code: 它提供以下代码:

template <class T>
struct Test
{
    static constexpr T f() {return T();} 
};

int main()
{
    Test<void> test;
    test.f(); // Why not an error?
    return 0;
}

For reference the draft C++ standard says the following ( emphasis mine ): 作为参考,C ++标准草案说明如下( 强调我的 ):

An object type is a (possibly cv-qualified) type that is not a function type, not a reference type, and not a void type. 对象类型是(可能是cv限定的)类型,它不是函数类型,不是引用类型, 也不是void类型。

and: 和:

The void type has an empty set of values. void类型具有一组空值。 The void type is an incomplete type that cannot be completed . void类型是不完整的类型,无法完成 It is used as the return type for functions that do not return a value. 它用作不返回值的函数的返回类型。 Any expression can be explicitly converted to type cv void (5.4). 任何表达式都可以显式转换为cv void(5.4)类型。 An expression of type void shall be used only as an expression statement (6.2), as an operand of a comma expression (5.18), as a second or third operand of ?: (5.16), as the operand of typeid or decltype, as the expression in a return statement (6.6.3) for a function with the return type void, or as the operand of an explicit conversion to type cv void void类型表达式只能用作表达式语句(6.2),作为逗号表达式(5.18)的操作数,作为?:( 5.16)的第二个或第三个操作数,作为typeid或decltype的操作数,如返回类型为void的函数的返回语句(6.6.3)中的表达式,或者作为类型为cv的显式转换的操作数

The other possibility is your teacher was referring to void pointers, which is used when you need a pointer that can point to any type. 另一种可能性是你的老师指的是void指针,当你需要一个可以指向任何类型的指针时使用它。 Most likely for using C APIs, see when to use void* in c++ : 最有可能使用C API,看看何时在c ++中使用void *

Compound types can be constructed in the following ways: 化合物类型可以通过以下方式构建:

and: 和:

pointers to void or objects or functions (including static members of classes) of a given type 指向void或指定类型的对象或函数(包括类的静态成员)的指针

Here's the deal (and folks, this is a legit beginner question, jaysus, lay off the guy.) 这是交易(和伙计们,这是一个合法的初学者问题,jaysus,裁掉那个人。)

The void type in C and C++ is a tag or label for "thing of no type at all." C和C ++中的void类型是“根本没有类型的东西”的标签或标签。 Now, when we talk about the "type" of something, we're really saying two things: 现在,当我们谈论某事物的“类型”时,我们真的说了两件事:

  • how much memory does it occupy? 它占用了多少内存?
  • what operations can we perform on that thing? 我们可以对那件事做什么操作?

So, for example, when we declare something like int x; 所以,例如,当我们声明像int x;这样的东西时int x; we're saying that x 我们说x

  • is sizeof(int) × 8 bits of memory sizeof(int) ×8位内存
  • has arithmetic operations like * and + . 有像*+这样的算术运算。

Now, an object of no type would have neither one, so the compiler tells you the size is unknown or 0. 现在,一个没有类型的对象都没有,所以编译器告诉你大小是未知的或0。

But what is useful is to have an address that doesn't have a type associated with it. 但是,什么有用的是有没有与之相关的类型的地址 When you declare something as int * xp; 当你将某些东西声明为int * xp; you're saying thet xp is the address of something that we agree to treat as an int -- but it's just an agreement because we can, eg with a typecast, change or minds later. 你说泰德xp是的,我们同意把为东西地址int -但它只是一个协议,因为我们可以,例如,与后来的类型转换,改变或心灵。

When we declare something void * vp; 当我们声明一些void * vp; , we're saying "this is the address of something, type of that something to be determined later." ,我们说“这是某种东西的地址,这种东西的类型将在以后确定。”

You can use void pointers to achieve that. 您可以使用void指针来实现这一点。 Check here: 点击这里:

http://www.learncpp.com/cpp-tutorial/613-void-pointers/ http://www.learncpp.com/cpp-tutorial/613-void-pointers/

int nValue;
float fValue;

struct Something
{
    int nValue;
    float fValue;
};

Something sValue;

void *pVoid;
pVoid = &nValue; // valid
pVoid = &fValue; // valid
pVoid = &sValue; // valid

Using void pointers usually is a bad practice . 使用void指针通常是一种不好的做法 It's a valid practice (in pre-C++11 standards) only for function pointers. 这是一种有效的实践(在C ++ 11之前的标准中),仅适用于函数指针。 Stay with hard typed pointers, because the compilers can perform optimizations on them. 保持硬类型指针,因为编译器可以对它们执行优化。

void is not valid for variable types other than void* s in C++, never for an actual value type. void对于C ++中的void* s以外的变量类型void* ,永远不会对实际值类型有效。

http://www.cplusplus.com has a good section on void* s: http://www.cplusplus.com/doc/tutorial/pointers/#void http://www.cplusplus.comvoid* s上有一个很好的部分: http//www.cplusplus.com/doc/tutorial/pointers/#void

Note that the lines: 注意这些行:

The data pointed by them cannot be directly dereferenced (which is logical, since we have no type to dereference to), and for that reason, any address in a void pointer needs to be transformed into some other pointer type that points to a concrete data type before being dereferenced. 它们指向的数据不能直接解引用(这是合乎逻辑的,因为我们没有要取消引用的类型),因此,void指针中的任何地址都需要转换为指向具体数据的其他指针类型在被解除引用之前输入。

A void* is typically used to reference values of an unknown type. void*通常用于引用未知类型的值。 Other information is used to determine the type, the void* is cast to that type and only then can be dereferenced. 其他信息用于确定类型, void*被强制转换为该类型,然后才能解除引用。

This behavior was very common in C, however with the advent of C++ it is vastly preferable to use templates to determine the value type of anything that is known at compile-time, void* s are used only in the special case that a value type must be determined at run-time. 这种行为在C中非常常见,但是随着C ++的出现,使用模板来确定编译时已知的任何值的类型是非常可取的, void* s仅在特殊情况下使用值类型必须在运行时确定。


I've been learning about aliasing a lot lately, and it may be further evidence of void* 's fall from grace that it is not listed as a supported type while char* and unsigned char* are: http://en.cppreference.com/w/cpp/language/reinterpret_cast#Type_aliasing 我最近一直在学习很多别名,而且它可能进一步证明了void*从恩典中堕落,它没有被列为受支持的类型,而char*unsigned char*是: http://en.cppreference .COM / W / CPP /语言/ reinterpret_cast的#Type_aliasing

An example of use of void* in modern C++, which is ok (says our local C++ guru): 在现代C ++中使用void *的一个例子,这是好的(我们的本地C ++专家说):

When creating portable code you sometimes want to know if your code was compiled as 32bit or 64bit, one way of determining this is to check the size of a void* as it represents the size of an address in bytes. 在创建可移植代码时,您有时想知道您的代码是编译为32位还是64位,确定这一点的一种方法是检查void *的大小,因为它表示以字节为单位的地址大小。 When 什么时候

sizeof(void*)==8

you know your code is compiled as 64bit (8bytes). 你知道你的代码编译为64位(8字节)。 And When 什么时候

sizeof(void*)==4

you know your code is compiled as 32bit 你知道你的代码编译为32位

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM