No Variable Permanence

L

Larry Lindsey

Aieeeeaiiieeaiiieii, ah think ah need some pro-zac

For some reason, my object variables aren't keeping their values outside of
the method scope. For instance, given:


//Begin code
class Parent{
public:
Parent(){}
virtual ~Parent(){}
virtual void setSomething(int thing){}
virtual int getSomething(){return 0}
...
};
class Child: public Parent{
public:
Child(){...}
Child(int thing){
...
setSomething(thing);
}
~Child(){...}
void setSomething(int thing){
something=thing;
}
int getSomething(){
return something;
}
private:
int something;
};

int main(){
Child *c;
c = new Child(12);
cout << c.getSomething()<<endl;
return 0;
}
//end code

the output will not be 12, but -842150451. I suspect that I'm doing
something wrong with the virtual keyword. Can anyone explain this?

Thanks,

Larry
 
C

Christopher Benson-Manica

Larry Lindsey said:
int main(){
Child *c;
c = new Child(12);
cout << c.getSomething()<<endl; ^^^^^^^^^^^^^^^^
return 0;
}
//end code

Shouldn't you have done

c->getSomething()

instead? c is a pointer...
 
J

jeffc

Larry Lindsey said:
Aieeeeaiiieeaiiieii, ah think ah need some pro-zac

For some reason, my object variables aren't keeping their values outside of
the method scope. For instance, given:


//Begin code
class Parent{
public:
Parent(){}
virtual ~Parent(){}
virtual void setSomething(int thing){}
virtual int getSomething(){return 0}
...
};
class Child: public Parent{
public:
Child(){...}
Child(int thing){
...
setSomething(thing);
}
~Child(){...}
void setSomething(int thing){
something=thing;
}
int getSomething(){
return something;
}
private:
int something;
};

int main(){
Child *c;
c = new Child(12);
cout << c.getSomething()<<endl;
return 0;
}
//end code

the output will not be 12, but -842150451. I suspect that I'm doing
something wrong with the virtual keyword. Can anyone explain this?

Larry, for your benefit, and for those trying to read your code, please only
post code that compiles. The code above doesn't compile. Aside from the
...., you cannot write c.getSomething. c is a pointer, so the syntax is
c->getSomething.
 
M

Mike Wahler

Larry Lindsey said:
Aieeeeaiiieeaiiieii, ah think ah need some pro-zac

For some reason, my object variables aren't keeping their values outside of
the method scope. For instance, given:


//Begin code

The following code is not what's giving the behavior
you describe. It won't even compile.

#include <iostream>
using std::cout;
using std::endl;
class Parent{
public:
Parent(){}
virtual ~Parent(){}
virtual void setSomething(int thing){}
virtual int getSomething(){return 0}

virtual int getSomething() const {return 0;}

// ...
};
class Child: public Parent{
public:
Child(){...}

Child() { /* ... */ }

If you write e.g.

Child ch;

you get an object in an unknown state.

Child ch2 = ch; /* undefined behavior, evaluation
of uninitialized object 'ch' */

Better to use *some* kind of 'default'
value when none is specified, e.g.:

Child() : something(0) {}
Child(int thing){
...
setSomething(thing);
}

Child(int thing) : something(thing) {}
~Child(){...}
void setSomething(int thing){
something=thing;
}
int getSomething(){

int getSomething() const {
return something;
}
private:
int something;
};

int main(){
Child *c;
c = new Child(12);
cout << c.getSomething()<<endl;

This line should not compile. The 'dot operator' can
only be used with a struct, class, or union type. 'c'
is none of these, it's a pointer.

cout << c->getSomething() << endl;

And finally, as Mother always said, put things back
the way you found them:

delete c;
return 0;
}
//end code

the output will not be 12, but -842150451.

Your code won't even compile. But I suspect that when
you ran whatever version of this does compile, you're
accessing the member object 'something' which has not
been initialized.
I suspect that I'm doing
something wrong with the virtual keyword.

virtual has nothing to do with the problem.
Can anyone explain this?

See above.

A complete adaptation of your code that gives
the expected result:


#include <iostream>

using std::cout;
using std::endl;

class Parent {
public:
Parent() {}
virtual ~Parent() {}
virtual void setSomething(int thing) {}
virtual int getSomething() const {return 0;}
};

class Child: public Parent{
public:
Child() {}
Child(int thing) : something(thing) {}
~Child() {}
void setSomething(int thing){
something=thing;
}
int getSomething() const {
return something;
}
private:
int something;
};

int main(){
Child *c;
c = new Child(12);
cout << c->getSomething()<<endl;
return 0;
}

Output:

12

HTH,
-Mike
 
C

Chris Theis

Larry Lindsey said:
Aieeeeaiiieeaiiieii, ah think ah need some pro-zac

Looks like you really should ;-) Please post some compilable code which
actually illustrates your problem and its probable cause.
For some reason, my object variables aren't keeping their values outside of
the method scope. For instance, given:


//Begin code
class Parent{
public:
Parent(){}
virtual ~Parent(){}
virtual void setSomething(int thing){}
virtual int getSomething(){return 0}

What about using a semicolon after return 0 for a change? Why is something
not embedded in the parent class and set to a meaningful value during object
construction?
...
};
class Child: public Parent{
public:
Child(){...}
Child(int thing){
...
setSomething(thing);
}

What do you think about using initializer lists for the constructor?
~Child(){...}
void setSomething(int thing){
something=thing;
}
int getSomething(){

I'd change that to
int getSomething() const {
return something;
}
private:
int something;
};

As I mentioned before you should consider moving something into the parent
class.
int main(){
Child *c;
c = new Child(12);
cout << c.getSomething()<<endl;

You certainly need to use c->getSomething() here!
return 0;

Keep in mind that you haven't deleted the allocated object which will result
in a memory leak!
}
//end code

the output will not be 12, but -842150451. I suspect that I'm doing
something wrong with the virtual keyword. Can anyone explain this?

You shouldn't even get -842150451 because the code you posted won't compile.
Thanks,

Larry

What about this:

class Parent{
public:
Parent( int thing = 0) : something( thing ) {} // use initializer lists
to set up a object with meaningful values
virtual ~Parent(){}
virtual void setSomething(int thing) = 0; // enforce derived
classes to provide an implementation for these functions
virtual int getSomething() const = 0;
protected:
int something;
};

class Child: public Parent{
public:
Child() {}
Child(int thing) : Parent( thing ) {} // pass the parameter on to
the base class
~Child(){}
void setSomething(int thing) {
something = thing;
}
int getSomething() const {
return something;
}
};

int main()
{
Child *c = new Child(12);
cout << c->getSomething()<<endl;
delete c;
return 0;
}

HTH
Chris
 
L

Larry Lindsey

Larry Lindsey said:
Aieeeeaiiieeaiiieii, ah think ah need some pro-zac

For some reason, my object variables aren't keeping their values outside of
the method scope. For instance, given:


//Begin code
class Parent{
public:
Parent(){}
virtual ~Parent(){}
virtual void setSomething(int thing){}
virtual int getSomething(){return 0}
...
};
class Child: public Parent{
public:
Child(){...}
Child(int thing){
...
setSomething(thing);
}
~Child(){...}
void setSomething(int thing){
something=thing;
}
int getSomething(){
return something;
}
private:
int something;
};

int main(){
Child *c;
c = new Child(12);
cout << c.getSomething()<<endl;
return 0;
}
//end code

the output will not be 12, but -842150451. I suspect that I'm doing
something wrong with the virtual keyword. Can anyone explain this?

Thanks,

Larry

Sorry. The code I posted is only representative of the code I'm actually
running, and in my decaffeinated state, I accidentally wrote c.getSomething,
when it should have been c->getSomething().

Either way, the code I actually have running is still returning garbage, but
now I at least know that its something going on somewhere else.....though
for the life of me I can't figure out what. Thanks

--Larry
 
L

Larry Lindsey

#include said:
using std::cout;
using std::endl;

class Parent {
public:
Parent() {}
virtual ~Parent() {}
virtual void setSomething(int thing) {}
virtual int getSomething() const {return 0;}
};

class Child: public Parent{
public:
Child() {}
Child(int thing) : something(thing) {}
~Child() {}
void setSomething(int thing){
something=thing;
}
int getSomething() const {
return something;
}
private:
int something;
};

int main(){
Child *c;
c = new Child(12);
cout << c->getSomething()<<endl;
delete c;
return 0;
}

Output:

12

HTH,
-Mike

This is what the code I posted *should* have looked like, were I not
decaffeinated or lazy. Thanks Mike, for correcting it. In any case, this
is more or less what my actual code looks like, except that where the ...'s
were deleted, there are other things, but this boils it down to the methods
that are actually being used. Is it possible that somehow I've done
something dumb and defined something to where the variable "something" will
not hold its value after the method scope exits? Thanks,

Larry
 
J

jeffc

Larry Lindsey said:
Sorry. The code I posted is only representative of the code I'm actually
running, and in my decaffeinated state, I accidentally wrote c.getSomething,
when it should have been c->getSomething().

Yes, but it will save a lot of time in the future if you actually compile
your snippet. It was very easy to get it to compile with a couple minor
changes, but we shouldn't be the ones making the changes, because then we're
just guessing at what you intended with respect to the parts that don't work
right.
 
M

Mike Wahler

Larry Lindsey said:
This is what the code I posted *should* have looked like, were I not
decaffeinated or lazy. Thanks Mike, for correcting it. In any case, this
is more or less

"More or less" is way too vague for me to try any
guesses.
what my actual code looks like, except that where the ...'s
were deleted, there are other things, but this boils it down to the methods
that are actually being used. Is it possible that somehow I've done
something dumb

I'll let you decide if it's 'dumb' or not, but you're
definitely doing something wrong.
and defined something to where the variable "something" will
not hold its value after the method scope exits? Thanks,

All data members of a class type exists until the
containing object is destroyed.

If you want better analysis, post a small, *compilable*
example program that demonstrates the problem, so we
can give useful assistance.

-Mike
 
M

Mike Wahler

Larry Lindsey said:
Sorry. The code I posted is only representative of the code I'm actually
running, and in my decaffeinated state, I accidentally wrote c.getSomething,
when it should have been c->getSomething().

Either way, the code I actually have running is still returning garbage, but
now I at least know that its something going on somewhere else.....though
for the life of me I can't figure out what. Thanks

Post the real code (pare it down to something that
still gives the problem), and I'm confident we can
identify the trouble. (Hint: compile it before posting
so we don't go off on unrelated tangents fixing typos,
etc.)

-Mike
 
K

Karl Heinz Buchegger

Larry said:
This is what the code I posted *should* have looked like, were I not
decaffeinated or lazy. Thanks Mike, for correcting it. In any case, this
is more or less what my actual code looks like, except that where the ...'s
were deleted, there are other things, but this boils it down to the methods
that are actually being used. Is it possible that somehow I've done
something dumb and defined something to where the variable "something" will
not hold its value after the method scope exits? Thanks,

Until you post your *real* code, all we can say is:
You have a bug somwhere.

Why don't you take your code, remove everything not
directly related to your actual problem, compile it,
run it, to make sure that the bug is still there

and

post that

Then we would have something to work with.
 
L

Larry Lindsey

jeffc said:
Yes, but it will save a lot of time in the future if you actually compile
your snippet. It was very easy to get it to compile with a couple minor
changes, but we shouldn't be the ones making the changes, because then we're
just guessing at what you intended with respect to the parts that don't work
right.

I agree. I'm not naturally a programmer (obviously), and I come from a
background where I do most of this stuff in my head where typos are easily
and automatically ignored, as opposed to using a compiler and fiddling
around with it that way. I'm not saying that its the best approach, hell I
don't even know if its a good one, but its the way I do things. Had I
realized that people would be trying to compile my code to try to reproduce
the error, and that my error was not a very simple one, I would have
actually tested it out in the first place. So accept my apologies for this
faux pas that I have committed. In any case, this code does do what I
expected it to do, but the code I have does not. I figured out the reason
just after my most previous post, and it has to do with the fact that most
of my OOP experience..err...I think its OOP experience, comes from Java and
so what I expect to happen by default in some cases doesn't, in fact,
happen. But thanks to all of you for your help. Even though no one was
able to tell me what it was, being as the problem didn't have anything to do
with the code I posted, you were able to tell me what it was not, which is
just as helpful.

Larry
 
M

Moah, full time turnip.

Larry said:
Aieeeeaiiieeaiiieii, ah think ah need some pro-zac

For some reason, my object variables aren't keeping their values outside of
the method scope. For instance, given:

.... snip ...
the output will not be 12, but -842150451. I suspect that I'm doing
something wrong with the virtual keyword. Can anyone explain this?

I tried your code in Visual C++ 7 (with a few minor tweaks) and it
executed fine (gives 12 as a result). My only suspicion, and I'm far
from an expert, so take it as you want, is that your compiler gets
confused because the Child::setSomething hasn't been declared yet
while in the constructor (the function is declared/defined just be-
low) so it calls the Parent::setSomething, which does nothing.

My 0.02€,
Moah, full time turnip.
 
J

jeffc

Larry Lindsey said:
I agree. I'm not naturally a programmer (obviously), and I come from a
background where I do most of this stuff in my head where typos are easily
and automatically ignored, as opposed to using a compiler and fiddling
around with it that way. I'm not saying that its the best approach, hell I
don't even know if its a good one, but its the way I do things.

Let's just say it's a popular approach :)
 

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
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top