473,503 Members | 11,968 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?

Sep 15 '05 #1
4 3701
ar*****@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
Sep 15 '05 #2
<ar*****@gmail.com> wrote in message
news:11**********************@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

Sep 15 '05 #3
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.

Sep 15 '05 #4
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
Sep 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3374
by: ralphNOSPAM | last post by:
Is there a PHP script that can find unused variables? I'd like to 'clean up' my scripts. Thanks...
15
2936
by: CR | last post by:
I've noticed that the trend these days is to declare variables in the middle of code instead of at the top. What is the advantage of this? It seems like it makes it hard to reuse variables. Here...
12
9589
by: zacks | last post by:
Suddenly, in a VB2005 project I am working on, several variables show up in the list of warnings are being unused local variables. But they all are. Several of them are the ex variable used in a...
9
3171
by: ThazKool | last post by:
I am just wondering if all the methods that are never called upon during a programs execution are removed when optimizing. For example you have: class FooA { void a(); ... void z(); }
5
6759
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
18
2609
by: MajorSetback | last post by:
I am using the Redhat version of Linux and GNU C++. It is not clear to me whether this is a Linux issue or a C++ issue. I do not have this problem running the same program on Windows but...
9
5480
by: werasm | last post by:
Hi all, I'm looking for a nice clean (portable) way to get rid of unused variable warnings without fiddling with compiler settings (on a per case basis). I've come up with this: template...
10
2742
by: Joris Dolderer | last post by:
Hello, I want to compile and link a program statically against the dietlibc. Of course, the dietlibc includes functions my program never needs. Is there any way to throw them out of the final...
8
1654
by: fabian.lim | last post by:
Hi, I have a question on constant variables. In the following code snippet, I have a function assign() that takes in an iterator to the private variable v, the number of stuff to assign (int n),...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5604
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.