-
March 4th, 2006, 07:54 AM
#1
Changes in value output
PHP Code:
template<typename T>
class AClass{
public:
AClass(){
std::cout<<"AClass is called with "<<var<<'\n';
}
private:
T var;
};
int main(){
AClass<int> m;
return 0;
}
Weird value of var will certainly be output but why it changes everytime I make any change in AClass ?
-
March 4th, 2006, 08:02 AM
#2
Re: Changes in value output
Because it's never initialize? So var's value is undefined? What do you think?
-
March 4th, 2006, 08:04 AM
#3
Re: Changes in value output
But why does it change each time I add something to the class ?
-
March 4th, 2006, 08:27 AM
#4
Re: Changes in value output
Because you change the alignment in memory, when you change the class.
Please don't forget to rate users who helped you!
-
March 4th, 2006, 08:31 AM
#5
Re: Changes in value output
Originally Posted by PeterPea
But why does it change each time I add something to the class ?
In your program var value is not initialized, and noone knows why its value is what it is. It depends on everything, like memory layout of your program, OS, and curent weather in Tokyo.
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
March 4th, 2006, 11:12 AM
#6
Re: Changes in value output
Originally Posted by Hobson
In your program var value is not initialized, and noone knows why its value is what it is. It depends on everything, like memory layout of your program, OS, and curent weather in Tokyo.
......
- Sreehari
"Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
" Everybody is sent to Earth on a purpose. I am so Lagging behind that i won't die." – Calvin
-
March 4th, 2006, 11:54 AM
#7
Re: Changes in value output
Your mind will take you far
MY tv is not bilingual, has no button to switch the language, please make them all in English at night because some of us don't understand , thank you
-
March 6th, 2006, 02:29 AM
#8
Re: Changes in value output
POD types are not default initialized. You need to initialize them yourself. Had it been a non-POD type object (var) - something for which the operator<< were overloaded to work with std::cout - you would have had got the same value over and over again. Since int is a POD type - you get garbage - that could be same for the first 1 million times and never get that value ever...
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|