Unused variables used for class constructors

Discussion in 'C++' started by artooro@gmail.com, Sep 15, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. 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
     
    John Harrison, Sep 15, 2005
    #2
    1. Advertising

  3. <> wrote in message
    news:...

    > 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
     
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Sep 15, 2005
    #3
  4. Guest

    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, 2005
    #4
  5. Jay Nabonne Guest

    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
     
    Jay Nabonne, Sep 15, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    6,805
    Chris Smith
    Nov 2, 2005
  2. Dave Rudolf
    Replies:
    12
    Views:
    8,308
    Martijn Lievaart
    Feb 6, 2004
  3. Jeremy Smith
    Replies:
    2
    Views:
    592
    Jeremy Smith
    Aug 3, 2006
  4. Rob Meade
    Replies:
    4
    Views:
    479
    Rob Meade
    Jul 6, 2007
  5. Peng Yu
    Replies:
    5
    Views:
    401
    Juha Nieminen
    Sep 19, 2008
Loading...

Share This Page