c++ - how do I cast pointers of user defined classes without using inheritance -
i have 2 user-defined classes:
class a:class base of operations { type x; dosomething(); } class b { type x; dosomething(); }
i have function gets variable of type base of operations , utilize dynamic_cast
convert type , utilize dosomething().
class d : class base2 { d(base _base1):base2(base _base1) { //here actual problem } void foo() { //b private fellow member of class base2 of type base of operations *a=dynamic_cast(b); a->dosomething(); } }
but want pass b function ,and @ same time don't want b inherit base.
p.s don't have access alter base
how possible?
casting between unrelated classes in not safe. safest way accomplish think you're trying utilize function template:
template <typename t> void foo(const t& b) { b.dosomething(); }
c++ pointers inheritance casting
No comments:
Post a Comment