473,785 Members | 2,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Base Class pointers to Derived Classes calling functions.

Hey. I have a base class (SPRITE), and using this base class I have
derived a large number of derived classes (PERSON, BULLET, MISSILE,
etc.). Now, at a certain point in my program, I have a pair of
pointers, where each is a pointer to the base class (each is a SPRITE
*). I know that each of these pointers actually points to one of the
derived classes, even though the type of the pointer is SPRITE *, but
I don't know which derived class it points to. I would like to write a
set of functions that do something different depending on what
combination of derived classes the two pointers point to (i.e. I want
one function for a PERSON * and BULLET *, a second function for a
PERSON * and MISSILE *, a third for BULLET * and MISSILE *, etc.).

How can I write these functions, and how do I call them, so that the
correct function is called given the two pointers?

I hope this is clear...
Dec 30 '07
12 2872
On Dec 31, 7:05 pm, Ron Natalie <r...@spamcop.n etwrote:
Rahul wrote:
dynamic_cast would work only with classes having virtual functions,
typeid operator would
be more appropriate...

typeid has the same restrictions. If the class isn't polymorphic
(virtual) functions, the runtime typing info ain't there.
I meant the compilation error, one gets with dynamic_cast for the
following cases,

class A
{
};

class B : public A
{
};

int main()
{
B *ptr1;
A *ptr2 = new B();
ptr1 = dynamic_cast<B* >(ptr2); // gives a compilation
error
if(ptr1 == NULL)
printf("fail\n" );
else
printf("success \n");
return(0);
}

but typeid doesn't give any such error,

A *ptr = new B();
cout<<typeid(pt r).name()<<endl ;
cout<<typeid(*p tr).name()<<end l; // Yes this would give A
without any virtual functions in A, but atleast works...
Dec 31 '07 #11
Rahul wrote:
On Dec 31, 7:05 pm, Ron Natalie <r...@spamcop.n etwrote:
>Rahul wrote:
>>dynamic_cas t would work only with classes having virtual functions,
typeid operator would
be more appropriate...
typeid has the same restrictions. If the class isn't polymorphic
(virtual) functions, the runtime typing info ain't there.

I meant the compilation error, one gets with dynamic_cast for the
following cases,

ptr1 = dynamic_cast<B* >(ptr2); // gives a compilation
error
If your compiler gives a compilation error there it is seriously busted.
Dynamic cast has a whole slew of cases that apply EVEN if the classes
aren't polymorphic.

A warning perhaps, but the program is well formed.
Dec 31 '07 #12
On Dec 31 2007, 5:20 pm, Ron Natalie <r...@spamcop.n etwrote:
Rahul wrote:
On Dec 31, 7:05 pm, Ron Natalie <r...@spamcop.n etwrote:
Rahul wrote:
>dynamic_cast would work only with classes having virtual functions,
typeid operator would
be more appropriate...
typeid has the same restrictions. If the class isn't polymorphic
(virtual) functions, the runtime typing info ain't there.
I meant the compilation error, one gets with dynamic_cast for the
following cases,
ptr1 = dynamic_cast<B* >(ptr2); // gives a compilation
error
If your compiler gives a compilation error there it is
seriously busted. Dynamic cast has a whole slew of cases that
apply EVEN if the classes aren't polymorphic.
True, but pointer to base to pointer to derived isn't one of
them. The above code should result in a compiler error.
(Remember that ptr2 has type A*, and that A is a base class of
B.)

The reverse, of course, should work.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 1 '08 #13

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

Similar topics

6
3168
by: vijay | last post by:
Hello I wanted to understand a contradictory design of C++ class A {public: virtual void f(){ cout<<" base f"<<endl; } }; class B:public A {
2
1990
by: Luca | last post by:
Hi, I have a quite complex question to ask you: I have defined a base class where I would like to have a map holding pointers to member functions defined in derived classes. To be more precise I would like my base class to have the following member: map<string, pointer_to_member_function> myClassMap;
9
4996
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class pointer which points to an instance of a derived class, but when I pass that base class pointer into a function, it can't access the derived object's public functions. Although, the base class pointer does call the appropriate virtual function...
10
3334
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
5
1962
by: Michael | last post by:
Hi, Could you tell me whether the following two statement are the same? Derived class is derived from Base class. 1. Base* ptr1; 2. Derived * ptr2; Does it mean ptr1 is the same as ptr2?
5
3456
by: Scott | last post by:
Hi All, Am I correct in assuming that there is no way to have a base pointer to an object that uses multiple inheritance? For example, class A { /* ... */ }; class B { /* ... */ };
47
4040
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever support multiple inheritance. In C++ for instance I noticed that the compiler flags an error if you use the "ref" keyword on a class with multiple base classes. This supports the above quote. However, under the "CodeClass2.Bases" property (part...
15
3537
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and you want to make a deque which can contain any objects of any of those types. Normally what you would have to do is to make a deque or vector of pointers of the base class type and then allocate each object dynamically with 'new' and store the...
1
2527
by: Rune Allnor | last post by:
Hi all. I am sure this is an oldie, but I can't find a useful suggestion on how to solve it. I have a class hierarchy of classes derived from a base class. I would like to set up a vector of smart pointers to the base class, and access the virtual functions of any class in the hierarchy through the virtual functions.
0
9645
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
10327
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10151
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
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
9950
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
7499
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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.