Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • Possible duplicate of Casting Class Pointer to Void Pointer Commented Jun 22, 2017 at 8:13
  • Why do you want to avoid inheritance? If you need to be able to store either of the two classes in one variable, then classes have to have something in common. If they have something in common, then inheritance is natural solution. Commented Jun 22, 2017 at 10:21
  • it only one way? but if i have class of matrix and class of Vector? Do you think that it good choice first of all create abstract class? Commented Jun 22, 2017 at 11:03
  • You have two paths in your code. One for Vector and one for Matrix. You want to have a variable which is capable of holding either of the two, so I assume those two code paths join later in, i.e. there's some do_something_with_either_matrix_or_vector(obj); function call. That means both Vector and Matrix share some properties, which in turn means that yes, abstract class (think interface in java/c#) would be good fit. Commented Jun 22, 2017 at 11:50
  • If you don't like dynamic polymorphism, then static polymorphism with templates might suit you better. Commented Jun 22, 2017 at 11:52