Discussion:
abstract class and some questions
(too old to reply)
Todor Atanasov
2014-02-07 21:08:59 UTC
Permalink
Hi guys, it is me again.
I have entered more deep in C++ and I have reached at a point at which my knowledge is a bit....let say limited:)

This is what I want to do:

To have a CArray (just a array, could be vector, list) with objects, that can be a different classes, but all of them have one method process;

In java this is done with abstract class and then type cast, for example

abstract class Base
{
public abstract void process();
}
class Floor extends Base
{
public void process();
}

and for the loop I can have

List<Floor> objects = new ArrayList<Floor>();
for (int i = 0; i < objects.size(); i++)
{
objects.get(i).process();
}

but how to make that in C++

I can make the two classes with Base having a pure virtual method process.

But how to make the cast of Base to Floor. To me that is impossible because how would the compile knows how to do it.

The purpose of all that is that I need to process objects from different classes and I don't know how to do it.
Cholo Lennon
2014-02-10 14:39:19 UTC
Permalink
Post by Todor Atanasov
Hi guys, it is me again.
I have entered more deep in C++ and I have reached at a point at which my knowledge is a bit....let say limited:)
To have a CArray (just a array, could be vector, list) with objects, that can be a different classes, but all of them have one method process;
In java this is done with abstract class and then type cast, for example
abstract class Base
{
public abstract void process();
}
class Floor extends Base
{
public void process();
}
and for the loop I can have
List<Floor> objects = new ArrayList<Floor>();
for (int i = 0; i < objects.size(); i++)
{
objects.get(i).process();
}
but how to make that in C++
I can make the two classes with Base having a pure virtual method process.
But how to make the cast of Base to Floor. To me that is impossible because how would the compile knows how to do it.
The purpose of all that is that I need to process objects from different classes and I don't know how to do it.
To perform a downcasting in C++ you could use static_cast or
dynamic_cast. Use the first one *only* if you have stored the same type
of object in your container, otherwise use dynamic_cast (dynamic_cast is
C++ counterpart to Java instanceof)

// static_cast with pointers
Derived* pDer = static_cast<Derived*>(pointer_to_base_obj);

// static_cast with references
Derived& rDer = static_cast<Derived&>(base_obj);

// dynamic_cast with pointers
if (Derived* pDer = dynamic_cast<Derived*>(pointer_to_base_obj)) {
// Use pDer
...
}
else {
// pointer_to_base_obj is not of Derived type
}

// dynamic_cast with references
try {
Derived& rDer = dynamic_cast<Derived&>(base_obj);

// Use rDer
...
}
catch (std::bad_cast& e) {
// base_obj is not of Derived type
...
}

In order to use dynamic_cast your base class has to have at least one
virtual function.


Regards
--
Cholo Lennon
Bs.As.
ARG
Stuart
2014-02-10 16:54:18 UTC
Permalink
Post by Todor Atanasov
Hi guys, it is me again.
I have entered more deep in C++ and I have reached at a point at which my knowledge is a bit....let say limited:)
To have a CArray (just a array, could be vector, list) with objects, that can be a different classes, but all of them have one method process;
In java this is done with abstract class and then type cast, for example
abstract class Base
{
public abstract void process();
}
class Floor extends Base
{
public void process();
}
and for the loop I can have
List<Floor> objects = new ArrayList<Floor>();
for (int i = 0; i < objects.size(); i++)
{
objects.get(i).process();
}
but how to make that in C++
Here you go:

#include <vector>
#include <iostream>

// Note: No keyword "interface" in C++, interfaces are just
// classes that only contain pure virtual methods.
class Base
{
public:
// Note: The keyword "virtual" is needed in C++. In Java
// all methods are virtual. The "= 0" is the
// C++ way of saying that the method is abstract.
virtual void process() = 0;
};

class Floor : public Base
{
public:
virtual void process() {
std::cout << "I'm a floor, and I am being processed.";
}
};

int main () {

std::vector<Base*> container;
container.push_back(new Floor());

for (int i = 0; i < container.size(); i++)
{
container[i]->process();
}
}

Disclaimer: I intentionally left out anything related to memory
management. Above program will leak memory. A proper production code
interface should have a virtual destructor as well (there are very very
rare cases where this is not necessary), so that the objects can be
cleaned up properly.
Post by Todor Atanasov
I can make the two classes with Base having a pure virtual method process.
But how to make the cast of Base to Floor. To me that is impossible because how would the compile knows how to do it.
You don't need to cast the object to the actual sub-type in order to
access their methods. That's what virtual method have been designed for.
Post by Todor Atanasov
The purpose of all that is that I need to process objects from different classes and I don't know how to do it.
Regards,
Stuart
Todor Atanasov
2014-02-10 19:10:14 UTC
Permalink
Post by Stuart
Post by Todor Atanasov
Hi guys, it is me again.
I have entered more deep in C++ and I have reached at a point at which my knowledge is a bit....let say limited:)
To have a CArray (just a array, could be vector, list) with objects, that can be a different classes, but all of them have one method process;
In java this is done with abstract class and then type cast, for example
abstract class Base
{
public abstract void process();
}
class Floor extends Base
{
public void process();
}
and for the loop I can have
List<Floor> objects = new ArrayList<Floor>();
for (int i = 0; i < objects.size(); i++)
{
objects.get(i).process();
}
but how to make that in C++
#include <vector>
#include <iostream>
// Note: No keyword "interface" in C++, interfaces are just
// classes that only contain pure virtual methods.
class Base
{
// Note: The keyword "virtual" is needed in C++. In Java
// all methods are virtual. The "= 0" is the
// C++ way of saying that the method is abstract.
virtual void process() = 0;
};
class Floor : public Base
{
virtual void process() {
std::cout << "I'm a floor, and I am being processed.";
}
};
int main () {
std::vector<Base*> container;
container.push_back(new Floor());
for (int i = 0; i < container.size(); i++)
{
container[i]->process();
}
}
Disclaimer: I intentionally left out anything related to memory
management. Above program will leak memory. A proper production code
interface should have a virtual destructor as well (there are very very
rare cases where this is not necessary), so that the objects can be
cleaned up properly.
Post by Todor Atanasov
I can make the two classes with Base having a pure virtual method process.
But how to make the cast of Base to Floor. To me that is impossible because how would the compile knows how to do it.
You don't need to cast the object to the actual sub-type in order to
access their methods. That's what virtual method have been designed for.
Post by Todor Atanasov
The purpose of all that is that I need to process objects from different classes and I don't know how to do it.
Regards,
Stuart
LOL thank you VERY MUCH. I really didn't expect that detailed explanation. Too bad Google Groups don't have rating, you sir would have gotten 10tp :)
Continue reading on narkive:
Loading...