473,566 Members | 3,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why won't this cast?

class SORef {...};
class SelectedFORef : public SORef {...};
SOFORef SOCastToSOF(SOR ef sor) {
SelectedFORef sof=nil;
sof=dynamic_cas t<SelectedFORef >(sor);
return sof;
};

I am getting a problem where sor is not being cast to sof (becomes nil)
even though sor IS a SelectedFORef underneath. (I have a debug method
with details contents and type).

I am not a C++ expert.
What am I doing wrong?

Mark
May 25 '07 #1
18 3096
mark wrote:
class SORef {...};
class SelectedFORef : public SORef {...};
SOFORef SOCastToSOF(SOR ef sor) {
SelectedFORef sof=nil;
What's nil?

--
Ian Collins.
May 25 '07 #2
In article <5b************ *@mid.individua l.net>,
Ian Collins <ia******@hotma il.comwrote:
mark wrote:
class SORef {...};
class SelectedFORef : public SORef {...};
SOFORef SOCastToSOF(SOR ef sor) {
SelectedFORef sof=nil;

What's nil?
nil == NULL.
Pascal version of NULL.
May 25 '07 #3
mark wrote:
In article <5b************ *@mid.individua l.net>,
Ian Collins <ia******@hotma il.comwrote:
>mark wrote:
>>class SORef {...};
class SelectedFORef : public SORef {...};
SOFORef SOCastToSOF(SOR ef sor) {
SelectedFORef sof=nil;
What's nil?

nil == NULL.
Pascal version of NULL.
Then why hide it? You can't assign NULL to a class object, only a pointer.

You can't apply a dynamic_cast to a class object, only a reference of a
pointer to one.
--
Ian Collins.
May 25 '07 #4
In article <5b************ *@mid.individua l.net>,
Ian Collins <ia******@hotma il.comwrote:
mark wrote:
In article <5b************ *@mid.individua l.net>,
Ian Collins <ia******@hotma il.comwrote:
mark wrote:
class SORef {...};
class SelectedFORef : public SORef {...};
SOFORef SOCastToSOF(SOR ef sor) {
SelectedFORef sof=nil;
What's nil?
nil == NULL.
Pascal version of NULL.

Then why hide it? You can't assign NULL to a class object, only a pointer.

You can't apply a dynamic_cast to a class object, only a reference of a
pointer to one.
I am idiot.
Sorry.
Should be:

class SOObject {...};
typdef class SOObject * SORef;

class SelectedFileObj ect : public SOObject {...};
typedef class SelectedFileObj ect * SelectedFORef;

SOFORef SOCastToSOF(SOR ef sor) {
SelectedFORef sof=nil;
sof=dynamic_cas t<SelectedFORef >(sor);
return sof;
};
May 25 '07 #5
mark wrote:
In article <5b************ *@mid.individua l.net>,
Ian Collins <ia******@hotma il.comwrote:
>mark wrote:
>>In article <5b************ *@mid.individua l.net>,
Ian Collins <ia******@hotma il.comwrote:

mark wrote:
class SORef {...};
class SelectedFORef : public SORef {...};
>
>
SOFORef SOCastToSOF(SOR ef sor) {
SelectedFORef sof=nil;
What's nil?
nil == NULL.
Pascal version of NULL.
Then why hide it? You can't assign NULL to a class object, only a pointer.

You can't apply a dynamic_cast to a class object, only a reference of a
pointer to one.

I am idiot.
Sorry.
Should be:

class SOObject {...};
typdef class SOObject * SORef;

class SelectedFileObj ect : public SOObject {...};
typedef class SelectedFileObj ect * SelectedFORef;
It's seldom, if ever, a good idea to hide pointer types behind typedefs.
SOFORef SOCastToSOF(SOR ef sor) {
SelectedFORef sof=nil;
sof=dynamic_cas t<SelectedFORef >(sor);
return sof;
};
Why not

SelectedFORef sof = dynamic_cast<Se lectedFORef>(so r);
--
Ian Collins.
May 25 '07 #6
In article <5b************ **@mid.individu al.net>,
Ian Collins <ia******@hotma il.comwrote:
mark wrote:
In article <5b************ *@mid.individua l.net>,
Ian Collins <ia******@hotma il.comwrote:
mark wrote:
In article <5b************ *@mid.individua l.net>,
Ian Collins <ia******@hotma il.comwrote:

mark wrote:
class SORef {...};
class SelectedFORef : public SORef {...};
SOFORef SOCastToSOF(SOR ef sor) {
SelectedFORef sof=nil;
What's nil?
nil == NULL.
Pascal version of NULL.
Then why hide it? You can't assign NULL to a class object, only a pointer.

You can't apply a dynamic_cast to a class object, only a reference of a
pointer to one.
I am idiot.
Sorry.
Should be:

class SOObject {...};
typdef class SOObject * SORef;

class SelectedFileObj ect : public SOObject {...};
typedef class SelectedFileObj ect * SelectedFORef;
It's seldom, if ever, a good idea to hide pointer types behind typedefs.
Have to. The class pointer is available to plugins. And C++ doesn't like
plugins. So I typedef as a struct * for plugins and use accessor
routines.
>
SOFORef SOCastToSOF(SOR ef sor) {
This is an accessor routine for plugins.
The debugger shows it to still be a SelectedFORef class instance.
SelectedFORef sof=nil;
sof=dynamic_cas t<SelectedFORef >(sor);
return sof;
};

Why not

SelectedFORef sof = dynamic_cast<Se lectedFORef>(so r);
Did try that. Makes no difference.
Would the typedef actually screw things up?
May 25 '07 #7
mark wrote:
In article <5b************ **@mid.individu al.net>,
Ian Collins <ia******@hotma il.comwrote:
>mark wrote:
>>In article <5b************ *@mid.individua l.net>,
Ian Collins <ia******@hotma il.comwrote:

mark wrote:
In article <5b************ *@mid.individua l.net>,
Ian Collins <ia******@hotma il.comwrote:
>
>mark wrote:
>>class SORef {...};
>>class SelectedFORef : public SORef {...};
>>>
>>>
>>SOFORef SOCastToSOF(SOR ef sor) {
>> SelectedFORef sof=nil;
>What's nil?
nil == NULL.
Pascal version of NULL.
Then why hide it? You can't assign NULL to a class object, only a pointer.

You can't apply a dynamic_cast to a class object, only a reference of a
pointer to one.
I am idiot.
Sorry.
Should be:

class SOObject {...};
typdef class SOObject * SORef;

class SelectedFileObj ect : public SOObject {...};
typedef class SelectedFileObj ect * SelectedFORef;
It's seldom, if ever, a good idea to hide pointer types behind typedefs.

Have to. The class pointer is available to plugins. And C++ doesn't like
plugins. So I typedef as a struct * for plugins and use accessor
routines.
That's not a reason that makes any sense to me, (not saying it is wrong
though).
>>SOFORef SOCastToSOF(SOR ef sor) {

This is an accessor routine for plugins.
The debugger shows it to still be a SelectedFORef class instance.
>> SelectedFORef sof=nil;
sof=dynamic_cas t<SelectedFORef >(sor);
return sof;
};
Why not

SelectedFORef sof = dynamic_cast<Se lectedFORef>(so r);

Did try that. Makes no difference.
Would the typedef actually screw things up?
No.

Does class SOObject have at least one virtual function? You need at
least one virtual function for dynamic_cast to work.

When you say the cast is not working, what *exactly* do you mean? How
can you tell the cast is not working?

john

May 25 '07 #8
In article <GY************ **@newsfe6-win.ntli.net>,
John Harrison <jo************ *@hotmail.comwr ote:
mark wrote:
In article <5b************ **@mid.individu al.net>,
Ian Collins <ia******@hotma il.comwrote:
mark wrote:
In article <5b************ *@mid.individua l.net>,
Ian Collins <ia******@hotma il.comwrote:

mark wrote:
In article <5b************ *@mid.individua l.net>,
Ian Collins <ia******@hotma il.comwrote:

mark wrote:
>class SORef {...};
>class SelectedFORef : public SORef {...};
>>
>>
>SOFORef SOCastToSOF(SOR ef sor) {
> SelectedFORef sof=nil;
What's nil?
nil == NULL.
Pascal version of NULL.
Then why hide it? You can't assign NULL to a class object, only a
pointer.

You can't apply a dynamic_cast to a class object, only a reference of a
pointer to one.
I am idiot.
Sorry.
Should be:

class SOObject {...};
typdef class SOObject * SORef;

class SelectedFileObj ect : public SOObject {...};
typedef class SelectedFileObj ect * SelectedFORef;

It's seldom, if ever, a good idea to hide pointer types behind typedefs.
Have to. The class pointer is available to plugins. And C++ doesn't like
plugins. So I typedef as a struct * for plugins and use accessor
routines.

That's not a reason that makes any sense to me, (not saying it is wrong
though).
>SOFORef SOCastToSOF(SOR ef sor) {
This is an accessor routine for plugins.
The debugger shows it to still be a SelectedFORef class instance.
> SelectedFORef sof=nil;
sof=dynamic_cas t<SelectedFORef >(sor);
return sof;
};
Why not

SelectedFORef sof = dynamic_cast<Se lectedFORef>(so r);
Did try that. Makes no difference.
Would the typedef actually screw things up?

No.

Does class SOObject have at least one virtual function? You need at
least one virtual function for dynamic_cast to work.

When you say the cast is not working, what *exactly* do you mean? How
can you tell the cast is not working?
Mac OS X gdb debugger.
Inspecting sor with XCode/gdb shows it to be a SelectedFileObj ect
underneath. It's type is listed as such and not the base class SORef.
So the dynamic_cast should work (according to the C++ book I have,
virtual
john
Here's the full class definitions.

class SelectedObject {
UInt16 refCt;

SelObjectClass objectClass;
OSType ready;
protected:
SOOwnerID ID; // need to identify where the SO belongs. An SO in one
view will have
// different select flags to an SO in another view
SelObjectType objectType; // NYI
SelectionFlag selections;

static CFMutableSetRef soCache;
static void SOCacheSetProc( const void* value, void* ctx);
friend SORef CheckSOCache(Se lObjectClass objClass, UInt16 objType,
SOOwnerID iID, void* data);
virtual OSStatus CheckCache(SelO bjectClass objClass, UInt16 dataType,
void* data,
SORef *sor);
// the arrays are used to send one select/deselect event when a whole
lot
// of objects are selected/deselected. Sending 1000+ deselected
events is not a good
// idea
static CritRgn recentSelRgn;
static CFMutableSetRef recentlySelecte d;
static CFMutableSetRef recentlyDeselec ted;
static OSStatus PostMassSelOfOw ner(SOOwnerID ID, Boolean selOrDesel,
CFArrayRef itsItems);
static OSStatus PostMassSelectE vents(SOOwnerID whichID=0); // clears
both arrays after sending
// dtEventKindObje ctsSelected & dtEventKindObje ctsSelected
// If zero is passed, all are sent (in seperate events for each ID)
OSStatus AddToSelected() ;
OSStatus AddToDeselected ();

void SOCleanup();
public:
SelectedObject( SelObjectClass theObjClass, SOOwnerID ID);
SelectedObject( const SelectedObject &obj);
virtual ~SelectedObject ();
UInt16 Retain();
UInt16 Release();
SelObjectClass GetObjectClass( );
virtual SelObjectType GetObjectType() ; // NYI. Will always return
zero.
SOOwnerID GetOwnerID();

virtual CFStringRef CopySOString();

virtual UInt64 GetSOData64(); // these two will always return zero
for the base class
virtual UInt32 GetSOData32(); // it is up to the subclasses to return
the correct values

SelectionFlag GetSelectFlags( );
void DefineSelectFla g(SelectionFlag theFlags, Boolean newValue);
void ToggleSelectFla g(SelectionFlag theFlags);
Boolean IsSelectedOr(Se lectionFlag whichFlags); // returns true if
any of the passed flags are set
Boolean IsSelectedAnd(S electionFlag whichFlags); // returns true if
all of the passed flags are set
Boolean AnySelection(); // returns true is any flag is set

void PostSelectedEve nt(Boolean selected);

virtual Boolean operator==(Sele ctedObject &obj);

virtual void DebugPrint(UInt 32 v=0); // printf
virtual CFStringRef DebugString(UIn t32 v=0); // CFStringRef -CFShow
};

//-----------------------------------------------------------------------
--------------

class SelectedFileObj ect:public SelectedObject {
OSType sfoReady;
protected:
FSRef fsr;

virtual OSStatus CheckCache(SelO bjectClass objClass, UInt16 dataType,
void* data,
SORef *sor);
void SFOCleanup();
public:
SelectedFileObj ect(const FSRef* fsrptr, SOOwnerID ID);
SelectedFileObj ect(const char* cpath, SOOwnerID ID);
SelectedFileObj ect(CFURLRef url, SOOwnerID ID);
SelectedFileObj ect(const SelectedFileObj ect &obj);
~SelectedFileOb ject();

CFStringRef CopySOString();
UInt64 GetSOData64();
UInt32 GetSOData32();
FSRef* GetFSRefPtr();
virtual CFStringRef CopyName();

Boolean operator==(Sele ctedFileObject &obj);

void DebugPrint(UInt 32 v=0); // printf
CFStringRef DebugString(UIn t32 v=0); // CFStringRef -CFShow
};

typedef class SelectedFileObj ect* SelectedFORef;
typedef SelectedFORef SOFORef;
May 25 '07 #9
>>>>I am idiot.
>>>>Sorry.
Should be:
>
class SOObject {...};
typdef class SOObject * SORef;
>
class SelectedFileObj ect : public SOObject {...};
typedef class SelectedFileObj ect * SelectedFORef;
>
[snip]
>
Here's the full class definitions.

class SelectedObject {
UInt16 refCt;
[snip]
>
//-----------------------------------------------------------------------
--------------

class SelectedFileObj ect:public SelectedObject {
OSType sfoReady;
protected:
[snip]
>
typedef class SelectedFileObj ect* SelectedFORef;
typedef SelectedFORef SOFORef;
There's some confusion here

In your original code, the base class is called SOObject, in the class
definitions posted more recently, it's SelectedObject.

Could this be the problem, are you getting mixed up with your names?

john
May 25 '07 #10

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

Similar topics

0
3575
by: Aaron W. West | last post by:
Fun with CAST! (Optimized SQLServerCentral script posts) I found some interesting "tricks" to convert binary to hexadecimal and back, which allow doing 4 or 8 at a time. Test code first: -- These two have the same output, other than the width: select dbo.ufn_vbintohexstr(0x123456789abcdef1234) select 0x123456789abcdef1234
15
1721
by: Gregg Woodcock | last post by:
My compiler (m68k-gcc) does not allow this (simplified example; not my real code) and I don't see why not: { char *arrayCharPtr1 = {"a", "ab", NULL}; char *arrayCharPtr2 = {"A", "AB", NULL}; char *arrayCharPtrVar = ((someBoolean) ? arrayCharPtr1 : arrayCharPtr2); // <-error here! StrCopy(someString, arrayCharPtr); }
6
1322
by: Adam Warner | last post by:
Hi all, When adding variable length arrays to my program I created the elements as type struct o *. This is because /most/ of the time the VLAs contains pointers to struct o objects (these objects are as stringently aligned as any C type in the implementation). Occasionally a single element is actually a pointer to another VLA of pointers...
17
2665
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this cboFont_DrawItem event handler? Why does it need to be cast? -hazz ,................. cboFont.Items.AddRange(FontFamily.Families); } private...
1
5487
by: Alpha | last post by:
I have a Window based application that shows up still running in the task manager when I close it. It reaches the "this.close" statement and then it stops at the "}" at the section of the following code after the "Application.Run(new VMSMain());": static void Main() { Application.Run(new VMSMain()); }
5
3405
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to...
5
2353
by: Frederick Gotham | last post by:
Before I begin, here's a list of assumptions for this particular example: (1) unsigned int has no padding bits, and therefore no invalid bit- patterns or trap representations. (2) All types have the same alignment requirements. (3) sizeof(double) >= sizeof(unsigned) ===================================== We can cast from double to...
9
3419
by: Ivan Jericevich | last post by:
In my code below at the line 'response' a blip sound is heard and the program exits the sub -- No MsgBox is displayed. What am I doing wrong? If nonNumberEntered = True Then msg = "Enter numbers only" style = MsgBoxStyle.OkOnly title = "ERROR in cast"
1
1819
by: DerekBaker | last post by:
I had this code previously: void App::FillFontBox(HWND PrefsBox) { EnumFontFamilies(hdc,(LPCTSTR)NULL,(FONTENUMPROC)EnumFontNamesProc,(long)this);
0
7584
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...
0
8109
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...
1
7645
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...
0
7953
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...
0
6263
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5485
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...
0
3643
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...
1
2085
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
0
926
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...

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.