Reputation:
I am learning C++ on my own from the internet. I was wondering if you can create a variable of the void
type. 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
Thanks :)
Upvotes: 2
Views: 2399
Reputation: 1578
An example of use of void* in modern C++, which is ok (says our local C++ guru):
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. When
sizeof(void*)==8
you know your code is compiled as 64bit (8bytes). And When
sizeof(void*)==4
you know your code is compiled as 32bit
Upvotes: 0
Reputation: 38939
void
is not valid for variable types other than void*
s in C++, never for an actual value type.
http://www.cplusplus.com has a good section on void*
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.
A void*
is typically used to reference values of an unknown type. Other information is used to determine the type, the void*
is cast to that type and only then can be dereferenced.
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.
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
Upvotes: 0
Reputation: 2893
You can use void pointers to achieve that. Check here:
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. It's a valid practice (in pre-C++11 standards) only for function pointers. Stay with hard typed pointers, because the compilers can perform optimizations on them.
Upvotes: 0
Reputation: 158559
i think my teacher told me it is possible to create a variable of void type . 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 expressions are very useful for templates when a function return different types, for example see the question: Returning a 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):
An object type is a (possibly cv-qualified) type that is not a function type, not a reference type, and not a void type.
and:
The void type has an empty set of values. The void type is an incomplete type that cannot be completed. 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). 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
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. Most likely for using C APIs, see when to use void* in c++:
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
Upvotes: 2
Reputation: 112386
Here's the deal (and folks, this is a legit beginner question, jaysus, lay off the guy.)
The void
type in C and C++ is a tag or label for "thing of no type at all." Now, when we talk about the "type" of something, we're really saying two things:
So, for example, when we declare something like int x;
we're saying that x
sizeof(int)
× 8 bits of memory*
and +
.Now, an object of no type would have neither one, so the compiler tells you the size is unknown or 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;
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.
When we declare something void * vp;
, we're saying "this is the address of something, type of that something to be determined later."
Upvotes: 2
Reputation: 1
"I was wondering if you can create a variable of the void type."
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.
"so wat is a void pointer used for?"
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.
Upvotes: 5