R
Russell Silva
I have a program set up something like this:
class A {
//(...)
}
class B : public A {
//(...)
public:
void not_in_class_A();
}
class usesA {
//(...)
protected:
A A_to_be_used;
}
class usesB : public usesA {
//(...)
void init(B B_to_be_used) {
A_to_be_used = B_to_be_used;
}
void do_something () {
A_to_be_used.not_in_class_A(); //error!
}
}
Assume that usesB::init(B_to_be_used) is the only method that modifies
the A A_to_be_used in a usesB object.
The above error can be easily fixed, if I'm not mistaken, using a cast:
((B) A_to_be_used).not_in_class_A();
but Bjarne Stroustrup says "casts are generally best avoided"
(http://www.research.att.com/~bs/bs_faq2.html#static-cast). I'm trying
to improve my C++ style, so I'm looking for either another solution to
the problem (namely, call A_to_be_used.not_in_class_A() somehow) or
assurance that this is an instance in which casting is stylistically
appropriate.
Thanks in advance.
-Russell Silva
class A {
//(...)
}
class B : public A {
//(...)
public:
void not_in_class_A();
}
class usesA {
//(...)
protected:
A A_to_be_used;
}
class usesB : public usesA {
//(...)
void init(B B_to_be_used) {
A_to_be_used = B_to_be_used;
}
void do_something () {
A_to_be_used.not_in_class_A(); //error!
}
}
Assume that usesB::init(B_to_be_used) is the only method that modifies
the A A_to_be_used in a usesB object.
The above error can be easily fixed, if I'm not mistaken, using a cast:
((B) A_to_be_used).not_in_class_A();
but Bjarne Stroustrup says "casts are generally best avoided"
(http://www.research.att.com/~bs/bs_faq2.html#static-cast). I'm trying
to improve my C++ style, so I'm looking for either another solution to
the problem (namely, call A_to_be_used.not_in_class_A() somehow) or
assurance that this is an instance in which casting is stylistically
appropriate.
Thanks in advance.
-Russell Silva