I have a problem that in both CPP and Java.
#include <vector>
class A {};
class B : A {};
int main()
{
std::vector<A> a;
std::vector<B> b;
a = b; // compiler error
}
For some reason these two vectors are not compatible;
EDIT FOR DETAILS
Thanks for all the answers. I needed to point out that the example above was a simplified version of problem to not confuse with details. My real problem is a Java project, in which I am using a wrapper class to keep a reference connection between two variables.
Printable.java
package ConsoleGraphics;
public class Printable { /** code */ }
StringPrintable.java
package ConsoleGraphics;
public class StringPrintable { /** code */}
Iterator.java
package Jav.util;
public abstract
class Iterator<T> implements java.util.Iterator<T>
{ /** code */ }
Copy_Iter.java
package Jav.util;
// An Iterator keeping deep copies of elements
public class Copy_Iter<T> extends Iterator<T>
{ /** code, defines abstract methods */ }
Ref_Iter.java
package Jav.util;
// Iterator keeping references of the elements.
public class Ref_Iter<T> extends Iterator<T>
{ /** code, defines abstract methods */ }
Box.java
package Jav.util;
public class Box<T> extends Copy_Iter<T>
{ /** code */ }
RefBox.java
package Jav.util;
public class RefBox<T> extends Ref_Iter<T>
{ /** code */ }
Screen.java
package ConsoleGraphics;
// This class creates my problem
public class Screen
{
// data members
private Jav.util.RefBox<Printable> p1;
private Jav.util.RefBox<
Jav.util.Box <Printable> >p2;
/** ctors and methods */
// METHOD OF CONCERN
// As you can see this function accepts a
// Box containing Printable. If I try to feed it a
// Box containing StringPrintable I fail. But if I
// create a seperate method for StringPrintable
// That would mean creating a separate method
// for every class that inherits from Printable.
//
// The aim is for screen class to keep a
// reference to the Box object added it. That
// when that box increases or decreases,
// Screen classes Box will do the same.
// In CPP I wouldn't need the Box wrapper class
// and would just use pointers to pointers.
public void
addPrintable(Jav.util.Box<Printable> p)
{
// pushBack was declared in Jav.util.Iterator
p2.pushBack(p);
}
}
Main.java
package main; // easier to make jar file
import ConsoleGraphics.*;
import Jav.util.*;
public class Main
{
public static void main(String[] args)
{
Box<StringPrintable> nums = new Box<>();
Screen sodoku_game = new Screen();
// error no matching function!
sudoku_game.addPrintable(nums);
}
// Now imagine if someone inherits
class TransformableChars extends Printable
{
/** extends code with techniques to make
Printable Transformable */
}
}