Reference - Programmer

This is a discussion on Reference - Programmer ; Is it possible to create a reference to another object such as: int & x = i; but using an image list, and referencing different objects depending on a condition? such as : CImageList & imageList; if (thumbnail == 1) ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: Reference

  1. Reference

    Is it possible to create a reference to another object such as:

    int & x = i;

    but using an image list, and referencing different objects depending on a
    condition?

    such as :

    CImageList & imageList;
    if (thumbnail == 1) imageList = m_ImageListThumb;
    else imageList = m_ImageListFullSize;

    Hope this makes sense!

    Ben



  2. Re: Reference

    Ben Williamson wrote:
    > Is it possible to create a reference to another object such as:
    >
    > int & x = i;
    >
    > but using an image list, and referencing different objects depending on a
    > condition?
    >
    > such as :
    >
    > CImageList & imageList;
    > if (thumbnail == 1) imageList = m_ImageListThumb;
    > else imageList = m_ImageListFullSize;


    A reference must be initialized, and can only be initialized. After
    initialization, it will always refer to the same object. I'm not sure
    I'd advise it, but you could do something like this:

    CImageList *temp[] = { &m_ImageListFullSize, &m_ImageListThumb};

    CImageList &imageList = *temp[thumbnail == 1];

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.


  3. Re: Reference

    @Ben Williamson:

    You can write something like this:

    CImageList& imageList = m_ImageListFullSize;
    if (thumbnail == 1)
    imageList = m_ImageListThumb;

    @Jerry Coffin:

    > After initialization, it will always refer to the same object.


    It is not true. You must initialize reference, but can change reference
    later:

    int i = 1;
    int j = 2;

    int& n = i;
    printf("n = %d\n", n);

    n = j;
    printf("n = %d\n", n);


  4. Re: Reference

    Thanks.

    I've tried Kela's suggestion but I get the error :
    error C2582: 'CImageList' : 'operator =' function is unavailable

    on the following line:
    imageList = m_ImageListThumb;

    Any ideas?

    "Kela" wrote in message
    news:1139737607.276996.291190@f14g2000cwb.googlegr oups.com...
    > @Ben Williamson:
    >
    > You can write something like this:
    >
    > CImageList& imageList = m_ImageListFullSize;
    > if (thumbnail == 1)
    > imageList = m_ImageListThumb;
    >
    > @Jerry Coffin:
    >
    >> After initialization, it will always refer to the same object.

    >
    > It is not true. You must initialize reference, but can change reference
    > later:
    >
    > int i = 1;
    > int j = 2;
    >
    > int& n = i;
    > printf("n = %d\n", n);
    >
    > n = j;
    > printf("n = %d\n", n);
    >




  5. Re: Reference

    I'm so sorry... I was not right. You cannot reinitialize reference.

    See answer of Ajay Kalra:
    http://groups.google.com.ua/group/mi...e469810fabbbc6


  6. Re: Reference

    Kela wrote:

    [ ... ]

    > It is not true. You must initialize reference, but can change reference
    > later:
    >
    > int i = 1;
    > int j = 2;
    >
    > int& n = i;
    > printf("n = %d\n", n);
    >
    > n = j;
    > printf("n = %d\n", n);


    This is perfectly legal, but it's doing something entirely different --
    it's creating n as a reference to i. Then it's taking the value of j,
    and assigning it to what n refers to -- i.e. n still refers to i, but i
    now has a new value.

    #include

    int main() {

    int i=1;
    int j=2;

    int &n = i;

    std::cout << "\nn = " << n << ", i = " << i << ", j = " << j;

    n = j;
    std::cout << "\nn = " << n << ", i = " << i << ", j = " << j;
    return 0;
    }

    With any C++ compiler that operates even sort of close to correctly,
    the first will give values of 1, 1 and 2, while the second will give
    values of 2, 2, and 2.

    FWIW, there have been languages that allowed exactly what you said --
    Algol 68, for one example, allowed you to re-target a reference after
    it was created. If you want to do something like that in C++, however,
    you need to use a pointer instead of a reference.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.


+ Reply to Thread