can a container contain references?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jess

    can a container contain references?

    Hello,

    Is it possible to put object references into a container, such as a
    vector? I tried the following, but the compiler gave me lots of error
    messages.

    struct A{
    int x;
    };

    int main(){
    vector<A&X;
    A a;
    X.push_back(a);
    }

    Thanks.
    Jess

  • modemer

    #2
    Re: can a container contain references?

    It's impossible as STL's implementation doesn't support it. If you
    like this feature, you have to define your own container.

    Cheers

    On May 9, 10:23 am, Jess <w...@hotmail.c omwrote:
    Hello,
    >
    Is it possible to put object references into a container, such as a
    vector? I tried the following, but the compiler gave me lots of error
    messages.
    >
    struct A{
    int x;
    >
    };
    >
    int main(){
    vector<A&X;
    A a;
    X.push_back(a);
    >
    }
    >
    Thanks.
    Jess

    Comment

    • Gianni Mariani

      #3
      Re: can a container contain references?

      Jess wrote:
      Hello,
      >
      Is it possible to put object references into a container, such as a
      vector? I tried the following, but the compiler gave me lots of error
      messages.
      >
      struct A{
      int x;
      };
      >
      int main(){
      vector<A&X;
      A a;
      X.push_back(a);
      }

      You can make a vector of pointers:

      struct A{
      int x;
      };

      int main(){
      vector<A*X;
      A a;
      X.push_back(&a) ;
      }

      Comment

      • Fei Liu

        #4
        Re: can a container contain references?

        Jess wrote:
        Hello,
        >
        Is it possible to put object references into a container, such as a
        vector? I tried the following, but the compiler gave me lots of error
        messages.
        >
        struct A{
        int x;
        };
        >
        int main(){
        vector<A&X;
        A a;
        X.push_back(a);
        }
        >
        Thanks.
        Jess
        >
        Container element must be copyable and assignable...If you check
        push_back's prototype, then it's clear why you are getting all the error
        messages.

        void push_back(const T& x);

        Fei

        Comment

        • red floyd

          #5
          Re: can a container contain references?

          Jess wrote:
          Hello,
          >
          Is it possible to put object references into a container, such as a
          vector? I tried the following, but the compiler gave me lots of error
          messages.
          >
          No. Container elements must be assignable (and/or copyable?).

          Comment

          • Pete Becker

            #6
            Re: can a container contain references?

            Jess wrote:
            >
            Is it possible to put object references into a container, such as a
            vector?
            Not directly. But if you've got an implementation of TR1 (or Boost) you
            can use a container of std::tr1::refer ence_wrapper<To bjects. A
            reference_wrapp er<Tacts pretty much like a T&, except that it can be
            copied and assigned.

            --

            -- Pete
            Roundhouse Consulting, Ltd. (www.versatilecoding.com)
            Author of "The Standard C++ Library Extensions: a Tutorial and
            Reference." (www.petebecker.com/tr1book)

            Comment

            • blangela

              #7
              Re: can a container contain references?

              On May 9, 8:11 am, red floyd <no.s...@here.d udewrote:
              Jess wrote:
              Hello,
              >
              Is it possible to put object references into a container, such as a
              vector? I tried the following, but the compiler gave me lots of error
              messages.
              >
              No. Container elements must be assignable (and/or copyable?).
              Once a reference has been initialized, it cannot be made to reference
              any other object (unlike some other programming languages). In other
              words, in the lifetime of a reference, it can reference one and only
              one object.

              Since the element of a container is normally constructed before an
              object "is put in the container element", it is no longer possible to
              store the object in the reference since the reference has already been
              created and initialized. If it were possible to change the object
              that a reference references, we would not have this problem. Since
              pointers are allowed to point to different objects in their lifetime,
              we must use pointers instead.

              Are my statements above basically correct (sorry for the English
              syntax, I did not start leaning English until I was 4 years old :) )?

              Bob

              Comment

              Working...