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

Using a macro, can I change what type an object is being cast to?

Using a macro, can I change what type an object is being cast to?

I know, the initial respone to question might be an instinctive "ugly, don't
even think about it!" or "don't use macros at all", but I still would like
to know because I am trying to encapsulate a third-party C-based
callback-oriented gui toolkit inside C++ classes and I would like to try
something.

So say you have:
long some_address = some_valid_address;
((SomeClass*)some_address)->foo();

I want to test something and I would like to know if I can write a macro
that casts some_address to the pointer of another class (actually a subclass
of SomeClass). Since macros are part of the preprocessor and the
preprocessor is run before the compiler, a macro can replace the actual code
seen by the compiler, yes? I don't think I've ever made my own macro, I only
use the preprocessor for #ifndef, #ifdef etc.

So why do I want to do this? Well, as I said I am trying to encapsulate a
third-party C-based gui toolkit that revolves around callbacks. My parent
class has a static member function that acts a dummy callback (called by the
system), and using the parameters it finds a pointer to itself (actually a
pointer to a subclass) and uses that to call non-static member functions.
The problem is that the system seems to be calling this static member
function before the subclass object has been constructed fully thus calling
the parent version of some virtual functions the first few calls.

Thanks for reading and replying

/ Eric
Jul 23 '05 #1
5 1879
Yes, you could use macro as you like, C++ compiler does still support macro.
As long as you could guarantee the syntex is correct in C++ after
proprocessed.

Just mention about your example, it's not good to use "long" variable to
store a "pointer" even though it's not a problem in some platforms, but
always "void *" is the best choise. There is not any relationship between
long data type and 32/64 bits address.

"Eric Lilja" <mi****************************@gmail.com> wrote in message
news:d1**********@news.island.liu.se...
Using a macro, can I change what type an object is being cast to?

I know, the initial respone to question might be an instinctive "ugly, don't even think about it!" or "don't use macros at all", but I still would like
to know because I am trying to encapsulate a third-party C-based
callback-oriented gui toolkit inside C++ classes and I would like to try
something.

So say you have:
long some_address = some_valid_address;
((SomeClass*)some_address)->foo();

I want to test something and I would like to know if I can write a macro
that casts some_address to the pointer of another class (actually a subclass of SomeClass). Since macros are part of the preprocessor and the
preprocessor is run before the compiler, a macro can replace the actual code seen by the compiler, yes? I don't think I've ever made my own macro, I only use the preprocessor for #ifndef, #ifdef etc.

So why do I want to do this? Well, as I said I am trying to encapsulate a
third-party C-based gui toolkit that revolves around callbacks. My parent
class has a static member function that acts a dummy callback (called by the system), and using the parameters it finds a pointer to itself (actually a
pointer to a subclass) and uses that to call non-static member functions.
The problem is that the system seems to be calling this static member
function before the subclass object has been constructed fully thus calling the parent version of some virtual functions the first few calls.

Thanks for reading and replying

/ Eric

Jul 23 '05 #2
Eric Lilja wrote:
Using a macro, can I change what type an object is being cast to?
No. Using a macro you can substitute one text with another before
compiling your code.
I know, the initial respone to question might be an instinctive "ugly, don't
even think about it!" or "don't use macros at all", but I still would like
to know because I am trying to encapsulate a third-party C-based
callback-oriented gui toolkit inside C++ classes and I would like to try
something.

So say you have:
long some_address = some_valid_address;
((SomeClass*)some_address)->foo();

I want to test something and I would like to know if I can write a macro
that casts some_address to the pointer of another class (actually a subclass
of SomeClass). Since macros are part of the preprocessor and the
preprocessor is run before the compiler, a macro can replace the actual code
seen by the compiler, yes? I don't think I've ever made my own macro, I only
use the preprocessor for #ifndef, #ifdef etc.
Technically, you won't make your own macro this time either :-)
So why do I want to do this? Well, as I said I am trying to encapsulate a
third-party C-based gui toolkit that revolves around callbacks. My parent
class has a static member function that acts a dummy callback (called by the
system), and using the parameters it finds a pointer to itself (actually a
pointer to a subclass) and uses that to call non-static member functions.
The problem is that the system seems to be calling this static member
function before the subclass object has been constructed fully thus calling
the parent version of some virtual functions the first few calls.
I am not sure how using macros will fix that.
Thanks for reading and replying


You can define a macro like this:

#define CAST(what,towhat) ((towhat)what)
...
long some_address = some_valid_address;
CAST(some_address, SomeClass*)->foo();

What it buys you is a different question.

V
Jul 23 '05 #3
Eric Lilja wrote:

So why do I want to do this? Well, as I said I am trying to encapsulate a
third-party C-based gui toolkit that revolves around callbacks. My parent
class has a static member function that acts a dummy callback (called by the
system), and using the parameters it finds a pointer to itself (actually a
pointer to a subclass) and uses that to call non-static member functions.
The problem is that the system seems to be calling this static member
function before the subclass object has been constructed fully thus calling
the parent version of some virtual functions the first few calls.


And you think, that casting the pointer will help with that?
If the object isn't fully constructed, the most likely thing is
that you produce a crash (if you are lucky). If you are unlucky
the system just executes whatever it finds in memory, thinking
this is your function.

Why not install the callback *after* the object has been constructed
fully. That would avoid all those problems.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #4

"Karl Heinz Buchegger" wrote:
Eric Lilja wrote:

So why do I want to do this? Well, as I said I am trying to encapsulate a
third-party C-based gui toolkit that revolves around callbacks. My parent
class has a static member function that acts a dummy callback (called by
the
system), and using the parameters it finds a pointer to itself (actually
a
pointer to a subclass) and uses that to call non-static member functions.
The problem is that the system seems to be calling this static member
function before the subclass object has been constructed fully thus
calling
the parent version of some virtual functions the first few calls.
And you think, that casting the pointer will help with that?
If the object isn't fully constructed, the most likely thing is
that you produce a crash (if you are lucky). If you are unlucky
the system just executes whatever it finds in memory, thinking
this is your function.

Why not install the callback *after* the object has been constructed
fully. That would avoid all those problems.


Yes, I am leaning towards that alternative. A simple constructor that does
nothing really but construct the objects fully, vtables and all then a
create member function that does most of what my constructor is doing now. I
just need to document that (and document it well) after constructing an
object all operations except the create() are invalid until create has been
called (and it may only be called once).

Some pointed out that I should use void* not long, but I don't have choice
in the matter. The arguments passed to the callback are specified by the
system not me.

I just wanted to try the macro version to see what happens. I have a feeling
it won't work but at least I will have learned something regarding macros
and how they can be used.
Karl Heinz Buchegger
kb******@gascad.at


/ Eric
Jul 23 '05 #5
"Eric Lilja" <mi****************************@gmail.com> wrote in message
news:d1**********@news.island.liu.se...
A simple constructor that does
nothing really but construct the objects fully, vtables and all then a
create member function that does most of what my constructor is doing now. I just need to document that (and document it well) after constructing an
object all operations except the create() are invalid until create has been called (and it may only be called once).


For reference, what you are describing is sometimes called "two-phase
construction;" and sometimes "post-construction," which usually is mentioned
with "pre-destruction."

Ali

Jul 23 '05 #6

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

Similar topics

44
by: Simon Morgan | last post by:
Hi, Can somebody please help me grok the offsetof() macro? I've found an explanation on http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031 but I'm afraid it still...
4
by: Chris | last post by:
Hi, everything works apart from the last line :-(( rng.Value2.ToString() An exception is thrown : "Old format or invalid type library" It gets compiled though (so he recognizes the property...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
4
by: James | last post by:
I have a VB windows forms application that accesses a Microsoft Access database that has been secured using user-level security. The application is being deployed using No-Touch deployment. The...
3
by: Michael Hoehne | last post by:
Hi, I'm currently facing a problem with a mixed environment using .NET 1.1 and ..NET 2.0 web services. We have a client application (the "client", system 1) running on .NET 2.0/WinXP, calling...
2
by: Martin v. Löwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version:...
4
by: darrell | last post by:
I recently wrote a program in Access VBA that contains this snippet of code: For i = 1 to TotalBanks With Me("txtBank" & Chr(i + 64)) .BackColor = vbBlue .ForeColor = vbWhite End With Next i...
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...
14
by: KK | last post by:
Dear All I have a small problem with using as operator on value type array. Here is an example what I am trying to do. using System; using System.Collections.Generic; using System.Text;
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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.