简体   繁体   中英

How can I prevent this kind of casting in c++

Suppose there are two classes like so

class Locator
{
public:
// this goes to the specified latitide and longitude
bool GoToLocation(long lat, long longtd);
};

class HouseLocator : private Locator
{
public:
// this internally uses GoToLocation() after fetching the location from address map
bool GoToAddress(char *p);
}

I make private inheritance to block GoToLocation() on HouseLocator because it doesn't make sense there and to force people to use the proper interface.

Now my question is, how can I prevent this kind of casting?

HouseLocator *phl = new HouseLocator;
Locator *pl = (Locator*)phl;
pl->GoToLocation(10, 10);

Should I just document not to do this and leave the rest to the user, I mean, if he makes wrong cast, its his problem?

Use composition instead of inheritance:

class Locator
{
public:
    bool GoToLocation(long lat, long longtd);
};

class HouseLocator
{
    Locator locator_;
public:
    // internally uses locator_.GoToLocation()
    bool GoToAddress(char *p);
};

If that's not practical for some reason, then using private inheritance as you already are is your best bet -- if the user casts a HouseLocator* to a Locator* they're invoking undefined behavior, and that's their problem, not yours.

If you can change the base class, you can make the function protected instead of public .

Otherwise there isn't much you can do, except telling people not to do this.

You can't. The C cast include the reinterpret_cast functionality and you can't prevent reinterpret_casting between any two data pointer types.

Note that that cast will not do the adjustments needed in some cases of multiple inheritance and thus may fail to achieve its intended result. (static_cast will do the adjustements, but will check accessibility).

This looks as though HouseLocator probably shouldn't derive from Locator . From the limited context, you probably want something that uses locator (ie, it's composed as a private member variable).

Private inheritance is almost exclusively used as a composition device, when you cant compose directly (because you also need to specialize the class).

Inheritance is generally used when you want a heterogeneous collection of some type and you want to treat them all the same (or in similar situations). If youre using the derived class in a way that requires you to know the derived type, inheritance is actually usually harming your design because its the tightest form of coupling (must be constructed and together, cannot be rebound).

Not worth trying to prevent it IMHO. User can anyways do Locator* p = new Locator(); p->GoToLocation(10,10); Locator* p = new Locator(); p->GoToLocation(10,10);

You may also conversion operator

operator Locator() const;

that raises exception in HouseLocator class. But this will not prevent the conversion by reinterpret_cast<>. Nothing can prevent it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM