Problem with values changing

This is a discussion on Problem with values changing within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by patra_user in my Metaforika constructor? How is that possible? My Metaforika constractor is just empty. I'm missing ...

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by patra_user View Post
    in my Metaforika constructor? How is that possible? My Metaforika constractor is just empty. I'm missing serious subjects here i think.

    or the other way,
    set function for my train component?
    Yes, create a member function of Trena that takes two variables, and pass in y, z to that function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    Member function of Trena
    Code:
    void fill_trena (int,int);

    Code:
    void Metaforika::kratisi(int x,int y,int z){
                           train.fill_trena(2,3);
                            cout<<"inside kratisi: ";
                            cout << train.kr_dr<< endl;

    Ok so far : above code prints 2.

    Code:
     
    void Metaforika::Trena::fill_trena(int y, int z){ 
            kr_dr=y;  
            cout << endl << "function: " << y << endl;
    Ok as well,above code prints 2.


    Code:
                 void Metaforika::test() {
                 Trena train;
    kratisi(1,2,2);
    cout<<"inside test-kratisi: ";
    cout << train.kr_dr<< endl;
    Prints 0 !!

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by patra_user View Post
    Member function of Trena
    Code:
    void fill_trena (int,int);

    Code:
    void Metaforika::kratisi(int x,int y,int z){
                           train.fill_trena(2,3);
                            cout<<"inside kratisi: ";
                            cout << train.kr_dr<< endl;

    Ok so far : above code prints 2.

    Code:
     
    void Metaforika::Trena::fill_trena(int y, int z){ 
            kr_dr=y;  
            cout << endl << "function: " << y << endl;
    Ok as well,above code prints 2.


    Code:
                 void Metaforika::test() {
                 Trena train;
    kratisi(1,2,2);
    cout<<"inside test-kratisi: ";
    cout << train.kr_dr<< endl;
    Prints 0 !!
    The red part shouldn't be there - it creates a local copy of Trena, which isn't the one you want, and since it's local, it will "shadow" (or hide) the instance in your class itself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    AMEN!
    Thanks a lot Mats.
    You sure know how to program.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by patra_user View Post
    AMEN!
    Thanks a lot Mats.
    You sure know how to program.
    Ehm :blush:, yes, at least for simple stuff

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  2. List iteration problem, it resets values
    By Mike Borozdin in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2006, 09:42 AM
  3. Replies: 1
    Last Post: 02-03-2005, 03:33 AM
  4. problem of garbage values
    By aldajlo in forum C Programming
    Replies: 5
    Last Post: 10-02-2004, 05:41 PM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21