CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Changes in value output

  1. #1
    Join Date
    Mar 2006
    Location
    Bulgaria
    Posts
    2

    Question Changes in value output

    PHP Code:
    template<typename T>
    class 
    AClass{
    public:
      
    AClass(){
        
    std::cout<<"AClass is called with "<<var<<'\n';
      }
    private:
      
    var;
    };

    int main(){
      
    AClass<intm;
      return 
    0;

    Weird value of var will certainly be output but why it changes everytime I make any change in AClass ?

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Changes in value output

    Because it's never initialize? So var's value is undefined? What do you think?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Mar 2006
    Location
    Bulgaria
    Posts
    2

    Re: Changes in value output

    But why does it change each time I add something to the class ?

  4. #4
    Join Date
    Jul 2005
    Location
    Germany
    Posts
    1,194

    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!

  5. #5
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: Changes in value output

    Quote 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 &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  6. #6
    Join Date
    Oct 2005
    Location
    Bangalore
    Posts
    1,051

    Re: Changes in value output

    Quote 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

  7. #7
    Join Date
    Oct 2005
    Location
    Just moved to New Mexico
    Posts
    53

    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

  8. #8
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)