Thread: Does it makes sense?

  1. #1
    Registered User heljy's Avatar
    Join Date
    Mar 2002
    Posts
    36

    Question Does it makes sense?

    I was assigned to write a function for a hashtable.

    The declaration looks like this:

    Status lookup(const File* file, const int pageNo, int & frameNo);

    I am new to C++ but have written in C and Java before.

    Now, the thing that puzzles me is the parameter

    int & frameNo

    what does that mean?

    thanks

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    int&

    This means reference to int. That means that you are passing a pointer to your int to your function but you can treat it as the object itself rather than a pointer. This makes it act as an alias.

    For instance....
    Code:
    #include<iostream>
    using namespace std;
    
    void foo(int);
    void bar(int&);
    
    int main()
    {
    int a=10;
    cout<<" value of a is :- "<<a<<endl;
    foo(a);
    cout<<"value of a after foo is :-"<<a<<endl;
    bar(a);
    cout<<"value of a after bar is :-"<<a<<endl;
    return 0;
    }
    
    void foo(int b)
    {
    b-=5;
    cout<<"value of b in foo is :-"<<b<<endl;
    }
    
    void bar(int& b)
    {
    b-=5;
    cout<<value of b in bar is :-"<<b<<endl;
    }
    As u see passing a by value to foo results in no change to a whereas passing a by reference to bar does result in a change in a. this is because b in bar is an alias for a in main. hope this helps you understand. If ive just made you more confused let me know and ill try again to explain it to 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

  3. #3
    Registered User heljy's Avatar
    Join Date
    Mar 2002
    Posts
    36
    So how we assign referenace is basically


    int value;
    int & ref = value;

    instead of pointers like this

    int value;
    int *pt = &value;


    Is that correct? And we can use ref without dereferencing it? Is it also true that references must be initialized once its declared or something like that?

    Is reference only in C++??

    Thanks for the help!
    If only life is as easy as C...

  4. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Is that correct? And we can use ref without dereferencing it? Is it also true that references must be initialized once its declared or something like that?
    That is correct. All references MUST be initialized once or else you get a compile-error.

    When you assign ref to something you are actually assigning value in this case.

    Code:
    int value;
    int & ref = value;
    ref = 100; //means value = 100;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printf output makes no sense. Is it a Pointer problem?
    By officedog in forum C Programming
    Replies: 3
    Last Post: 10-03-2008, 09:01 AM
  2. Advice requested, Code makes sense to me, not compiler
    By andrew.bolster in forum C Programming
    Replies: 53
    Last Post: 01-06-2008, 01:44 PM
  3. Help: makes pointer from integer without a cast
    By steffi in forum C Programming
    Replies: 4
    Last Post: 11-14-2007, 04:38 AM
  4. doubleanti's gender poll...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 111
    Last Post: 08-04-2002, 12:15 PM
  5. WinXP Upgrade Doesn't Make Any Sense
    By Troll_King in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-25-2001, 04:18 PM