Pointer to a derived class C++ - class

I'm having some trouble with deriving a pointer to a derived class. I think it has something to do with a constructor. Do I have to create a new constructor in my derived class? How to I create a pointer from a derived class? Thanks
Class Base
{
public:
int myfunction(int a,int b,int c)
{
return a+b+c;
}
};
Class Derived: public Base
{
int newfunction(int a, int b, int c)
{
return a*b*c;
};
};
int main()
{
// this doesn't work at all.. I get all errors every time I try to refer to the object
//instantiated from my derived class.
//I know it's my lack of understanding.
Derived *NewObject = new Derived;
//Why wont this work?
}

C++ is case sensitive, and the keyword class is lowercase. You wrote Class for both classes, and it looks like it's the only issue with your code.

Related

Swift. How is it possible to get the name of an instance of a class within another class

Could the Swift Jedi help me, I'm new to Swift.
There is class A and class B.
Is it possible to get the name (var name = Class A() ) of an instance of class A in the code of class B, which would then be added to the array.
You can get the class of an object, using type(of: ...). Example:
class A {
func test() {
print (type(of: self))
}
}
class B : A{
}
let a = A()
a.test()
let b = B()
b.test()
However, this information is available only at runtime and not at compile time. You may for example display the name of the class. But you cannot dynamically create an array or a variable of that class.
If you need this, you need to design your classes using some kind of polymorphism. If all the classes have a base class in common, you can just define an array of the base class. If not, you may consider letting the classes of your design share some common protocol:
protocol MySpecs {
func test()
}
class AA : MySpecs {
...
}
class BB : MySpecs {
...
}
var mylist = [ MySpecs ]()
mylist.append(AA())
mylist.append(BB())
for x in mylist {
x.test()
}
There are other possibilities. But unless you provide more details it'll be difficult to be more specific in the recommendations.

"'Class.field=' isn't a valid override" when overriding a field with a more specific type

abstract class Ainterface {
String? name;
List<Binterface?>? list;
}
abstract class Binterface {
String? age;
int? len;
}
class ObjA extends Ainterface {
String? name;
List<ObjB?>? list;
ObjA({this.name, this.list});
}
class ObjB extends Binterface {
String? age;
int? len;
ObjB({this.age, this.len});
}
I used objb as the type of each item in the list, but the editor gave an error 'obja. List =' ('void function (list < objb? >?)) isn't a valid override of 'Ainterface.list=' ('void Function(List<Binterface?>?)').
How I can solve this problem?
Fields (data members) in Dart implicitly provide getters and setters as part of the interface. Therefore when you write:
abstract class Ainterface {
List<Binterface?>? list;
}
The interface is implicitly:
abstract class Ainterface {
List<Binterface?>? get list;
set list(List<Binterface?>? value);
}
The problem is that your derived class wants to provide an interface:
class ObjA extends Ainterface {
List<ObjB?>? get list;
set list(List<ObjB?>? value);
}
Even though List<ObjB?>? is substitutable for List<Binterface?>?, the reverse is not true: List<Binterface?>? is not substitutable for List<ObjB?>?, and that's the direction that matters for the setter.
The list setter in ObjA is not a safe override of the corresponding setter from Ainterface because it would allow callers to violate its contract. Ainterface.list advertises that it allows being set to any instance of a Binterface, but ObjA.list expects only an instance of ObjB. As a concrete example, if the override were allowed, then the following code would compile without error:
class AnotherB extends Binterface {}
void main() {
Ainterface a = ObjA();
a.list = [AnotherB()]; // This error would not be caught at compile-time.
}
And now a.list contains a List<AnotherB> even though ObjA.list expects List<ObjB>, and you'd eventually get an error at runtime if you try to use a.list as a List<ObjB>.
If you can logically guarantee that the above scenario will never happen, then you can use the covariant keyword to relax static type-checking and allow the override:
abstract class Ainterface {
covariant List<Binterface?>? list;
}
but I re-emphasize that the above code removes some type-safety. By using the covariant keyword, you take responsibility for ensuring that you do not violate the contract in practice.
Alternatively:
Reconsider if your abstract base class needs to expose setters as part of its interface, and expose only getters if possible.
Make Ainterface a generic class parameterized on the concrete type of Binterface:
abstract class Ainterface<DerivedB extends Binterface> {
List<DerivedB?>? list;
}
class ObjA extends Ainterface<ObjB> {
List<ObjB?>? list;
}
I'll also point out that overriding fields is usually a bad idea, and you usually should be explicitly overriding getters and setters anyway.
In the Ainterface abstract class you declared that a list should be declared of type Binterface so any class extending that class should do that. so when you want to declare a class extending this class you should consider that. if you want to also declare a list from type ObjB you can do it after that. it will be like this:
class ObjA extends Ainterface {
String? name;
List<Binterface?>? list;
List<ObjB ?>? list2;
ObjA({this.name, this.list, this.list2});
}

How to generate Mobx store, json, hive class from single class;

I am using the code generation to use the Mobx, Hive, and JsonSerializable for a flutter.
So I have a Class I need to define it 3 times to generate all the required classes to work properly.
suppose I have a class:-
class A {
int x
}
I need to define it 3 times.
for JsonSerializable:-
#JsonSerializable
class A {
int x
}
For Hive:-
#HiveType(0)
class A {
#HiveField(0)
int x
}
for mobx:-
class A = _A with _$A;
abstract class _A with Store{
#observable int x
}
is there any way to create just one class definition and create all the required generated class?

Call parent constructor with this as argument

I have a class that takes one of its subclasses as an argument. When constructing that subclass, I want to be able to use this as the value of that argument:
class A(val b: B)
class B extends A(this)
However, this fails to compile
this can be used only in a class, object, or template
[error] class B extends A(this)
[error] ^
Is there any way to get around this? I'm pretty sure that a pattern like this can be written in Java.
I'm not so sure about the last statement:
public class MyClass {
static class A {
A(B b) {
System.out.println(b.value);
}
}
static class B extends A {
String value;;
B() {
super(this);
value = "x";
}
}
public static void main(String args[]) {
new B();
}
}
gives the following error:
/MyClass.java:10: error: cannot reference this before supertype constructor has been called
super(this);
^
There is no good reason to attempt to let the this reference escape the scope of the constructor before the object itself has been constructed. Refactor it.

Swift generics complex inheritance

The following Swift code does not compile:
class Entity {
}
class EntityConverter<T: Entity> {
}
class GetEntityServerAction<T: Entity, C: EntityConverter<T>> {
}
class GetEntityListServerAction<T: Entity, C: EntityConverter<T>>: GetEntityServerAction<T, C> {
}
with the error:
Type 'C' does not inherit from 'EntityConverter<T>'
for the GetEntityListServerAction class definition.
For some reasons the compiler doesn't see that C parameter is defined like inheriting exactly the same type it wants.
The code should look rather simple for those who are used to complicated generic hierarchies in Java or C# but the Swift compiler doesn't like something indeed.
You might find that using protocols and associated types is the more Swift-like way of doing things:
class Entity { }
protocol EntityConversionType {
// guessing when you say conversion, you mean from something?
typealias FromEntity: Entity
}
class EntityConverter<FromType: Entity>: EntityConversionType {
typealias FromEntity = FromType
}
class GetEntityServerAction<T: Entity, C: EntityConversionType where C.FromEntity == T> { }
class GetEntityListServerAction<T: Entity, C: EntityConversionType where C.FromEntity == T>: GetEntityServerAction<T, C> { }
let x = GetEntityListServerAction<Entity, EntityConverter<Entity>>()
Possibly GetEntityServerAction can also be represented by just a protocol, and that you could convert Entity, EntityConverter and GetEntityListServerAction to structs, also.