473,387 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

upcasting of baseclasspointer

Dear all,

I think I have a very common problem:

How shall I upcast an object out of a heterogeneous set. Even worse, I want
to avoid the built in C++ RTTI for some reasons. So I thought every object
which could be upcastet shall get an ID. If an upcast is needed I use a
switch table or a if_else_if_else construct. The switch table is quite
nicer, but while I want to upcast more than one object at a time it needs
more accurate work for the IDs to get unique cases.

Until now, I use an extra namespace where my IDs are stored. The only reason
you can find inside the macro ADD_CASES I wrote: I use the same name for the
class and for the ID. This macro is written to avoid the repepetition of the
possible cases (which must not match all permutations, see not allowed
case).

I am sorry to post so much code. The foo-function is not allowed to be
virtual. Typelist may not be a good idea (not sure), because my real types
are templated.

Coming to my questions:

How do you solve these kinds of problems ?
Any suggestions for my code ? Did I overlook some ugly sideeffects ?

Regards,
Patrick

#include <iostream>
#include <vector>
#include <utility>

// Polymorphic class hierachy
namespace ID {
const int A = 0;
const int B = 1;
}

class AB {
public:
virtual ~AB() {}
virtual int ID() = 0;
};

class A : public AB {
public:
A() {
std::cout << "Constructor A called" << std::endl;
}
virtual ~A() {
std::cout << "Destructor A called" << std::endl;
}
virtual int ID() { return ID::A; }
void foo() {
std::cout << "Foo A called" << std::endl;
}
};

class B : public AB {
public:
B() {
std::cout << "Constructor B called" << std::endl;
}
virtual ~B() {
std::cout << "Destructor B called" << std::endl;
}
virtual int ID() { return ID::B; }
void foo() {
std::cout << "Foo B called" << std::endl;
}
};

// 2.Polymorphic class hierachy
namespace ID {
const int C = 0;
const int D = 2;
}

class CD {
public:
virtual ~CD() {}
virtual int ID() = 0;
};

class C : public CD {
public:
C() {
std::cout << "Constructor C called" << std::endl;
}
virtual ~C() {
std::cout << "Destructor C called" << std::endl;
}
virtual int ID() { return ID::C; }
void foo() {
std::cout << "Foo C called" << std::endl;
}
};

class D : public CD {
public:
D() {
std::cout << "Constructor D called" << std::endl;
}
virtual ~D() {
std::cout << "Destructor D called" << std::endl;
}
virtual int ID() { return ID::D; }
void foo() {
std::cout << "Foo D called" << std::endl;
}
};
// END OF CLASS DEFINITIONS
#define SWITCH_FOO_CASE(CLASS1,CLASS2,BASE1,BASE2) \
case(ID::CLASS1 + ID::CLASS2) : \
static_cast<CLASS1 *>(BASE1)->foo(); \
static_cast<CLASS2 *>(BASE2)->foo(); \
break;

// only here the order for the table is from importance
#define ADD_CASES(CASE,BASE1,BASE2) \
CASE(A,C,BASE1,BASE2) \
CASE(B,C,BASE1,BASE2) \
CASE(B,D,BASE1,BASE2)
int main() {

// CASE 1
// ONLY ONE CLASS
std::vector<AB *> vAB;

// storage
vAB.push_back(new A);
vAB.push_back(new B);

// easy example for switch
for (size_t i=0;i<vAB.size();++i)
switch (vAB[i]->ID()) {
case(0) : static_cast<A *>(vAB[i])->foo();
break;
case(1) : static_cast<B *>(vAB[i])->foo();
break;
default : std::cout << "Ups....something is wrong" << std::endl;
}

for (size_t i=0;i<vAB.size();++i) if (vAB[i]) delete vAB[i];
// CASE 2
// TWO DIFFERENT CLASSES
std::vector< std::pair<AB *,CD *> > vP;

// storage
vP.push_back( std::pair<AB *,CD *>(new A,new C) );
vP.push_back( std::pair<AB *,CD *>(new A,new D) );
vP.push_back( std::pair<AB *,CD *>(new B,new C) );
vP.push_back( std::pair<AB *,CD *>(new B,new D) );

// more complicated switch constructed by macro
for (size_t i=0;i<vP.size();++i)
switch (vP[i].first->ID() + vP[i].second->ID()) {
ADD_CASES(SWITCH_FOO_CASE,vP[i].first,vP[i].second)
default : std::cout << "This case is not allowed" << std::endl;
}

for (size_t i=0;i<vP.size();++i) {
if (vP[i].first) delete vP[i].first;
if (vP[i].second) delete vP[i].second;
}

return 0;
}
Jul 22 '05 #1
2 2362
Something like

class Base{
public:
virtual void *CastFn() const = 0;
};
#define MYUPCAST(a) \
public: static a *Upcast(Base *pbase) \
{
\
if( pbase->CastFn() == Upcast ) \
return static_cast<a *>(pbase); \
return 0;
\
}
\
virtual void *CastFn(){ return Upcast; } \
class A: public Base
{
MYUPCAST(A)
...
}

class B: public Base
{
MYUPCAST(B)
...
}

Base *p;

A *pa = A::Upcast(p);
if( pa )
{
// It's an A
}

B *pb = B::Upcast(p)
if(pb)
{
// It's a B
}

Pro: The compiler gives each class it's own, unique ID, by providing a
pointer to a static function.
Contra: The classes all have to be derived from base

Isaac


Jul 22 '05 #2
Hello Isaac,

I like the solution with the pointer to a static function.

The problem with this solution is, that I do not find a way to integrate
this in a "switch" table. The case with the ifs works fine but in my program
I have 54 different cases....and it is getting more. You could assume poor
design :-) ... but I am trying.
#define MYUPCAST(a) \
public: static a *Upcast(Base *pbase) \
{
\
if( pbase->CastFn() == Upcast ) \
return static_cast<a *>(pbase); \
return 0;
\
}
\
virtual void *CastFn(){ return Upcast; } \


I think the last line should be:
virtual void *CastFn() const { return Upcast; }

And in my OP I forgotten quite more const:
virtual int ID() = 0;

should be
virtual int ID() const = 0;
and as well in all inherited classes as well.

Thanks a lot,
Patrick

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lawrence | last post by:
Lets suppose I have an class called DatastoreSelect. It has a method called getNextRow(). This method is not abstract, it is fully implemented. It also has a method called setInfoToBeSought, which...
0
by: lawrence | last post by:
In Java one is supposed to upcast an object to its interface whenever appropriate: MyInterface customers = new MySpecificClass(); The idea is that if later one wants to change the specific...
2
by: Judith | last post by:
Hi there everyone I've got: class A { ... }; class B : public A { ... }
4
by: juli jul | last post by:
Hello, I did the following with Class A which is a parent and B inherits from it: B b1=new B(); A a1=new A(); a1=b1; Is it an upcasting in c#? Thank you!
4
by: Amod | last post by:
I want to call a child class constructor using the base class instance ... whats the xact mechanism to perform this function ? Regards, Amod
5
by: Mark | last post by:
I can't seem to get this subclass to upcast to a baseclass... see comments below #include <cstdlib> #include <iostream> #include "Base.h" #include "Sub1.h" using namespace std;
3
by: mati-006 | last post by:
Hi, I think the code will be the best way to explain what I mean: #include "arglib/arg_shared.h" class base { public: base() {} virtual ~base() {} };
4
by: majsta | last post by:
Hello, I have the following code. #include <vector> #include <iostream> class Foo { public: Foo(){} virtual void print() const { std::cout << "foo" << std::endl;} };
0
by: Juha Nieminen | last post by:
If I'm not mistaken, general OOP wisdom says that upcasting should usually be avoided if possible. I have a situation, however, where I can't think of a better way than upcasting for this specific...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.