Virtual function input Argument type [duplicate] - c++

This question already has answers here:
"Overloading" pure virtual function with different set of arguments
(5 answers)
Closed 2 years ago.
I am trying to understand inheritance and polymorphism. When I run this :
#include<iostream>
using namespace std;
class Base
{
int x;
public:
virtual Base* fun() = 0;
int getX() { return x; }
};
// This class inherits from Base and implements fun()
class Derived: public Base
{
public:
int y;
Derived* fun() {
Derived *d = new Derived;
d->y = 2;
return d;
}
};
int main(void)
{
Derived d;
Derived *p = d.fun();
cout<< p->y;
}
This works fine as it is legal for a derived class to override a virtual function using a different return type as long as the return type is covariant with the original return type
But what if the virtual function contain the input argument of type Base*. Like :
#include<iostream>
using namespace std;
class Base
{
int x;
public:
virtual Base* fun(Base * t) = 0;
int getX() { return x; }
};
// This class inherits from Base and implements fun()
class Derived: public Base
{
public:
int y;
Derived* fun(Derived *t) {
Derived *d = new Derived;
d->y = t->y;
return d;
}
};
int main(void)
{
Derived d;
Derived *p = d.fun();
cout<< p->y;
}
This is throwing error error: invalid new-expression of abstract class type ‘Derived’ Derived *d = new Derived; which I understand it means that compiler is not recognising the implementation of the virtual function and considering the class ``Derived``` as abstract.
So how can we override virtual function of this type ??
I tried to search it , but didn't find any reference. If similar question exists , let me know.
Thanks

If I needed this in my own code. I'd write the new derived class like this:
// This class inherits from Base and implements fun()
class Derived: public Base
{
public:
int y;
Derived* fun(Base *t) {
Derived *d = new Derived;
Derived* input = dynamic_cast<Derived *>(t);
ASSERT(input); // Cause the debug version to throw an exception so the error can be debugged
if(input)
d->y = t->y;
else
d->y = 0; // Or the default of your choice.
return d;
}
};
This introduces a potential runtime error if the base type passed in is not the correct derived type. But I don't see a way to avoid it.
The other options I see are use templates. Where the derived type requires the derived type to be passed in. Or simply create a new function that takes the derived type as input and also overrides fun(Base *t);

using namespace std;
template <class T>
class Base
{
int x;
public:
virtual T* fun(T* t) = 0;
int getX() { return x; }
};
class Derived : public Base<Derived>
{
public:
int y;
Derived() { y = 0; }
Derived* fun(Derived* t) {
Derived *d = new Derived;
d->y = t->y;
return d;
}
};
int main(int argc, const char* argv[])
{
Derived *d = new Derived;
Derived * p = d->fun(d);
cout << p->y;
return 0;
}
test in visual studio 2019.

Related

Like, virtual function can we make a variable virtual in c++

when a base class pointer points to the object of it's derived class and if a function being overridden we use virtual function to solve the problem . So that we can access the own function of derived class using the pointer.
Like this , i was thinking that if there a way which can be applied on virtual keyword in variable , so that we can access the latest version of a variable in derived class using pointer.
#include <iostream>
using namespace std;
class base
{
public:
int x;//but what about this , if we add virtual keyword here.
//it will give error if trying to do so .
//but can you tell me what can i do if i want to make use of it as virtual function
//if not please tell me why
virtual void display(void) //to call the recent version of display function we make use of virtual here
{
cout << "base\n";
}
};
class derived : public base
{
public:
int x;
void display(void)
{
cout << "derived\n";
}
};
int main(void)
{
base *p;
base ob1;
derived ob2;
p=&ob2;
p->x=100;//here i want to set 100 to the x of derived class not that x which has been inherited
//But it sets to the x of base class which i dont wanted
p->display();//here we can access the latest version of display function in derived class
return 0;
}
Please, No body ask me why i want to do so.I don't have any intention to do in my real code. i asked for the curiosity.
No, you cannot use virtual for fields, only for methods.
However you can simulate that by creating a function that returns a reference to a field:
class Base
{
private:
int x;
public:
virtual int& X() { return x; }
};
class Derived : public Base
{
private:
int x;
public:
virtual int& X() override { return x; }
};
int main()
{
Derived d;
Base* b = &d;
b->X() = 100; // will set d's x
}
You can't override member variables with the virtual keyword. You could, however, have virtual getters and setters that refer to different member variables in the base and derived classes to achieve a similar effect:
class base {
public:
virtual int getX() {
return x;
}
virtual void setX(int x) {
this->x = x;
}
private:
int x;
}
class derived : public base {
public:
int getX() {
return x;
}
void setX(int x) {
this->x = x;
}
private:
int x;
}
The other answers are totally fine but you can also use the much simpler syntax:
class base {
public:
virtual operator int&() { return x; };
virtual operator int() { return x; };
protected:
int x;
};
if you have a single variable that you'd wish to virtualize in your class.
The second declaration is only to avoid using a reference when you just need the value, while when assigning the reference is automatically chosen for you.
You can override these operators at will from classes derived from base.
class derived : public base {
public:
operator int() override { return x * 5; };
}

C++ How to return a private variable of derived class from base class object

I am trying to use functions that sets-gets a private variable of a derived class from base class objects. I think that is working for the void setC function but when i am trying to compile the getC function i get an error "Control reaches and non-void function". Is the whole concept possible?
class BASE{
int a, b;
public:
BASE(){}
BASE(int a, int b){
this->a = a;
this->b = b;
}
void setC(int){}
int getC(){}
};
class Derived : public BASE{
int c;
public:
void setC(int c) {
this->c = c;
}
int getC() {
return c;
}
};
int main(int argc, const char * argv[]) {
vector<BASE> d;
d.push_back(BASE(1,1));
d[0].setC(5);
cout << d[0].getC();
}
Implement getC and setC as (pure) virtual functions. And instantiate using derived class, like this:
class BASE {
public:
virtual void setC(int c) = 0;
virtual int getC() = 0;
};
class Derived : public BASE {
int c;
public:
virtual void setC(int c) { this->c = c; }
virtual int getC() { return c; }
};
void main() {
vector<BASE*> d;
d.push_back(new Derived());
d[0]->setC(5);
cout << d[0]->getC();
delete d[0];
}
PS. You got that error message because your BASE::getC doesn't return a value.
In order to get polymorphic behavior for getC(), you need the vector to be a vector of pointers, and declare getC and setC as virtual functions.
vector<BASE*> d;
Then
d.push_back(new DERIVED(1,1));
Will insert a new DERIVED object, and then
d[0]->getC()
will return the value from the derived object.

Do I need to override my virtual function using an inherited object (vs. base object)?

Say I have a base class Base, and two derived classes Derived: public Base and Derived2: public Base.
I have a purely virtual function
virtual int CompareByInsertKey( Base* item_in_list ) = 0;
In class Base. So it's a function that takes a pointer to a Base class.
I want my class Derived to overwrite this virtual function but I want it to take a pointer to a Derived class as an argument.
I also want my class Derived2 to overwrite this function but I want to to take a pointer to the Derived2 class as an argument.
Does it do this automatically if I keep the argument as Base* item_in_list because Derived and Derived2 are part Base classes?
If not, how do I do this?
Thanks, if you need me to post more information, please comment.
it takes pointers to the derived classes automatically because Derived * is implicitly convertible to Base *. You will have to implement it using Base in the function argument in order to override the function though.
virtual int CompareByInsertKey( Base* item_in_list ) override
{
Derived *ptr = dynamic_cast<Derived *>(item_in_list);
// ...
}
The compiler cannot do what are hoping to do.
struct Base
{
virtual int CompareByInsertKey( Base* item ) = 0;
};
struct Derived : Base
{
virtual int CompareByInsertKey( Base* item )
{
return 1;
}
};
struct Derived2 : Base
{
virtual int CompareByInsertKey( Base* item )
{
return 2;
}
};
int main()
{
Base* d1 = new Derived;
Base* d2 = new Derived2;
d1->CompareByInsertKey(d2); // This is a valid call
// but the argument is not of type Derived*
}
In order for the function signatures to match you have to use Base* in CompareByInsertKey. Unfortunately, that means you could pass a Derived as the parameter on a Derived2. Maybe templates would work better for your app.
Try running the following example.
#include "stdafx.h"
using namespace std;
class Base {
public:
string name;
Base(string n) : name(n) {}
virtual int CompareByInsertKey(Base* item_in_list) = 0;
virtual string Name() { return name; }
};
class Derived : public Base
{
public:
Derived() : Base("derived") {}
int CompareByInsertKey(Base *item_in_list)
{
cout << "In Derived, parameter is " << item_in_list->Name() << endl;
return 0;
}
};
class Derived2 : public Base
{
public:
Derived2() : Base("derived2") {}
int CompareByInsertKey(Base *item_in_list)
{
cout << "In Derived2, parameter is " << item_in_list->Name() << endl;
return 0;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Base * d2 = new Derived2();
Base *d = new Derived();
d2->CompareByInsertKey(d);
return 0;
}
KC
You can do this via a more complicated structure and knowledge about the types
class Base
{
public:
int publicComparator(Base* instance)
{
// you can use either typeid() or your on enum that you put in the
// hierarchy
if (typeid(this) == typeidf(instance))
{
return privateComparator(instance);
}
return ErrorCode;
}
private:
virtual privateComparator(Base *instance) = 0;
}
class Dervied : public Base {
private:
privateComparator(Base* instance) override {
// The assumption is privateComparator is only called when this cast
// can sucessfully be completed
Derived* derivedInstance = static_cast<Derived*>(instance);
// Do functionality
return result;
}
}
So with this if you call a->publicComparator(b)' only when the type ofaandb` are the same will the derived function actually be executed, is that the functionality that you where looking for ?

using dynamic_cast to point to other derived class object

I have the following scenario. There are two Base classes: Base1, Base2 and two derived classes: Derived, sysCommandExecutor which are derived as follows:
#include <iostream>
using namespace std;
class Base1 { virtual void dummy() {} };
class Base2 { virtual void dumy() {} };
class Derived: virtual public Base1, public Base2
{ int a; };
class sysCommandExecutor : public Base2
{
public:
int b;
Base1 *ptr;
void func(void);
};
void sysCommandExecutor::func(void)
{
Derived *d;
d = dynamic_cast<Derived *>(ptr);
if (d == NULL)
std::cout << "This is NULL" << std::endl;
else
{
// Call a function of Derived class
}
}
int main () {
try {
sysCommandExecutor * sys = new sysCommandExecutor;
sys->func();
return 0;
}
}
I want to call this function of "Derived" class inside func but the dynamic_cast fails.
I cannot create the function in the sysCommandExecutor class as that is someone else's code.
How to make the ptr pointer in sysCommandExecutor class to point to Derived class object??
Thanks in advance
You are referencing an uninitialized pointer ptr
if i change main to:
int main () {
sysCommandExecutor * sys = new sysCommandExecutor;
sys->ptr=new Derived;
sys->func();
delete dynamic_cast<Derived *>(sys->ptr);
delete sys;
return 0;
}
it works
you are missing a virtual dtor as well

dynamic_cast and static_cast in C++

I am quite confused with the dynamic_cast keyword in C++.
struct A {
virtual void f() { }
};
struct B : public A { };
struct C { };
void f () {
A a;
B b;
A* ap = &b;
B* b1 = dynamic_cast<B*> (&a); // NULL, because 'a' is not a 'B'
B* b2 = dynamic_cast<B*> (ap); // 'b'
C* c = dynamic_cast<C*> (ap); // NULL.
A& ar = dynamic_cast<A&> (*ap); // Ok.
B& br = dynamic_cast<B&> (*ap); // Ok.
C& cr = dynamic_cast<C&> (*ap); // std::bad_cast
}
the definition says:
The dynamic_cast keyword casts a datum from one pointer or reference
type to another, performing a runtime check to ensure the validity of the cast
Can we write an equivalent of dynamic_cast of C++ in C so that I could better understand things?
Here's a rundown on static_cast<> and dynamic_cast<> specifically as they pertain to pointers. This is just a 101-level rundown, it does not cover all the intricacies.
static_cast< Type* >(ptr)
This takes the pointer in ptr and tries to safely cast it to a pointer of type Type*. This cast is done at compile time. It will only perform the cast if the types are related. If the types are not related, you will get a compiler error. For example:
class B {};
class D : public B {};
class X {};
int main()
{
D* d = new D;
B* b = static_cast<B*>(d); // this works
X* x = static_cast<X*>(d); // ERROR - Won't compile
return 0;
}
dynamic_cast< Type* >(ptr)
This again tries to take the pointer in ptr and safely cast it to a pointer of type Type*. But this cast is executed at runtime, not compile time. Because this is a run-time cast, it is useful especially when combined with polymorphic classes. In fact, in certain cases the classes must be polymorphic in order for the cast to be legal.
Casts can go in one of two directions: from base to derived (B2D) or from derived to base (D2B). It's simple enough to see how D2B casts would work at runtime. Either ptr was derived from Type or it wasn't. In the case of D2B dynamic_cast<>s, the rules are simple. You can try to cast anything to anything else, and if ptr was in fact derived from Type, you'll get a Type* pointer back from dynamic_cast. Otherwise, you'll get a NULL pointer.
But B2D casts are a little more complicated. Consider the following code:
#include <iostream>
using namespace std;
class Base
{
public:
virtual void DoIt() = 0; // pure virtual
virtual ~Base() {};
};
class Foo : public Base
{
public:
virtual void DoIt() { cout << "Foo"; };
void FooIt() { cout << "Fooing It..."; }
};
class Bar : public Base
{
public :
virtual void DoIt() { cout << "Bar"; }
void BarIt() { cout << "baring It..."; }
};
Base* CreateRandom()
{
if( (rand()%2) == 0 )
return new Foo;
else
return new Bar;
}
int main()
{
for( int n = 0; n < 10; ++n )
{
Base* base = CreateRandom();
base->DoIt();
Bar* bar = (Bar*)base;
bar->BarIt();
}
return 0;
}
main() can't tell what kind of object CreateRandom() will return, so the C-style cast Bar* bar = (Bar*)base; is decidedly not type-safe. How could you fix this? One way would be to add a function like bool AreYouABar() const = 0; to the base class and return true from Bar and false from Foo. But there is another way: use dynamic_cast<>:
int main()
{
for( int n = 0; n < 10; ++n )
{
Base* base = CreateRandom();
base->DoIt();
Bar* bar = dynamic_cast<Bar*>(base);
Foo* foo = dynamic_cast<Foo*>(base);
if( bar )
bar->BarIt();
if( foo )
foo->FooIt();
}
return 0;
}
The casts execute at runtime, and work by querying the object (no need to worry about how for now), asking it if it the type we're looking for. If it is, dynamic_cast<Type*> returns a pointer; otherwise it returns NULL.
In order for this base-to-derived casting to work using dynamic_cast<>, Base, Foo and Bar must be what the Standard calls polymorphic types. In order to be a polymorphic type, your class must have at least one virtual function. If your classes are not polymorphic types, the base-to-derived use of dynamic_cast will not compile. Example:
class Base {};
class Der : public Base {};
int main()
{
Base* base = new Der;
Der* der = dynamic_cast<Der*>(base); // ERROR - Won't compile
return 0;
}
Adding a virtual function to base, such as a virtual dtor, will make both Base and Der polymorphic types:
class Base
{
public:
virtual ~Base(){};
};
class Der : public Base {};
int main()
{
Base* base = new Der;
Der* der = dynamic_cast<Der*>(base); // OK
return 0;
}
Unless you're implementing your own hand-rolled RTTI (and bypassing the system one), it's not possible to implement dynamic_cast directly in C++ user-level code. dynamic_cast is very much tied into the C++ implementation's RTTI system.
But, to help you understand RTTI (and thus dynamic_cast) more, you should read up on the <typeinfo> header, and the typeid operator. This returns the type info corresponding to the object you have at hand, and you can inquire various (limited) things from these type info objects.
More than code in C, I think that an english definition could be enough:
Given a class Base of which there is a derived class Derived, dynamic_cast will convert a Base pointer to a Derived pointer if and only if the actual object pointed at is in fact a Derived object.
class Base { virtual ~Base() {} };
class Derived : public Base {};
class Derived2 : public Base {};
class ReDerived : public Derived {};
void test( Base & base )
{
dynamic_cast<Derived&>(base);
}
int main() {
Base b;
Derived d;
Derived2 d2;
ReDerived rd;
test( b ); // throw: b is not a Derived object
test( d ); // ok
test( d2 ); // throw: d2 is not a Derived object
test( rd ); // ok: rd is a ReDerived, and thus a derived object
}
In the example, the call to test binds different objects to a reference to Base. Internally the reference is downcasted to a reference to Derived in a typesafe way: the downcast will succeed only for those cases where the referenced object is indeed an instance of Derived.
First, to describe dynamic cast in C terms, we have to represent classes in C.
Classes with virtual functions use a "VTABLE" of pointers to the virtual functions.
Comments are C++. Feel free to reformat and fix compile errors...
// class A { public: int data; virtual int GetData(){return data;} };
typedef struct A { void**vtable; int data;} A;
int AGetData(A*this){ return this->data; }
void * Avtable[] = { (void*)AGetData };
A * newA() { A*res = malloc(sizeof(A)); res->vtable = Avtable; return res; }
// class B : public class A { public: int moredata; virtual int GetData(){return data+1;} }
typedef struct B { void**vtable; int data; int moredata; } B;
int BGetData(B*this){ return this->data + 1; }
void * Bvtable[] = { (void*)BGetData };
B * newB() { B*res = malloc(sizeof(B)); res->vtable = Bvtable; return res; }
// int temp = ptr->GetData();
int temp = ((int(*)())ptr->vtable[0])();
Then a dynamic cast is something like:
// A * ptr = new B();
A * ptr = (A*) newB();
// B * aB = dynamic_cast<B>(ptr);
B * aB = ( ptr->vtable == Bvtable ? (B*) aB : (B*) 0 );
The following is not really close to what you get from C++'s dynamic_cast in terms of type checking but maybe it will help you understand its purpose a little bit better:
struct Animal // Would be a base class in C++
{
enum Type { Dog, Cat };
Type type;
};
Animal * make_dog()
{
Animal * dog = new Animal;
dog->type = Animal::Dog;
return dog;
}
Animal * make_cat()
{
Animal * cat = new Animal;
cat->type = Animal::Cat;
return cat;
}
Animal * dyn_cast(AnimalType type, Animal * animal)
{
if(animal->type == type)
return animal;
return 0;
}
void bark(Animal * dog)
{
assert(dog->type == Animal::Dog);
// make "dog" bark
}
int main()
{
Animal * animal;
if(rand() % 2)
animal = make_dog();
else
animal = make_cat();
// At this point we have no idea what kind of animal we have
// so we use dyn_cast to see if it's a dog
if(dyn_cast(Animal::Dog, animal))
{
bark(animal); // we are sure the call is safe
}
delete animal;
}
A dynamic_cast performs a type checking using RTTI. If it fails it'll throw you an exception (if you gave it a reference) or NULL if you gave it a pointer.
There are no classes in C, so it's impossible to to write dynamic_cast in that language. C structures don't have methods (as a result, they don't have virtual methods), so there is nothing "dynamic" in it.
No, not easily. The compiler assigns a unique identity to every class, that information is referenced by every object instance, and that is what gets inspected at runtime to determine if a dynamic cast is legal. You could create a standard base class with this information and operators to do the runtime inspection on that base class, then any derived class would inform the base class of its place in the class hierarchy and any instances of those classes would be runtime-castable via your operations.
edit
Here's an implementation that demonstrates one technique. I'm not claiming the compiler uses anything like this, but I think it demonstrates the concepts:
class SafeCastableBase
{
public:
typedef long TypeID;
static TypeID s_nextTypeID;
static TypeID GetNextTypeID()
{
return s_nextTypeID++;
}
static TypeID GetTypeID()
{
return 0;
}
virtual bool CanCastTo(TypeID id)
{
if (GetTypeID() != id) { return false; }
return true;
}
template <class Target>
static Target *SafeCast(SafeCastableBase *pSource)
{
if (pSource->CanCastTo(Target::GetTypeID()))
{
return (Target*)pSource;
}
return NULL;
}
};
SafeCastableBase::TypeID SafeCastableBase::s_nextTypeID = 1;
class TypeIDInitializer
{
public:
TypeIDInitializer(SafeCastableBase::TypeID *pTypeID)
{
*pTypeID = SafeCastableBase::GetNextTypeID();
}
};
class ChildCastable : public SafeCastableBase
{
public:
static TypeID s_typeID;
static TypeID GetTypeID()
{
return s_typeID;
}
virtual bool CanCastTo(TypeID id)
{
if (GetTypeID() != id) { return SafeCastableBase::CanCastTo(id); }
return true;
}
};
SafeCastableBase::TypeID ChildCastable::s_typeID;
TypeIDInitializer ChildCastableInitializer(&ChildCastable::s_typeID);
class PeerChildCastable : public SafeCastableBase
{
public:
static TypeID s_typeID;
static TypeID GetTypeID()
{
return s_typeID;
}
virtual bool CanCastTo(TypeID id)
{
if (GetTypeID() != id) { return SafeCastableBase::CanCastTo(id); }
return true;
}
};
SafeCastableBase::TypeID PeerChildCastable::s_typeID;
TypeIDInitializer PeerChildCastableInitializer(&PeerChildCastable::s_typeID);
int _tmain(int argc, _TCHAR* argv[])
{
ChildCastable *pChild = new ChildCastable();
SafeCastableBase *pBase = new SafeCastableBase();
PeerChildCastable *pPeerChild = new PeerChildCastable();
ChildCastable *pSameChild = SafeCastableBase::SafeCast<ChildCastable>(pChild);
SafeCastableBase *pBaseToChild = SafeCastableBase::SafeCast<SafeCastableBase>(pChild);
ChildCastable *pNullDownCast = SafeCastableBase::SafeCast<ChildCastable>(pBase);
SafeCastableBase *pBaseToPeerChild = SafeCastableBase::SafeCast<SafeCastableBase>(pPeerChild);
ChildCastable *pNullCrossCast = SafeCastableBase::SafeCast<ChildCastable>(pPeerChild);
return 0;
}
static_cast< Type* >(ptr)
static_cast in C++ can be used in scenarios where all type casting can be verified at compile time.
dynamic_cast< Type* >(ptr)
dynamic_cast in C++ can be used to perform type safe down casting. dynamic_cast is run time polymorphism. The dynamic_cast operator, which safely converts from a pointer (or reference) to a base type to a pointer (or reference) to a derived type.
eg 1:
#include <iostream>
using namespace std;
class A
{
public:
virtual void f(){cout << "A::f()" << endl;}
};
class B : public A
{
public:
void f(){cout << "B::f()" << endl;}
};
int main()
{
A a;
B b;
a.f(); // A::f()
b.f(); // B::f()
A *pA = &a;
B *pB = &b;
pA->f(); // A::f()
pB->f(); // B::f()
pA = &b;
// pB = &a; // not allowed
pB = dynamic_cast<B*>(&a); // allowed but it returns NULL
return 0;
}
For more information click here
eg 2:
#include <iostream>
using namespace std;
class A {
public:
virtual void print()const {cout << " A\n";}
};
class B {
public:
virtual void print()const {cout << " B\n";}
};
class C: public A, public B {
public:
void print()const {cout << " C\n";}
};
int main()
{
A* a = new A;
B* b = new B;
C* c = new C;
a -> print(); b -> print(); c -> print();
b = dynamic_cast< B*>(a); //fails
if (b)
b -> print();
else
cout << "no B\n";
a = c;
a -> print(); //C prints
b = dynamic_cast< B*>(a); //succeeds
if (b)
b -> print();
else
cout << "no B\n";
}
dynamic_cast uses RTTI. It can slow down your application, you can use modification of the visitor design pattern to achieve downcasting without RTTI http://arturx64.github.io/programming-world/2016/02/06/lazy-visitor.html