Thread: a simple C++ question

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    101

    a simple C++ question

    int main()
    {
    int A, B, C;
    A=2;
    B=4;
    C=6;
    mystery(B,C,A);
    cout<<A+B+C;
    }

    void mystery(int D,int& E, int C)
    {
    int A;
    A=C+1;
    E=A+C;
    C=C*2;
    }

    so is that A=2 (unchange)
    B=4 (unchange)
    C=5 (changed)
    and the output is 11

    is that correct??
    i just found the question from my tutorial!!
    Thanks a lot

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    wrong geuss again!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Unregistered
    Guest
    The values of B and A are passed to Mystery() by value and stored in variables local to Mystery() called D and C respectively. Therefore any changes in D and C in Mystery() will not affect the value of B and A in main().

    The value of C in main()is passed by reference to Mystery() and stored in the variable local to Mystery() called E. Therefore, any changes to E in Mystery() will change the value of C in main().

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    Originally posted by Stoned_Coder
    wrong geuss again!
    Where is the wrong?

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    no i was wrong. I read the call as (A,B,C) for some reason when it was (B,C,A). You were right and i apologize for misleading you.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    How dose the 'address of' operator function when put with a variable decleration like that?

    Ive only see it used to send a pointer of a variable to a function.

    like.

    void do_something(int *p_a) {
    *p_a=0; //modifys the variable declared elsewhere
    }



    int a;
    do_something(&a);

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Its a reference. It provides an alias for another variable. You can imagine a reference as an already dereferenced pointer. i.e.

    int a=10; // an int
    int& ref =a; // now ref is just another name for a so any changes to ref actually change a.
    ref=30;
    cout<<a;

    now lets look at using this as a function parameter.

    void func(int byValue,int& byRef);

    now suppose we call this func with ints a and b like this....
    func(a,b);
    now any changes made to a in the func are local to the func and the value of a in the calling function never changes. You have in fact passed a copy of a to func. Now b is different. Any changes to b make changes to b in the calling function because instead of passing a copy you have passed a reference which is basically an address like a pointer but it behaves as if it is already dereferenced.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM