Unused variables used for class constructors

A

artooro

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

John Harrison

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

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
 
A

artooro

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.
 
J

Jay Nabonne

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top