Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Unused variables used for class constructors (http://www.velocityreviews.com/forums/t448432-unused-variables-used-for-class-constructors.html)

artooro@gmail.com 09-15-2005 09:36 PM

Unused variables used for class constructors
 
Hi,

I'm using some lines of code like this:
ClassName *var = new ClassName(...);

and that's it. The class constructor does everything I want (there are
methods to do more but I don't need them)
But this generates a warning (gcc4) saying it's an unused variable.
Is there a better way to do this?


John Harrison 09-15-2005 09:45 PM

Re: Unused variables used for class constructors
 
artooro@gmail.com wrote:
> Hi,
>
> I'm using some lines of code like this:
> ClassName *var = new ClassName(...);
>
> and that's it. The class constructor does everything I want (there are
> methods to do more but I don't need them)
> But this generates a warning (gcc4) saying it's an unused variable.
> Is there a better way to do this?
>


Well a better way is

ClassName var(...);

Why use new when you don't have to? Whether that gets rid of your
warning or not is another matter. Compiler warnings are often dubious.

john

=?iso-8859-1?Q?Ali_=C7ehreli?= 09-15-2005 09:48 PM

Re: Unused variables used for class constructors
 
<artooro@gmail.com> wrote in message
news:1126820173.261337.218570@f14g2000cwb.googlegr oups.com...

> I'm using some lines of code like this:
> ClassName *var = new ClassName(...);
>
> and that's it. The class constructor does everything I want (there are
> methods to do more but I don't need them)
> But this generates a warning (gcc4) saying it's an unused variable.
> Is there a better way to do this?


Even though g++ specific issues are off-topic here, I can come up with two
solutions:

int g_i = 0;

class ClassName
{
public:

ClassName(int i)
{
g_i += i; // everything I need
}
};

#include <iostream>

void do_it(ClassName const &)
{}

int main()
{
// one way
do_it(ClassName(1));

// another way
ClassName var(1) __attribute__((unused));
}

Ali


artooro@gmail.com 09-15-2005 10:10 PM

Re: Unused variables used for class constructors
 
Well your suggestions which I have tried before but forgot about don't
work. It seems this class only work using this method. (wxWidgets
stuff)

No big problem though.


Jay Nabonne 09-15-2005 10:52 PM

Re: Unused variables used for class constructors
 
On Thu, 15 Sep 2005 14:36:13 -0700, artooro wrote:

> Hi,
>
> I'm using some lines of code like this:
> ClassName *var = new ClassName(...);
>
> and that's it. The class constructor does everything I want (there are
> methods to do more but I don't need them)
> But this generates a warning (gcc4) saying it's an unused variable.
> Is there a better way to do this?


If you don't need the var, then don't use it:

new ClassName(...);

I'm assuming there's some reason you don't need to delete it (like it
deletes itself).

- Jay


All times are GMT. The time now is 10:44 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.