Test Whether a Class Is Polymorphic

Test whether a class is polymorphic

I cannot imagine any possible way how that typeid could be used to check that type is polymorphic. It cannot even be used to assert that it is, since typeid will work on any type.
Boost has an implementation here. As for why it might be necessary -- one case I know is the Boost.Serialization library. If you are saving non-polymorphic type, then you can just save it. If saving polymorphic one, you have to gets its dynamic type using typeid, and then invoke serialization method for that type (looking it up in some table).

Update: it appears I am actually wrong. Consider this variant:

template <class T> 
bool isPolymorphic() {
bool answer=false;
T *t = new T();
typeid(answer=true,*t);
delete t;
return answer;
}

This actually does work as name suggests, exactly per comment in your original code snippet. The expression inside typeid is not evaluated if it "does not designate an lvalue of polymorphic class type" (std 3.2/2). So, in the case above, if T is not polymorphic, the typeid expression is not evaluated. If T is polymorphic, then *t is indeed lvalue of polymorphic type, so entire expression has to be evaluated.

Now, your original example is still wrong :-). It used T(), not *t. And T() create rvalue (std 3.10/6). So, it still yields an expression that is not "lvalue of polymorphic class".

That's fairly interesting trick. On the other hand, its practical value is somewhat limited -- because while boost::is_polymorphic gives you a compile-time constant, this one gives you a run-time value, so you cannot instantiate different code for polymorphic and non-polymorphic types.

C++ polymorphism: how to test if a class derived from another base class?

You can perform dynamic_cast like this:

 if (dynamic_cast<Base2 *>(p)) {

online_compiler

Unlike approach with typeid this one does not require an extra header to be included, however it relies on RTTI as well (this implies that these classes need to be polymorphic).

C++ polymorphism: Checking data type of sub class

You can do this by checking if dynamic_cast<CRectangle*>(ptr) return non-null, where ptr is a pointer to CPolygon. However this requires the base class (CPolygon) to have at least one virtual member function which you probably need anyway (at least a virtual destructor).

In C++ Is there a (new) way to test if a class has a virtual destructor in a template without instantiating an instance?

With C++11, or later, the std::has_virtual_destructor<T> template provides this information.

Check if an object belongs to a class in Java

The instanceof keyword, as described by the other answers, is usually what you would want.
Keep in mind that instanceof will return true for superclasses as well.

If you want to see if an object is a direct instance of a class, you could compare the class. You can get the class object of an instance via getClass(). And you can statically access a specific class via ClassName.class.

So for example:

if (a.getClass() == X.class) {
// do something
}

In the above example, the condition is true if a is an instance of X, but not if a is an instance of a subclass of X.

In comparison:

if (a instanceof X) {
// do something
}

In the instanceof example, the condition is true if a is an instance of X, or if a is an instance of a subclass of X.

Most of the time, instanceof is right.

C++ Determine the type of a polymorphic object at runtime

I think this will work for you.
First we define a pure virtual member function call_bar so that we can call it on the base class A. For the implementation we need the right type but with CRTP the user class can specify it for us.

class A
{
public:
virtual void call_bar() = 0;
virtual ~A() {}
};

template <typename T>
class B : public A
{
public:
virtual void call_bar() override
{
bar(static_cast<T*>(this));
}
};

class C: public B<C>
{
};

Rails Minitest for polymorphism

Fixtures are data which are created and that you can test against. We can refer https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html for detail understanding. In our case we don't need to modify the fixtures.

The error which we are getting in our test case is because we haven't created record in users table. So when it tries to validate the record it check whether the record with mentioned id associable_id: 2 (i.e 2 in our case) is present in users table or not.

You can modify test case as below

test "valid " do
user = User.new(name: 'Jerome', email: 'jerome@gmail.com')
user.save
Contact.new(associable: user)
puts contact.valid?
puts contact.errors.inspect
assert contact.valid?
end

Note: I haven't add other columns as i am not aware of your users and contacts schema. Please add columns if there are any.

What is polymorphism, what is it for, and how is it used?

If you think about the Greek roots of the term, it should become obvious.

  • Poly = many: polygon = many-sided, polystyrene = many styrenes (a), polyglot = many languages, and so on.
  • Morph = change or form: morphology = study of biological form, Morpheus = the Greek god of dreams able to take any form.

So polymorphism is the ability (in programming) to present the same interface for differing underlying forms (data types).

For example, in many languages, integers and floats are implicitly polymorphic since you can add, subtract, multiply and so on, irrespective of the fact that the types are different. They're rarely considered as objects in the usual term.

But, in that same way, a class like BigDecimal or Rational or Imaginary can also provide those operations, even though they operate on different data types.

The classic example is the Shape class and all the classes that can inherit from it (square, circle, dodecahedron, irregular polygon, splat and so on).

With polymorphism, each of these classes will have different underlying data. A point shape needs only two co-ordinates (assuming it's in a two-dimensional space of course). A circle needs a center and radius. A square or rectangle needs two co-ordinates for the top left and bottom right corners and (possibly) a rotation. An irregular polygon needs a series of lines.

By making the class responsible for its code as well as its data, you can achieve polymorphism. In this example, every class would have its own Draw() function and the client code could simply do:

shape.Draw()

to get the correct behavior for any shape.

This is in contrast to the old way of doing things in which the code was separate from the data, and you would have had functions such as drawSquare() and drawCircle().

Object orientation, polymorphism and inheritance are all closely-related concepts and they're vital to know. There have been many "silver bullets" during my long career which basically just fizzled out but the OO paradigm has turned out to be a good one. Learn it, understand it, love it - you'll be glad you did :-)


(a) I originally wrote that as a joke but it turned out to be correct and, therefore, not that funny. The monomer styrene happens to be made from carbon and hydrogen, C8H8, and polystyrene is made from groups of that, (C8H8)n.

Perhaps I should have stated that a polyp was many occurrences of the letter p although, now that I've had to explain the joke, even that doesn't seem funny either.

Sometimes, you should just quit while you're behind :-)



Related Topics



Leave a reply



Submit