473,699 Members | 2,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"dynamic_ca st" with instance of an abstract class ?!?

Hi @all,

I've got an interesting problem.

These are my classes:

----------------------

class fooBase
{
virtual func();
}

class fooA : public fooBase
{
virtual func();
};

class fooB : public fooBase
{
virtual func1() = 0;
virtual func2();
};

class fooB2 : public fooB
{
virtual func1();
}

----------------------

// Template class
class myFooBase
{
fooBase *m_pPtr;
};

template <class T>
class myFoo : public myFooBase
{
public:

myFoo(fooBase *p_pPtr) :
m_pPtr(p_pPtr)
{
}

bool isCompatible(my Foo *p_p)
{
// Check, if p_p is compatible (dynamic_castab le) to T

// Possible way:
T *TempPtr = new T();
if (dynamic_cast<T *> (p_p->m_pPtr))
}
};

----------------------

int main()
{
fooB2 *TEST1 = new fooB2();

myFoo<fooB2> test1(dynamic_c ast<fooBase *>(new fooB2()));
myFoo<fooBase> test2(dynamic_c ast<fooBase *>(new fooBase()));

// Test: OK
test2.isCompati ble(&test1);

// Cannot create, because of new T() => fooB is an abstract class
myFoo<fooB> test3(dynamic_c ast<fooBase *>(new fooB3()))
}

----------------------

As you see, I need another way to check, if to base classes are
dynamically castable or have the same base classes.

Jul 23 '05 #1
19 4059
Use RTTI (typeid operator).

Jul 23 '05 #2
// Template class
class myFooBase
{
fooBase *m_pPtr;
};

template <class T>
class myFoo : public myFooBase
{
public:

myFoo(fooBase *p_pPtr) :
m_pPtr(p_pPtr)
{
}

bool isCompatible(my Foo *p_p)
{
// Check, if p_p is compatible (dynamic_castab le) to T

// Possible way:
T *TempPtr = new T();
if (dynamic_cast<T *> (p_p->m_pPtr))
}
};


Why do you want to leak memory? Why "new T()"? The
"if dynamic_cast<>( )" alone will do just fine. BTW: if you
just write "myFoo *p_p" in the parameter list it actually
means "myFoo<T> *p_p" - i.e. if you only assign Ts (or derived)
to "m_pPtr" it will always be compatible!
I think what you're trying to do would read something like:

// inside template <class T> class myFoo ...:
template<class U>
bool isCompatible( myFoo<U> const & other )
{
return !! dynamic_cast<T* >(other.m_pPtr) ;
}
Jul 23 '05 #3
How to use typeid to check if a class A is the same or a derivation of
class B?

I have explained my problem wrong... I'm sorry for my bad English!

I have a better example:

template <class A, class B>
bool testDerivation( )
{
// check, if A is the same or a derivation of class B

// typeid(A) == typeid(B) would be possible.... but only for
Equality... what about derivation?
}

How, to implement such a function?
As I said, I cannot use dynamic_cast, because B (or A) can be abstract.
Because of this I cannot create an instance.

Jul 23 '05 #4
You can also put dynamic_cast within an if/else structure.

eg for testing if a baseClassInstan ce is 'castable' to a CDerivedClass
type :
if(CDerivedClas s* der == dynamic_cast<CD erivedClass*>(b aseClassInstanc e)){
der->doSomething( );
}
else if(/*testing to other type*/){
}

//and so on...
Jan
Jul 23 '05 #5
My code is terribly wrong!!!! When I was constructing my code, I was
sleeping...
Normally I can use dynamic_cast<> perfectly...

I have a template class with a pointer

template <class T>
T *myVar;

Now I want to know (in runtime), if this T is for example an base class
of XYZ...

template <class XYZ>
XYZ *myVar2;

BUT... I cannot use dynamic_cast for this... Because dynamic_cast needs
myVar! I want to use T...

Ohhhhh, my English... sorry for that... nobody knows what I want to do?

Jul 23 '05 #6
tt******@gmx.de wrote:
My code is terribly wrong!!!! When I was constructing my code, I was
sleeping...
Normally I can use dynamic_cast<> perfectly...

I have a template class with a pointer

template <class T>
T *myVar;

Now I want to know (in runtime), if this T is for example an base class
of XYZ...

template <class XYZ>
XYZ *myVar2;

BUT... I cannot use dynamic_cast for this... Because dynamic_cast needs
myVar! I want to use T...

Ohhhhh, my English... sorry for that... nobody knows what I want to do?


T and XYZ are both static types, so where's the runtime thing?
This is really starting to confuse me. Why don't you just tell us what
you are trying to do, and why (=what do you need this functionality for)
and maybe we can find a solution.
Jul 23 '05 #7
Ok... what follows now is not exactly what I want to do. But if anyone
can solve this problem, I can solve my own problem!

I have two class types: class A and B. The exact type of those are
defined in a template: e.g. template <class A>, template<class B>

Now I want to know, if A is a derivation of B!
Of course I could use dynamic_cast<>. ....
Normally I could use dynamic_cast and one instances: A* classA = new
A(); dynamic_cast<B *>(classA) etc.
BUT I cannot of an instance for an abstract class... "Aabstract* classA
= new Aabstract()" fails...

Jul 23 '05 #8
tt******@gmx.de wrote:
Ok... what follows now is not exactly what I want to do. But if anyone
can solve this problem, I can solve my own problem! I have two class types: class A and B. The exact type of those are
defined in a template: e.g. template <class A>, template<class B>

Now I want to know, if A is a derivation of B!


Since std::type_info offers no way to check the hierarchy, and nobody
else provided a good answer yet, I suppose, that this can't be done in
general.
But I cannot imagine, what the following is good for:

template <class A, class B>
void f()
{
if ( typeid(A).has_b ase(typeid(B)) ) // fictitious!
{
// you can't create an A or B (might be abstract)
// you can't convert an A (B might be a private base)
}
else
{
// A might otherwise be perfectly convertible to B anyway
}
}

Can you name a single C++ statement, that is guaranteed to work in one
case, but might fail in the other case? I don't see one.
Why don't you just post your *real* problem?
Ralph

Jul 23 '05 #9
* Ralph D. Ungermann:
* tt******@gmx.de:

Now I want to know [presumably at compile time], if A is a
derivation of B!


Since std::type_info offers no way to check the hierarchy, and nobody
else provided a good answer yet, I suppose, that this can't be done in
general.


Andrei Alexandrescu presented one solution to this problem in his "Modern
C++ Design". It's based on having a function with one overload that
accepts a B, and one that accepts anything else. Then you pseudo-call it
(within a sizeof-expression) with an A, and check which one is selected.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #10

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

Similar topics

6
6020
by: Jordi Vilar | last post by:
Hi All, Is there a way to dynamic_cast a pointer to a type defined by a type_info*? something like: type_info *info_of_type_to_cast = typeid(type_to_cast); type_to_cast *casted = really_dynamic_cast<info_of_type_to_cast>(my_data_ptr);
11
2097
by: Joseph Turian | last post by:
Fellow hackers, I have a class BuildNode that inherits from class Node. Similarly, I have a class BuildTree that inherits from class Tree. Tree includes a member variable: vector<Node> nodes; // For clarity, let this be "orig_nodes" BuildTree includes a member variable:
2
2814
by: Torsten Landschoff | last post by:
Hi there, I am having an interesting C++ problem which I currently am working around but I don't like this so perhaps somebody has a better idea. Basically I am parsing (rather: scanning) a specification (Word document, ugh) and want to generate code from the information I gather, mostly argument passing stuff. The types I have to (un)marshal are currently represented by the
53
4574
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document, and at the end there is even pure mindstorming text left in, but already I think it can be very useful. <url: http://home.no.net/dubjai/win32cpptut/special/pointers/preview/pointers_01__alpha.doc.pdf>.
3
1900
by: craig | last post by:
Given two existing but different classes OldA and OldB (that can not be made to derive from any new base class); is there a way to make them both "observer" objects so that they can be put in one central list and updated thru a common interface. (i.e. observer->update( ..))? Potential solution 1 (multiple inheritence): make a small new observer class, and two new classes: NewA: derived from OldA, and Observer,.. and NewB: derived from...
17
2306
by: nicolas.hilaire | last post by:
Hi all, i've read this article http://msdn2.microsoft.com/en-us/library/85af44e9.aspx who first interest me much. I've translated it to use generic instead of template : generic < typename T, typename U > Boolean isinst(U u) {
0
2676
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never looked for one on the net (I did browse the boost library though). It doesn't matter right now, anyway. To put it simply, Abstract Iterator is mainly a wrapper class. It helps
18
3637
by: desktop | last post by:
I have 3 types of objects: bob1, bob2 and bob3. Each object is identified by a unique ID which gets returned by the function getId(). All bobs are descendants from class BaseBob which is an abstract class that has the virtual function getId(). I then have a function that prints a special string when a bob meets another bob (all combinations of bobs has to meet and since the are schizophrenic they can also meet themselves):
8
1642
by: Oliver Graeser | last post by:
Hi All, I'm coming from Java to C++ and this is one of the very last problems I have so far... In Java, if I have, say, a class SISNode that extends NetworkNode, I can have a function that returns a NetworkNode but I can assure the compiler that it is in fact a SISNode and therefore call the method getStatus() that only a SISNode has. Like SISnode s,t; NetworkNode n;
0
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.