G
G
Grigory Dikiy2015-04-08 18:38:12
OOP
Grigory Dikiy, 2015-04-08 18:38:12

How to explicitly cast a base class to a child?

void SplitString(int choice)
{
    char string[100];

    system("clear");
    LevelHead("Тестирование","Строка-Идентификатор","Сложение строк");
    cout << "Введите значение второго операнда" << endl;
    cin >> string;

    Str_Indef strIndef(string);

    strIndef = strIndef + (Str_Indef)pointClass[strArray[choice]];

    strIndef.Print();
    Pause();
}

/home/dikiigr/Desktop/Education/JVU/Laba3/INHERIT/main.cpp|205|error: invalid conversion from 'Stroka*' to 'char' [-fpermissive]|
Hello, I need to cast a parent class to a child class in order to use the overloaded child class operator. I don't know how to implement it. Gentlemen experts, I ask for your help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2015-04-08
@frilix

i need to cast parent class to child class

You should probably provide a pointer or a link.
Any of the 4 cast operators (except const_cast), in decreasing order of kosherness (and if there are no virtual functions in A, then three, starting with static_cast):
class A
{
public:
    virtual ~A() {}
};
class B : public A
{
};

A *pa = ...;
B *pb1 = dynamic_cast<B*>(pa);
B *pb2 = static_cast<B*>(pa);
B *pb3 = (B *)pa;
B *pb4 = reinterpret_cast<B*>(pa);

X
xibir, 2015-04-08
@xibir

I suspect you need this:

#include <iostream>

class Base
{
public:
   virtual void foobar(){}
   // или:
   //virtual void foobar() = 0;
   virtual ~Base(){}
};

class Child : public Base
{
public:
    virtual ~Child() {}
    void foobar() override { std::cout << "foobar" << std::endl; }
};

int main()
{
    Base *b = new Child;
    b->foobar();
    delete b;
    return 0;
}

No typecasts.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question