Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9259495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:40:18+00:00 2026-06-18T12:40:18+00:00

Suppose I have this base class: struct Vehicle { //op stands for the operator

  • 0

Suppose I have this base class:

struct Vehicle {
  //"op" stands for the operator of the vehicle
  //examples: pilot, truck driver, etc.
  virtual void insert_op(Op op) = 0;
  //other members...
};

And these two subclasses

struct Truck : public Vehicle {
  void insert_op(Op op) override {
    //prepare Truck with truck driver
  }
};

struct Airplaine : public Vehicle {
  void insert_op(Op op) override {
    //prepare Airplaine with pilot
  }
};

As you guess, this is the other hierarchy:

struct Op {};
struct TruckDriver : public Op {};
struct Pilot : public Op {};

You already see the problem, don’t you? I want to FORCE Truck to accept only TruckDrivers and FORCE airplaines to accept only Pilots, but this is not possible in the current design. C++ does not allow diff parameters for overridden virtuals.

I guess I could do a run-time type check of the type of “Op” in each of the subclass implementations of insert_op, but that sounds like a really ugly solution, plus its not enforced at compile time.

Any ways out?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:40:19+00:00Added an answer on June 18, 2026 at 12:40 pm

    Your Vehicle says virtual void insert_op(Op op) which means “every vehicle can accept any Op“.

    Therefore, according to your design, a Truck isn’t a valid candidate for a Vehicle subclass because it can’t accept any Op – it can accept only TruckDrivers.

    Related: Liskov substitution principle


    The problem is in your design, not in implementation of it. I suggest to simplify your class hierarchy. Do you really need so many classes and inheritance? Can you simply go with Vehicle and Op that have fields identifying their type?


    Let me further explain the design problem:

    Assume some object A with a method manVehicle(Vehicle&). Truck is a subclass of Vehicle, so it’s possible to call this method with an object of type Truck.

    However, the implementation of A doesn’t have a clue what concrete types of Vehicles. It only knows that all vehicles have a method insert_op(Op), so it’s valid for it to attempt a call like insert_op(Pilot()) even if the vehicle is actually a Truck.

    Conclusions:

    • Compile-time check isn’t even possible

    • Runtime check could work…

      • but would only sweep the problem under the rug. An user of Vehicles expects to be able to call insert_op(Op) on any Vehicle.

    A solution would be to modify the Vehicle interface to look like:

    struct Vehicle {
      virtual bool can_insert_op(Op op) = 0;
      virtual void insert_op(Op op) = 0;
    };
    

    and document it so that the caller would know that insert_op can be only called with Ops that satisfy can_insert_op on the given Vehicle. Or something analogous (like a documented exception from insert_op “invalid op type for this vehicle”) – anything works as long as it’s a documented part of this interface.


    BTW technical remark: You’d probably want these methods to take the Op by pointer or reference instead of copying it, to avoid an unnecessary copy as well as slicing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have: class Base { public: void operator()() { this->run(); } virtual void
Suppose I have some code like this: class Base { public: virtual int Foo(int)
Suppose I have this class hierarchy: class A { public: virtual void foo(Base *b)
Suppose I have a class like this class Base { private: int i; int
Suppose I have a base class foo class foo { virtual int get() const
suppose we have this class structure: public class BaseClass { public BaseClass() { Console.WriteLine(Base
Suppose you have a base class A, and this class is reimplemented by B
Suppose I have a one-to-many relationship like this: class Book(Base): __tablename__ = books id
Hi suppose I have a code like this // base class class A {
Suppose that I have the following python base class: class BaseClass(object): def a(): This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.