Incrementing using ....

Discussion in 'C++' started by 2005, Oct 26, 2006.

  1. 2005

    2005 Guest

    Hi

    I have a "private" member "mSize" & "public" function Kite( ).

    I want to increment it by 1 everytime it is in the Kite( ).

    It appears that the increment disappears as it re-enters Kite( ) the
    second time.

    So the most I had was mSize =1 (the class initializes to mSize =0).

    Is this expected?

    Is there a way I can do this?

    Thank you
    2005, Oct 26, 2006
    #1
    1. Advertising

  2. 2005

    Gavin Deane Guest

    2005 wrote:
    > Hi
    >
    > I have a "private" member "mSize" & "public" function Kite( ).
    >
    > I want to increment it by 1 everytime it is in the Kite( ).
    >
    > It appears that the increment disappears as it re-enters Kite( ) the
    > second time.
    >
    > So the most I had was mSize =1 (the class initializes to mSize =0).
    >
    > Is this expected?


    Not by you, it would seem. Nobody else has any expectations one way or
    the other since nobody else can see your code.

    > Is there a way I can do this?


    I expect so.

    The FAQ tells you how to post questions about problem code.
    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.8

    Follow *all* those guidelines and you will get the help you want.

    Gavin Deane
    Gavin Deane, Oct 26, 2006
    #2
    1. Advertising

  3. 2005 wrote:

    > It appears that the increment disappears as it re-enters Kite( ) the
    > second time.


    Yes, it disappeared. I can't see it.

    --
    Salu2
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Oct 26, 2006
    #3
  4. 2005

    Salt_Peter Guest

    2005 wrote:
    > Hi
    >
    > I have a "private" member "mSize" & "public" function Kite( ).
    >
    > I want to increment it by 1 everytime it is in the Kite( ).
    >
    > It appears that the increment disappears as it re-enters Kite( ) the
    > second time.
    >
    > So the most I had was mSize =1 (the class initializes to mSize =0).
    >
    > Is this expected?
    >
    > Thank you


    Thats, nice. Share the code or a reconstruction that is compileable.
    Otherwise, you are out of luck.
    Salt_Peter, Oct 26, 2006
    #4
  5. 2005

    Guest

    What you could do is make the mSize variable static so that only one
    instance of it is created. Inside of the function Kite increment the
    value of mSize, this way multiple objects of your class will share the
    same mSize variable.

    On Oct 26, 5:34 pm, "Salt_Peter" <> wrote:
    > 2005 wrote:
    > > Hi

    >
    > > I have a "private" member "mSize" & "public" function Kite( ).

    >
    > > I want to increment it by 1 everytime it is in the Kite( ).

    >
    > > It appears that the increment disappears as it re-enters Kite( ) the
    > > second time.

    >
    > > So the most I had was mSize =1 (the class initializes to mSize =0).

    >
    > > Is this expected?

    >
    > > Thank youThats, nice. Share the code or a reconstruction that is compileable.

    > Otherwise, you are out of luck.
    , Oct 27, 2006
    #5
  6. 2005

    Daniel T. Guest

    "2005" <> wrote:

    > I have a "private" member "mSize" & "public" function Kite( ).
    >
    > I want to increment it by 1 everytime it is in the Kite( ).
    >
    > It appears that the increment disappears as it re-enters Kite( ) the
    > second time.
    >
    > So the most I had was mSize =1 (the class initializes to mSize =0).
    >
    > Is this expected?


    Your code does exactly what it you wrote it to do, but it doesn't do
    what you want it to do. Therefore, you need to write it to do what you
    want, then when it does what you wrote, it will do what you expect.

    > Is there a way I can do this?


    Yes.

    I appreciate that you are not simply posting your homework assignment
    and asking someone to do it. That really is a good trait. At the same
    time, we can't help you if you give us too little information.

    Check out the code below, really study it and tell me if it does what
    you want done.

    class MyClass {
    int mSize;
    public:
    MyClass():mSize(0) { }
    int size() const { return mSize; }
    void Kite() { ++mSize; }
    };

    int main() {
    MyClass object;
    assert( object.size() == 0 );
    object.Kite();
    assert( object.size() == 1 );
    object.Kite();
    assert( object.size() == 2 );
    }

    --
    To send me email, put "sheltie" in the subject.
    Daniel T., Oct 27, 2006
    #6
  7. 2005

    Salt_Peter Guest

    [please don't top post] reorganized...
    >
    > On Oct 26, 5:34 pm, "Salt_Peter" <> wrote:
    > > 2005 wrote:
    > > > Hi

    > >
    > > > I have a "private" member "mSize" & "public" function Kite( ).

    > >
    > > > I want to increment it by 1 everytime it is in the Kite( ).

    > >
    > > > It appears that the increment disappears as it re-enters Kite( ) the
    > > > second time.

    > >
    > > > So the most I had was mSize =1 (the class initializes to mSize =0).

    > >
    > > > Is this expected?

    > >
    > > > Thank youThats, nice. Share the code or a reconstruction that is compileable.

    > > Otherwise, you are out of luck.


    wrote:
    > What you could do is make the mSize variable static so that only one
    > instance of it is created. Inside of the function Kite increment the
    > value of mSize, this way multiple objects of your class will share the
    > same mSize variable.


    Thats not neccessarily the right strategy since then *all* the objects
    have the same static "mSize". We have no knowledge about what Kite() or
    mSize are attempting to solve since we can't see the problem.
    Salt_Peter, Oct 27, 2006
    #7
    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. Josh Graham
    Replies:
    3
    Views:
    23,143
    Nicolas Matringe
    Apr 7, 2004
  2. Salman

    Incrementing value test

    Salman, Apr 13, 2005, in forum: VHDL
    Replies:
    2
    Views:
    611
    Engineering Guy
    Apr 13, 2005
  3. Mothra
    Replies:
    3
    Views:
    420
  4. Ron
    Replies:
    0
    Views:
    301
  5. =?Utf-8?B?bWVraW0=?=

    Incrementing Assembly Version?

    =?Utf-8?B?bWVraW0=?=, Oct 28, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    7,283
    Saravana
    Oct 29, 2004
Loading...

Share This Page