-
August 25th, 2009, 04:08 PM
#1
(void)var;
I have seen this syntax a few times, and im not exactly sure what it means
Code:
virtual OBJ:OBJThing( ...., unsigned int variable)
{
(void)variable;
....
}
what exactly is (void)variable doing?
-
August 25th, 2009, 04:09 PM
#2
Re: (void)var;
Most likely it's just trying to suppress an "unused parameter" compiler warning.
-
August 25th, 2009, 04:17 PM
#3
Re: (void)var;
ha, so its literally doing nothing? Would explain why I could not figure out what it was tring to accomplish.
-
August 26th, 2009, 12:35 PM
#4
Re: (void)var;
I sure didn't know that, was a learning for me. Thanks.
I have just given a small suggestion (may not be the answer to your question but an alternative).
I guess this is mainly used in virtual functions to maintain the spec. In non-virtual functions this wouldn't normally arise, as you can remove that entire parameter. Also could be used to catch exceptions and if the catch block is not going to use the catch parameter.
Alternative:
--------------
I just feel in such scenarios (virtual functions), if you want to retain the spec but not use the parameter at all, then you need not specify the parameter name. Also in catch blocks where you are not going to use the parameter, the parameter name need not be specified.
This shown with an example below (see the function f1 in ClassA and ClassB):
Code:
#include <iostream>
using std :: cout;
using std :: endl;
class ClassA
{
public:
virtual void f1(int pInt);
};
class ClassB : public ClassA
{
public:
void f1(int);
};
int main()
{
ClassA objA;
ClassB objB;
ClassA *ptrA1 = &objA;
ClassA *ptrA2 = &objB;
ptrA1 -> f1(5);
ptrA2 -> f1(5);
return(0);
}
void ClassA :: f1(int pInt)
{
cout << "ClassA :: f1(int) invoked\n"
<< "pInt = " << pInt << endl
<< endl;
};
void ClassB :: f1(int)
{
cout << "ClassB :: f1(int) invoked\n"
<< endl;
};
Last edited by Muthuveerappan; August 26th, 2009 at 10:41 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|