473,402 Members | 2,064 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,402 software developers and data experts.

Problem with Cast

Hi!
I have the following scenario:

class QWidget
{}

class GeneratedClass : public QWidget
{}

class AbstractPanel
{
virtual void AbsMethod() = 0;
}

class MyClass : public GeneratedClass , AbstractPanel
{
void AbsMethod() {;} // impl
}
I have the QWidget pointer, how can I cast it so that I can
make a call to the AbsMethod implemented by MyClass ?
thanx a lot ...
bye


--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Jul 22 '05 #1
5 1586
AriZOnA wrote:
Hi!
I have the following scenario:

class QWidget
{}

class GeneratedClass : public QWidget
{}

class AbstractPanel
{
virtual void AbsMethod() = 0;
}

class MyClass : public GeneratedClass , AbstractPanel
Was the private inheritance from AbstractPanel intentional?
{
void AbsMethod() {;} // impl
}
I have the QWidget pointer, how can I cast it so that I can
make a call to the AbsMethod implemented by MyClass ?


I assume QWidget is polymorphic (Well, I know Qt :-) ) and the
inheritance from AbstractPanel was supposed to be public.
Use dynamic_cast:

if (MyClass* p = dynamic_cast<MyClass*>(thewidgetpointer))
p->AbsMethod();
else
std::cerr << "your QWidget isn't an instance of MyClass\n";

Jul 22 '05 #2
"AriZOnA" <wa*@libero.it> wrote:
Hi!
I have the following scenario:

class QWidget
{}

class GeneratedClass : public QWidget
{}

class AbstractPanel
{
virtual void AbsMethod() = 0;
}

class MyClass : public GeneratedClass , AbstractPanel
{
void AbsMethod() {;} // impl
}
I have the QWidget pointer, how can I cast it so that I can
make a call to the AbsMethod implemented by MyClass ?


How do you know that the object in the QWidget pointer you have
implements the AbsMethod?
Jul 22 '05 #3
On Wed, 14 Apr 2004 11:25:14 +0000 (UTC), "AriZOnA" <wa*@libero.it>
wrote:
Hi!
I have the following scenario:

class QWidget
{}

class GeneratedClass : public QWidget
{}

class AbstractPanel
{
virtual void AbsMethod() = 0;
}

class MyClass : public GeneratedClass , AbstractPanel
{
void AbsMethod() {;} // impl
}
I have the QWidget pointer, how can I cast it so that I can
make a call to the AbsMethod implemented by MyClass ?


Assuming QWidget has at least 1 virtual function (and surely it has a
virtual destructor), you can do:

if (MyClass* p = dynamic_cast<MyClass*>(qptr))
{
p->AbsMethod();
}

(or dynamic_cast to AbstractPanel instead).

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #4
tom_usenet <to********@hotmail.com> wrote:
"AriZOnA" <wa*@libero.it> wrote:
class QWidget
{}

class GeneratedClass : public QWidget
{}

class AbstractPanel
{
virtual void AbsMethod() = 0;
}

class MyClass : public GeneratedClass , AbstractPanel
{
void AbsMethod() {;} // impl
}

I have the QWidget pointer, how can I cast it so that I can
make a call to the AbsMethod implemented by MyClass ?
Assuming QWidget has at least 1 virtual function (and surely it has a
virtual destructor), you can do:

if (MyClass* p = dynamic_cast<MyClass*>(qptr))
{
p->AbsMethod();
}


If you expect qptr to always be a MyClass *, you can go:

dynamic_cast<MyClass &>(*qptr).AbsMethod();

which will throw a std::bad_cast exception if qptr was not a
MyClass *.
(or dynamic_cast to AbstractPanel instead).


Is it safe to "sideways" dynamic cast? I've only ever used it
where the cast-to class was a direct descendent of the cast-from class.
Jul 22 '05 #5
On 14 Apr 2004 21:39:24 -0700, ol*****@inspire.net.nz (Old Wolf)
wrote:
Is it safe to "sideways" dynamic cast? I've only ever used it
where the cast-to class was a direct descendent of the cast-from class.


Yes, dynamic_cast can be used to cross-cast.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #6

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

Similar topics

0
by: krystoffff | last post by:
For the following query, I have a little problem ... First, I have to rewrite several times the same query because the alias are not recognised in the same query ( I got an error when I try to...
3
by: Myron | last post by:
I'm trying to create a query that will tell me which requests took longer than 10 days to move one from particular state to another state. The query I've created returns the correct requests, but...
17
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee...
1
by: Sacha Faust | last post by:
I have an abstract class, RuleResponse, and then create a new class base on it, RuleResponseSequence, and override the ++ operater. If I try to cast a RuleResponse as a RuleResponseSequence and...
14
by: Jason Sewall | last post by:
Hello, I'm using the intel C++ 8.0 compiler on windows and I'm working on some software that uses the following lines of code: gf->invdim = 1.0/(double)dim; printf("invdim: %f\n",...
8
by: I am Sam | last post by:
Hi everyone, This problem is making me old. I don't want to get any older. I have a multi-nested repeater control as follows: <asp:Repeater ID="clubRep1" Runat="server">...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
0
by: Gagan Sindhu Dewangan | last post by:
Below is the following procedure where I am facing the problem, the work of the below procedure is to update the database. query result:sp_helptext bProjectUpdate Text ...
10
by: kkirtac | last post by:
Hi, i have a void pointer and i cast it to an appropriate known type before using. The types which i cast this void* to are, from the Intel's open source computer vision library. Here is my piece...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.