class definition forgets values

R

rene

Hi all,
please look into this small piece of code and tell me why my values
weren't stored. Ive searched for hours on an explanation, but found
none.
Ive got an class with (in this example) 2 ints and want to set an read
them
out of another implementation. BUT i always get zeros back.
Ive also tried that with pointer (eg mycur=new curve();) but same
result.

Thanks in advance for any reply
Rene

class definition:

class curve
{
public:
curve()
{
}
int ckeytime;
int ckeyspeed;

void setCurvekeyTime (long t)
{
ckeytime=t;
}

void setCurvekeySpeed (long s)
{
ckeyspeed=s;
}

int CurvekeyTime() {
return ckeytime;
}
int CurvekeySpeed() {
return ckeyspeed;
}
};

implementation other class:
public: curve mycur;

one void:
mycur.setCurvekeyTime(11);
mycur.setCurvekeySpeed(12);

other void:
cout << mycur.CurvekeyTime() << endl;
cout << mycur.CurvekeySpeed() << endl;

results in:
0
0
 
I

Ian McCulloch

rene said:
Hi all,
please look into this small piece of code and tell me why my values
weren't stored. Ive searched for hours on an explanation, but found
none.
Ive got an class with (in this example) 2 ints and want to set an read
them
out of another implementation. BUT i always get zeros back.
Ive also tried that with pointer (eg mycur=new curve();) but same
result.

Can you post a minimal, COMPLETE & COMPILABLE section of code that
demonstrates the problem?

Ian
 
R

rene

Hi Ian,
no I couldn't.
The whole code is 16000 lines long.
If I write a little example code. Everythis works fine.
Yes, could be a bug in the compiler, but Ivr tried several gccs and
iccs.
Same result
 
S

shez

rene said:
implementation other class:
public: curve mycur;

one void:
mycur.setCurvekeyTime(11);
mycur.setCurvekeySpeed(12);

other void:
cout << mycur.CurvekeyTime() << endl;
cout << mycur.CurvekeySpeed() << endl;

results in:
0
0

Are you sure you're calling the first "void"? Print something there to
make sure your program actually calls the set functions.

BTW, we have a name for these "voids"... they're called "functions" (or
"methods") ;)

-shez-
 
R

rene

Are you sure you're calling the first "void"?
Yes, if sourrounded the code with debugging stuff.

the FUNCTION (<- you know)
mycur.setCurvekeyTime(11);
is OK.
debugging cout in the curve class tells me, that 11 is assigned to t
and ckeytime.
But calling mycur.CurvekeyTime() holds the value 0.
So something went wrong with the return value.
If I changed the code to:
int CurvekeyTime() {
//return ckeytime;
return 123;
}
I get the 123.

BTW. Please forgive the typo with long and int. There are all long in
my code.
I have acted desperately and set them all to int.

Tnx Rene
 
I

Ian McCulloch

rene said:
Hi Ian,
no I couldn't.
The whole code is 16000 lines long.
If I write a little example code. Everythis works fine.
Yes, could be a bug in the compiler, but Ivr tried several gccs and
iccs.
Same result

Almost no chance it is a compiler bug.

The only thing I can suggest is that you systematically either expand your
working example to the point that it demonstrates the problem, or you strip
down the full code down towards the example, until it becomes clear where
the bug is.

HTH,
Ian
 
T

teejbee

Usually these sorts of problems occur when the code where you set the
value and the code where you read the value actually do not talk to the
same instance of the class. Make *sure* you are dealing with the same
instance of curve class and as posted earlier, you are calling the
setCurvekeyTime() before CurvekeyTime() call.

If you have access for modifying the code, try to print (or by any
other means) check the value of "this" pointer in both the
setCurvekeyTime() and CurvekeyTime() functions.

Hope this helps,
TJ
 
R

rene

Hi TJ,

tried this. Won't help.
Got a question. Do I need that constructor to initialize the integers
(ckeytime, ckeyspeed)?
Will the mem space reserved by initialize the class curve(normal or as
pointer; curve mycur or. mycur= new curve.)

If also tried the set these values via the constructor.
curve::curve(int a, int b){ckeytime=a;ckeyspeed=b;};
mycur=new curve(12,13);

But It don't work either.
 
T

TJ

tried this. Won't help.
So you have confirmed that you get same value of 'this' pointer and
hence effectively talking to the same instance in both the places?
Got a question. Do I need that constructor to initialize the integers
(ckeytime, ckeyspeed)?
Hmm... Not necessary, but it is a very good practise to initialize all
the member variables in the constructor.

When you create an instance as a local (aka automatic) variable, the
memory space will not be initialized; as is the case of curve mycur.
Whereas when you create an instance in the heap (new curve), the memory
space is initialized to zero by *most* of the compilers.

Setting the values via constructor or via any set functions, does not
matter much. Look out for other places in the code where you change
the value of the variables.

By the way what is the value you get when you query for the variables?
Do you get zero (and you have intialized to 0 in the ctor) or you get
junk?
 

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
474,200
Messages
2,571,046
Members
47,646
Latest member
xayaci5906

Latest Threads

Top