Incrementing using ....

2

2005

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
 
G

Gavin Deane

2005 said:
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
 
S

Salt_Peter

2005 said:
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.
 
J

JonathanK.Kelly

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

Daniel T.

2005 said:
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 );
}
 
S

Salt_Peter

[please don't top post] reorganized...
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.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top