can one use vectors of subclasses in a parameter list?
-
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.
-
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.
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
-
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.
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
-
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
-
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.
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). -
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).That would seem like a better idea, unless it's part of the plan to slice.
Steve