Polymorphic conversion in return type of function

In summary, the conversation discusses two class templates, BinNode<E> and BSTNode<Key, E>, where BSTNode is derived from BinNode. The relevant code from each template is shown, and it is mentioned that the compiler will implicitly convert a BSTNode pointer to a BinNode pointer. However, this does not work for the parameter type of BSTNode::setLeft, so it must be downcasted in the function body. Additionally, the return type for BinNode::left is a BinNode pointer, while the return type for BSTNode::left is a BSTNode pointer, and the compiler only allows implicit conversion for the return type and not the parameter type. The reason for this is unclear, but it is speculated that it
  • #1
JonnyG
233
30
I have an abstract class template, BinNode<E> and another class template, BSTNode<Key, E>, which is derived from BinNode<E>. Here is the relevant pieces of code from each template:

BinNode:
template <typename E> class BinNode {
public:
    virtual BinNode<E>* left() const = 0;
    virtual void setLeft(BinNode<E>*) = 0;
};

And

BSTNode:
template <typename Key, typename E> class BSTNode : public BinNode<E> {
public:
    BSTNode<Key, E>* left() const override { return lc; }
    void setLeft(BinNode<E>* l) override { lc = static_cast<BSTNode*>(l); }
};

I figured that the compiler will implicitly convert a BSTNode pointer to a BinNode pointer, allowing me to set the parameter type of BSTNode::setLeft to a BSTNode pointer, because that's what I really want. But if I do that then it won't compile. I guess the parameter type has to match exactly. So I am forced to make the parameter type of BSTNode::setLeft be a BinNode pointer then downcast it in the function body. Fair enough.

But now look at the function left in both classes. The return type of BinNode::left is a BinNode pointer, but the return type of BSTNode::left is a BSTNode pointer. So the compiler can do the implicit conversion if it's the return type but not if it's the parameter type?

Is there a reason for this besides "that's just the way the language was designed"?
 
Technology news on Phys.org
  • #2
I don't understand why the compiler would be more lenient with the return type than the parameter type.
 

Related to Polymorphic conversion in return type of function

1. What is polymorphic conversion in return type of function?

Polymorphic conversion in return type of function refers to the ability of a function to return different data types depending on the input parameters. This allows the function to be more flexible and handle a variety of input types.

2. How does polymorphic conversion work?

Polymorphic conversion works by using a technique called function overloading, where multiple functions with the same name but different parameters are defined. When the function is called, the appropriate version is chosen based on the input parameters and the return type is converted accordingly.

3. Why is polymorphic conversion useful?

Polymorphic conversion is useful because it allows a function to be more versatile and handle a wider range of input types. This can save time and effort in writing multiple functions to handle different types of data, and also makes the code more concise and readable.

4. What are some examples of polymorphic conversion in return type of function?

Some examples of polymorphic conversion in return type of function include a function that can add both integers and floating-point numbers, a function that can concatenate strings of different lengths, and a function that can multiply matrices of varying dimensions.

5. Are there any limitations to using polymorphic conversion in return type of function?

Yes, there are some limitations to using polymorphic conversion in return type of function. It can make the code more complex and difficult to understand, and may also lead to unexpected results if not implemented carefully. Additionally, some programming languages do not support polymorphic conversion.

Similar threads

  • Programming and Computer Science
Replies
23
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
910
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
6
Views
958
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
795
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top