1
#include<iostream>


class A {
    protected:
        int x;
    public:
        A(int n){
            x = n;
        }

        int getX(){return x;}

        static A add(A a, A b){
            return A(a.getX() + b.getX()); 
        }

        void print(){
            std::cout << x;
        }
};

class B : public A {
    public:
        B(int x) : A(x){

        };
};


int main(){
    B a(10), b(10);

    B c = A::add(a, b);
    c.print();

    int d;
    std::cin >> d;
    return 0;
}

For this snippet I get an error that

main.cpp: In function ‘int main()’: main.cpp:34:17: error: conversion from ‘A’ to non-scalar type ‘B’ requested B c = A::add(a, b);

I understand the error that the error is because I can't pass a object of class B when the parameter is of class A.

I want to know if there is any way out of this, something like B c = A::add(a.super (), b.super()); or something like this ?

Although I can easily get rid of this error in my actual program by removing inheritance and making a object of A inside of class B, I still want to use the inheritance in my program.

8
  • 9
    That's because B is an A but A is not a B so it can't cast an A into a B Commented Sep 1, 2017 at 13:34
  • 3
    I like the title of your question... very not ambiguous! Commented Sep 1, 2017 at 13:36
  • Every B is an A. Not all A are B Commented Sep 1, 2017 at 13:41
  • @litelite Oh you are correct. So no way out of this without leaving the inheritance of A into B ? Commented Sep 1, 2017 at 13:42
  • 4
    The real issue is the opposite of what you think - You can pass a B to add because B is implicitly convertible to A (be careful regarding object slicing though), but you cannot convert the result, which is of type A, to B. Commented Sep 1, 2017 at 13:43

1 Answer 1

2

Type compatibility is ascending. If B derives from A, then any object of type B can be converted or viewed as an object of type A, but not the converse. Think about Cars and Vehicules. Any car is a Vehicules, but not all Vehicules are Cars some are Airplanes.

Here the problem is that the method declares returning an object of type A, but the target of the assignment is of type B. Converting a object of type A to an object of type B cannot be guaranteed here so compiler complains.

Write:

A c = A::add(a,b);

and the compiler will be silent.

Now I understand that you need polymorphism; For it you need to use pointers or references. But as your problem is not really specified, I can't help you more on this.

Sign up to request clarification or add additional context in comments.

2 Comments

Will you help me if I edit my question to be more specific ?
Don't rewrite it too much, if needed post another question, that the rule here. Define your problem/goal and post the code you tried...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.