Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Unused variables used for class constructors

Reply
Thread Tools

Unused variables used for class constructors

 
 
artooro@gmail.com
Guest
Posts: n/a
 
      09-15-2005
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?

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-15-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
=?iso-8859-1?Q?Ali_=C7ehreli?=
Guest
Posts: n/a
 
      09-15-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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

 
Reply With Quote
 
artooro@gmail.com
Guest
Posts: n/a
 
      09-15-2005
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.

 
Reply With Quote
 
Jay Nabonne
Guest
Posts: n/a
 
      09-15-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is the possible to have all the public constructors of the publicbase class as the constructors of a derived class? Peng Yu C++ 5 09-19-2008 10:19 AM
.net 2.0 - unused class variables / methods Rob Meade ASP .Net 4 07-06-2007 01:00 PM
Copy constructors, de/constructors and reference counts Jeremy Smith C++ 2 08-02-2006 11:25 PM
Eclipse - plugin to find unused CLASS MEMBER VARIABLES? steve.chambers@gmail.com Java 5 11-02-2005 03:33 PM
Constructors that call other Constructors Dave Rudolf C++ 12 02-06-2004 03:26 PM



Advertisments