CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: (void)var;

Hybrid View

  1. #1
    Join Date
    Aug 2009
    Posts
    2

    (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?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: (void)var;

    Most likely it's just trying to suppress an "unused parameter" compiler warning.

  3. #3
    Join Date
    Aug 2009
    Posts
    2

    Re: (void)var;

    ha, so its literally doing nothing? Would explain why I could not figure out what it was tring to accomplish.

  4. #4
    Join Date
    Feb 2009
    Posts
    326

    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
  •  





Click Here to Expand Forum to Full Width

Featured