Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. can one use vectors of subclasses in a parameter list?

can one use vectors of subclasses in a parameter list?

Scheduled Pinned Locked Moved C / C++ / MFC
helpgraphicsoopquestion
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    genush
    wrote on last edited by
    #1

    I am trying to pass in a vector of some subclass to a function defined with the base class in the parameter list, like void mod(vector < baseclass > &bcs). If I have a vector of subclass objects, as vector < subclass > sc, and call mod(sc), I get the error "A reference that is not to 'const' cannot be bound to a non-lvalue". Doing this without vectors is no problem; i.e, void mod(baseclass &bc), subclass sc; call mod(sc). I can get around this by defining 2 sum(*) functions, one with a baseclass parameter and one with a subclass parameter, but there goes the use of inheritance. The vector will by modified by mod(*), so I don't want to pass as const. Is there a way I can use 1 function on both the base and sub classes? Thanks.

    K A C 3 Replies Last reply
    0
    • G genush

      I am trying to pass in a vector of some subclass to a function defined with the base class in the parameter list, like void mod(vector < baseclass > &bcs). If I have a vector of subclass objects, as vector < subclass > sc, and call mod(sc), I get the error "A reference that is not to 'const' cannot be bound to a non-lvalue". Doing this without vectors is no problem; i.e, void mod(baseclass &bc), subclass sc; call mod(sc). I can get around this by defining 2 sum(*) functions, one with a baseclass parameter and one with a subclass parameter, but there goes the use of inheritance. The vector will by modified by mod(*), so I don't want to pass as const. Is there a way I can use 1 function on both the base and sub classes? Thanks.

      K Offline
      K Offline
      Kevin McFarlane
      wrote on last edited by
      #2

      OK, I haven't done C++ for 5 years but here are some thoughts (which may be wrong but may suggest some ideas) 1. Does C++ allow vector to be substituted for vector in the first place (template covariance)? 2. If it does then try void mod(const vector &bcs) as your function signature. (Parameters should be const-qualified anyway.)

      Kevin

      1 Reply Last reply
      0
      • G genush

        I am trying to pass in a vector of some subclass to a function defined with the base class in the parameter list, like void mod(vector < baseclass > &bcs). If I have a vector of subclass objects, as vector < subclass > sc, and call mod(sc), I get the error "A reference that is not to 'const' cannot be bound to a non-lvalue". Doing this without vectors is no problem; i.e, void mod(baseclass &bc), subclass sc; call mod(sc). I can get around this by defining 2 sum(*) functions, one with a baseclass parameter and one with a subclass parameter, but there goes the use of inheritance. The vector will by modified by mod(*), so I don't want to pass as const. Is there a way I can use 1 function on both the base and sub classes? Thanks.

        A Offline
        A Offline
        Avi Berger
        wrote on last edited by
        #3

        This would be a no, since vector< subclass > is not a subclass of vector< baseclass >. (Note that in the implementation the internal arrays may have different size entries. If you somehow forced this into one function, your single function would be using the wrong entry size and be reading misaligned garbage rather than valid objects.) My first quick thought is that you could use a vector< std::tr1::shared_ptr< baseclass > > in both cases. This would, however, impact the rest of your software, and might not be acceptable. My second thought is that you could turn your function into a template itself. This changes your maintenance to 1 place, but really still leaves you with 2 compiled function bodies. My third thought would be to provide a wrapper ( internally akin to a discriminated union ) with forwarding functions. I believe this would be a bad idea and extra effort without real benefit. Maybe someone else has a better idea, I've run out for now. It looks to me that making your function into a template function is about the best you will do.

        Please do not read this signature.

        modified on Thursday, February 18, 2010 4:22 PM

        G 1 Reply Last reply
        0
        • A Avi Berger

          This would be a no, since vector< subclass > is not a subclass of vector< baseclass >. (Note that in the implementation the internal arrays may have different size entries. If you somehow forced this into one function, your single function would be using the wrong entry size and be reading misaligned garbage rather than valid objects.) My first quick thought is that you could use a vector< std::tr1::shared_ptr< baseclass > > in both cases. This would, however, impact the rest of your software, and might not be acceptable. My second thought is that you could turn your function into a template itself. This changes your maintenance to 1 place, but really still leaves you with 2 compiled function bodies. My third thought would be to provide a wrapper ( internally akin to a discriminated union ) with forwarding functions. I believe this would be a bad idea and extra effort without real benefit. Maybe someone else has a better idea, I've run out for now. It looks to me that making your function into a template function is about the best you will do.

          Please do not read this signature.

          modified on Thursday, February 18, 2010 4:22 PM

          G Offline
          G Offline
          genush
          wrote on last edited by
          #4

          Thanks for the responses everyone. I'll go with the template function.

          1 Reply Last reply
          0
          • G genush

            I am trying to pass in a vector of some subclass to a function defined with the base class in the parameter list, like void mod(vector < baseclass > &bcs). If I have a vector of subclass objects, as vector < subclass > sc, and call mod(sc), I get the error "A reference that is not to 'const' cannot be bound to a non-lvalue". Doing this without vectors is no problem; i.e, void mod(baseclass &bc), subclass sc; call mod(sc). I can get around this by defining 2 sum(*) functions, one with a baseclass parameter and one with a subclass parameter, but there goes the use of inheritance. The vector will by modified by mod(*), so I don't want to pass as const. Is there a way I can use 1 function on both the base and sub classes? Thanks.

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #5

            can you change the parameter to vector < baseclass *> ? yeah, you'd have to build vectors of pointers before each call (which might be a PITA).

            image processing toolkits | batch image processing

            S 1 Reply Last reply
            0
            • C Chris Losinger

              can you change the parameter to vector < baseclass *> ? yeah, you'd have to build vectors of pointers before each call (which might be a PITA).

              image processing toolkits | batch image processing

              S Offline
              S Offline
              Stephen Hewitt
              wrote on last edited by
              #6

              That would seem like a better idea, unless it's part of the plan to slice.

              Steve

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups