converting pointer to const ref

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

    converting pointer to const ref

    I can't figure out why this code won't work, or how to fix it:

    list<MyClass> l;
    int main() {
    for (int i=0;i<5;i++) {
    MyClass* myClass = new MyClass;
    myClass -> someInitMethod( );
    l.push_back(myC lass);
    }
    }

    The compiler errors on "l.push_back(my Class)", saying that it can't convert
    MyClass* to const MyClass&.
    Two questions:

    1. Why can't it do this conversion?
    2. How do you make this code work?

    thanks,
    cppaddict





  • David White

    #2
    Re: converting pointer to const ref

    cppaddict <cppaddict@yaho o.com> wrote in message
    news:MoKib.892$ vc4.637@newssvr 27.news.prodigy .com...[color=blue]
    > I can't figure out why this code won't work, or how to fix it:
    >
    > list<MyClass> l;
    > int main() {
    > for (int i=0;i<5;i++) {
    > MyClass* myClass = new MyClass;
    > myClass -> someInitMethod( );
    > l.push_back(myC lass);
    > }
    > }
    >
    > The compiler errors on "l.push_back(my Class)", saying that it can't[/color]
    convert[color=blue]
    > MyClass* to const MyClass&.
    > Two questions:
    >
    > 1. Why can't it do this conversion?[/color]

    Well, there's really no need. If you have a vector of pointers, then you
    push back a pointer. If you have a vector of objects (non-pointer objects
    that is), then push back an object. Why pass a pointer and expect the
    compiler to do a conversion if you can easily pass the correct type?
    [color=blue]
    > 2. How do you make this code work?[/color]

    l.push_back(*my Class);

    DW



    Comment

    • John Carson

      #3
      Re: converting pointer to const ref

      "cppaddict" <cppaddict@yaho o.com> wrote in message
      news:MoKib.892$ vc4.637@newssvr 27.news.prodigy .com[color=blue]
      > I can't figure out why this code won't work, or how to fix it:
      >
      > list<MyClass> l;
      > int main() {
      > for (int i=0;i<5;i++) {
      > MyClass* myClass = new MyClass;
      > myClass -> someInitMethod( );
      > l.push_back(myC lass);
      > }
      > }
      >
      > The compiler errors on "l.push_back(my Class)", saying that it can't
      > convert MyClass* to const MyClass&.
      > Two questions:
      >
      > 1. Why can't it do this conversion?[/color]

      Why should it? C++ is a typed language, which means that variables of
      different types are not interchangeable (some limited conversions/casts are
      possible, but conversions are not universally available). myClass is a
      pointer to a MyClass object, but the list does not store pointers, it stores
      MyClass objects.
      [color=blue]
      > 2. How do you make this code work?[/color]

      Either change the list so it stores pointers (i.e., use list<MyClass*> l) or
      add objects rather than add pointers to the list (i.e., use
      l.push_back(*my Class) ). Whether either option represents the best design is
      another issue.


      --
      John Carson
      1. To reply to email address, remove donald
      2. Don't reply to email address (post here instead)

      Comment

      • Marcin Vorbrodt

        #4
        Re: converting pointer to const ref

        "cppaddict" <cppaddict@yaho o.com> wrote in message
        news:MoKib.892$ vc4.637@newssvr 27.news.prodigy .com...[color=blue]
        > I can't figure out why this code won't work, or how to fix it:
        >
        > list<MyClass> l;
        > int main() {
        > for (int i=0;i<5;i++) {
        > MyClass* myClass = new MyClass;
        > myClass -> someInitMethod( );[/color]

        //l.push_back(myC lass);
        l.push_back(*my Class); // dereference the pointer
        [color=blue]
        > }
        > }
        >
        > The compiler errors on "l.push_back(my Class)", saying that it can't[/color]
        convert[color=blue]
        > MyClass* to const MyClass&.
        > Two questions:
        >
        > 1. Why can't it do this conversion?[/color]

        Because there is a type missmatch. Pointer is not a reference. Function
        expects a const reference, but instead it gets a pointer... bad.
        [color=blue]
        > 2. How do you make this code work?[/color]

        Look above.
        [color=blue]
        >
        > thanks,
        > cppaddict
        >
        >
        >
        >
        >[/color]


        Comment

        • Jakob Bieling

          #5
          Re: converting pointer to const ref

          "cppaddict" <cppaddict@yaho o.com> wrote in message
          news:MoKib.892$ vc4.637@newssvr 27.news.prodigy .com...[color=blue]
          > I can't figure out why this code won't work, or how to fix it:
          >
          > list<MyClass> l;
          > int main() {
          > for (int i=0;i<5;i++) {
          > MyClass* myClass = new MyClass;
          > myClass -> someInitMethod( );
          > l.push_back(myC lass);
          > }
          > }
          >
          > The compiler errors on "l.push_back(my Class)", saying that it can't[/color]
          convert[color=blue]
          > MyClass* to const MyClass&.
          > Two questions:
          >
          > 1. Why can't it do this conversion?[/color]

          Because you are the programmer.
          [color=blue]
          > 2. How do you make this code work?[/color]

          Change your line to:

          l.push_back (*myClass);

          You dereference the pointer and have the actual object (and not just the
          pointer to it). Now the compiler can create a reference to that object and
          pass the reference to push_back ().

          hth
          --
          jb

          (replace y with x if you want to reply by e-mail)


          Comment

          • Jonathan Mcdougall

            #6
            Re: converting pointer to const ref

            cppaddict wrote:

            nice name :)
            [color=blue]
            > I can't figure out why this code won't work, or how to fix it:
            >
            > list<MyClass> l;
            > int main() {
            > for (int i=0;i<5;i++) {
            > MyClass* myClass = new MyClass;
            > myClass -> someInitMethod( );[/color]

            someInitMethod( ) ? That is what constructor are for.
            [color=blue]
            > l.push_back(myC lass);
            > }
            > }[/color]

            Are you sure you want to allocate the objects dynamically? Why not
            something like

            for (int i=0;i<5;i++)
            {
            MyClass myClass;
            myClass.someIni tMethod();

            l.push_back(myC lass);
            }
            [color=blue]
            > The compiler errors on "l.push_back(my Class)", saying that it can't
            > convert MyClass* to const MyClass&.[/color]

            The list you created accepts MyClass objects and you are giving it a
            MyClass*, that is the problem
            [color=blue]
            > Two questions:
            >
            > 1. Why can't it do this conversion?[/color]

            Because it is invalid.
            [color=blue]
            > 2. How do you make this code work?[/color]

            Either with

            list<MyClass*> l;

            in which case *do not forget* to delete every pointer in the list, or with
            the solution I provided.


            Jonathan


            Comment

            • cppaddict

              #7
              Re: converting pointer to const ref

              thank you everyone for your help.

              cpp


              Comment

              Working...