This question already has answers here:
Is there a difference between foo(void) and foo() in C++ or C?
(4 answers)
Closed 8 years ago.
I have noticed that when I create a new C++ class in Visual Studio, it writes down Foo(void) and ~Foo(void) as constructor and destructor templates instead of Foo() and ~Foo(). However, both versions seem to work. Is there a difference between both or does it not matter at all which version I use?
In C++, both are technically the same, as in "a function that takes no arguments"
For more info:
In C, however, Foo(void) is "a function that takes no arguments", but Foo() is "a function that takes unknown amount of arguments." These arguments are of an unspecified type.
Foo(void) is used across both languages with the same meaning.
Both are equivalent, so this is merely a choice of style. I prefer Foo() because it involves less typing and it's what I've seen more often.
This question has actually been covered before on SO.
Apparently foo() in C means "a function foo taking an unspecified number of arguments of unspecified type" where in C++ it means "a function foo taking no arguments."
Only when the constructor and destructor do not need any arguments, there are the same. Also Foo(void) seems less frequently used.
Related
This question already has answers here:
Is f(void) deprecated in modern C and C++? [duplicate]
(6 answers)
Closed 6 years ago.
I have seen in C++ program, during function declaration if there is no parameter for the function void is declared as parameter like this:
int F1(void)
How is it different than:
int F1()
There is no difference. Using void is just a more explicit way to declare the same thing. Personally, I never use that syntax and rarely see anyone else use it either.
It's the same thing in C++, and is a holdover from C.
Here's an excerpt from the C++ 2003 standard (C.1.6):
Change: In C++, a function declared with an empty parameter list takes no arguments.
In C, an empty parameter list means that the number and type of the function arguments are unknown"
Example:
int f(); // means int f(void) in C++
// intf(unknown) in C
Rationale: This is to avoid erroneous function calls (i.e. function calls with the wrong number or type of arguments).
Effect on original feature: Change to semantics of well-defined feature. This feature was marked as “obsolescent” in C.
Both of them are exactly the same, leaving the argument field empty as () is the one I prefer, some prefer writing (void) just so that someone editing the code may be ensured that no arguments are required. Makes no difference though, just a readability thing.
This question already has answers here:
Is there a difference between copy initialization and direct initialization?
(9 answers)
Closed 7 years ago.
Why C++ gives two ways to initialize variable?
First way is C-type initialization where we assign value to the variable at the place where we define it.
int a = 0;
Another way, constructor initialization which is done by enclosing the initial value between parentheses ().
int a(0);
My question is what was reason that the creators of C++ were forced to introduce new way to initialize variable. Although C-style initialization was doing the job.
int a = 0; exists for legacy (and because it feels natural, especially for built-in types), and int a(0) exists for explicitness and consistency - there are situations where you may want a more complicated copy constructor which takes multiple arguments or arguments of other types (conversion constructors).
If it can (ie. if the appropriate constructor is available), the compiler will treat both int a = 0; and int a(0) as a call to the copy constructor. The precise behavior is explained here.
I think this is because constructors with initializer lists are generally faster, which I think has to do with the the fact that the value can be placed into the newly allocated variable memory in fewer memory accessing operations. Here is a CPP FAQ on that topic (a great website for questions like this, btw).
Basically implicit is the preferred way:
int nValue = 5; // explicit initialization
int nValue(5); // implicit initialization
Here are some reads:
http://www.learncpp.com/cpp-tutorial/21-basic-addressing-and-variable-declaration/
Explicit Assignment vs Implicit Assignment
When you use the first, what you call C-type initialization, the variable will be written to twice, first the default value and second the assignment, as for the second case it will directly be set to the specified value. I think modern compilers optimize this for primitive types but for objects it could make quite a difference.
This is code compiled with GCC(with -std=c99) and G++(with -std=c++98):
void fun()
{}
int main()
{
fun(1,2,3);
return 0;
}
GCC compiled the code successfully,but the G++ came up with this:
error: too many arguments to function ‘int fun()’
I am totally confused.
I knew that the C++ should be compatible with C by design,but this case shows me incompatibility.This case happens by design?
In C an empty parameter list means that you don't specify how many arguments the function takes. To define a function with 0 parameters, you'd use (void) as the parameter list. In C++ an empty parameter list means the function takes 0 parameters, so yes, C and C++ are not compatible in this instance.
I knew that the C++ should be compatible with C by design,but this case shows me incompatibility.This case happens by design?
While C++ is compatible to C in many instances, this is not the case all the time. Other examples are implicit casts from void* (allowed in C, but not in C++) and keywords (it's perfectly valid to use something like class as a variable name in C, but obviously not in C++ where it is a keyword). And yes, that's by design.
In c++
void fun();
means a function taking no arguments.
To communicate that to C write
void fun(void); // also works in c++ but it's frowned upon
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Question about a function definition (three dots in parameters..)
I'm new to c++, what is the meaning of the ... (triple dot) inside the method or function
sample
void strArr::set(const char *value, ...) {
// some code here
}
It's a way to express a variable number of arguments, used to be called va_args or variadic function.
Take a look here to learn about them in C++, they do exist in other programming languages though, since it's just a generic concept.
That's a variadic function, it can take any number of parameters (more than 1 in this case).
Same principle as printf and others:
printf("%d", 1);
printf("%d%d%d", 1,1,1);
printf("%d%d%d%d%d%d", 1,1,1,1,1,1);
These aren't overloads, but the same function taking a variable number of arguments.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How come pointer to a function be called without dereferencing?
How does dereferencing of a function pointer happen?
Supposing I have a function pointer like:
void fun() { /* ... */ };
typedef void (* func_t)();
func_t fp = fun;
Then I can invoke it by:
fp();
or
(*fp)();
What is the difference/
Precisely two parentheses and an asterisk.
Both call the function pointed to by fun, and both do so in the same manner.
However, visually, (*fun) makes it clear that fun is not a function in and of itself, and the dereference operator is a visual cue that it is a pointer of some kind.
The without-parentheses syntax, fun(), is the same as a regular function call and so visually equates to that, making it primarily clear you're calling some kind of function. It takes context or a lookup to notice that it is a function pointer.
This is just a style difference, as far as what happens.