473,396 Members | 1,599 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,396 software developers and data experts.

derive a class from a struct?

This is a solution I thought about to some of the problems I had talked
about in the "zero memory" above in the ng.

While this isn't a MS specific post, let me walk you though the problem that
MS created that I am trying to overcome

They have functions:

HANDLE CreateIoCompletionPort(
HANDLE FileHandle,
HANDLE ExistingCompletionPort,
ULONG_PTR CompletionKey,
DWORD NumberOfConcurrentThreads
);

BOOL AcceptEx(
SOCKET sListenSocket,
SOCKET sAcceptSocket,
PVOID lpOutputBuffer,
DWORD dwReceiveDataLength,
DWORD dwLocalAddressLength,
DWORD dwRemoteAddressLength,
LPDWORD lpdwBytesReceived,
LPOVERLAPPED lpOverlapped
);

BOOL GetQueuedCompletionStatus(
HANDLE CompletionPort,
LPDWORD lpNumberOfBytes,
PULONG_PTR lpCompletionKey,
LPOVERLAPPED* lpOverlapped,
DWORD dwMilliseconds
);

Now the flow of things that they show in thier example code creates a
context struct for each IO command, which contains user specific data. This
struct that they make contains an OVERLAPPED structure, which is what MS
uses to register an IO command with an IO-completion port. When the
IO-Completion port is done with an IO command, a thread stops waiting at a
call to GetQueuedCompletionStatus. Now then, when GetQueuedCompletionStatus
returns, all we have is the OVERLAPPED structure that was passed in as an
argument to the IO command, but we need the entire context. So, MS, in
thier example, casts the OVERLAPPED structure to the entrire structure that
it was a member of. This works because, they made the OVERLAPPED structure
the first member of the context structure.

I want to use C++, I am tired of mixing C code and C++ code together and
having to hack past the problems that arise.

In order to do this I was wondering if I could make a context class that is
derived from thier OVERLAPPED structure. That way I could dynamic cast from
an OVERLAPPED pointer to a context pointer right? That would get rid of
thier C cast that they depend on (which is really unsafe and is pretty much
a reinterp cast imo). I know I can cast from a base class upwards to a
derived class, can you also cast the other way around? Are there any inherit
problems with deriving a class from a struct that I need to know about? do
you think this solution would work?

Here is there OVERLAPPED for reference:

typedef struct _OVERLAPPED {
ULONG_PTR Internal;
ULONG_PTR InternalHigh;
union {
struct {
DWORD Offset;
DWORD OffsetHigh;
};
PVOID Pointer;
};
HANDLE hEvent;
} OVERLAPPED,
*LPOVERLAPPED;
Apr 6 '07 #1
4 3305
Christopher Pisz wrote:
>
I want to use C++, I am tired of mixing C code and C++ code together and
having to hack past the problems that arise.
You're calling a C API, so you have to respect C conventions.
In order to do this I was wondering if I could make a context class that is
derived from thier OVERLAPPED structure. That way I could dynamic cast from
an OVERLAPPED pointer to a context pointer right?
No. If OVERLAPPED has any virtual functions (which is required in order
to use dynamic_cast), then it won't match the layout that the C function
you're calling expects. Even if it doesn't (which means you can't use
dynamic_cast), there's no guarantee that the compiler will lay out the
data in the same way it would when OVERLAPPED is the first member.
That would get rid of
thier C cast that they depend on (which is really unsafe and is pretty much
a reinterp cast imo).
Its behavior is well defined in both C and C++.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Apr 6 '07 #2
I'm probably overquoting... Sorry about that. Don't have time to
trim it as needed.

Christopher Pisz wrote:
This is a solution I thought about to some of the problems I had
talked about in the "zero memory" above in the ng.

While this isn't a MS specific post, let me walk you though the
problem that MS created that I am trying to overcome

They have functions:

HANDLE CreateIoCompletionPort(
HANDLE FileHandle,
HANDLE ExistingCompletionPort,
ULONG_PTR CompletionKey,
DWORD NumberOfConcurrentThreads
);

BOOL AcceptEx(
SOCKET sListenSocket,
SOCKET sAcceptSocket,
PVOID lpOutputBuffer,
DWORD dwReceiveDataLength,
DWORD dwLocalAddressLength,
DWORD dwRemoteAddressLength,
LPDWORD lpdwBytesReceived,
LPOVERLAPPED lpOverlapped
);

BOOL GetQueuedCompletionStatus(
HANDLE CompletionPort,
LPDWORD lpNumberOfBytes,
PULONG_PTR lpCompletionKey,
LPOVERLAPPED* lpOverlapped,
DWORD dwMilliseconds
);

Now the flow of things that they show in thier example code creates a
context struct for each IO command, which contains user specific
data. This struct that they make contains an OVERLAPPED structure,
which is what MS uses to register an IO command with an IO-completion
port. When the IO-Completion port is done with an IO command, a
thread stops waiting at a call to GetQueuedCompletionStatus. Now
then, when GetQueuedCompletionStatus returns, all we have is the
OVERLAPPED structure that was passed in as an argument to the IO command,
but we need the entire context. So, MS, in thier example,
casts the OVERLAPPED structure to the entrire structure that it was a
member of. This works because, they made the OVERLAPPED structure the
first member of the context structure.
I want to use C++, I am tired of mixing C code and C++ code together
and having to hack past the problems that arise.

In order to do this I was wondering if I could make a context class
that is derived from thier OVERLAPPED structure. That way I could
dynamic cast from an OVERLAPPED pointer to a context pointer right?
No. You can only use 'dynamic_cast' with polymorphic classes. Since
your OVERLAPPED struct is not polymorphic (and it can't be), you can
only use 'static_cast' if you are sure that the pointer you have is
in fact obtained by converting from a pointer to your derived class.
That would get rid of thier C cast that they depend on (which is
really unsafe and is pretty much a reinterp cast imo). I know I can
cast from a base class upwards to a derived class, can you also cast
the other way around?
Yes, but why?
Are there any inherit problems with deriving a
class from a struct that I need to know about?
No.
do you think this
solution would work?
It might.
>
Here is there OVERLAPPED for reference:

typedef struct _OVERLAPPED {
ULONG_PTR Internal;
ULONG_PTR InternalHigh;
union {
struct {
DWORD Offset;
DWORD OffsetHigh;
};
PVOID Pointer;
};
HANDLE hEvent;
} OVERLAPPED,
*LPOVERLAPPED;
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 6 '07 #3

"Pete Becker" <pe**@versatilecoding.comwrote in message
news:Zc******************************@giganews.com ...
Christopher Pisz wrote:
>That would get rid of thier C cast that they depend on (which is really
unsafe and is pretty much a reinterp cast imo).

Its behavior is well defined in both C and C++.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
(www.petebecker.com/tr1book)
Not to be a smartass, but more out of curiosity, where would I find such a
definition?
Does the same go for classes? Can you cast from a pointer to a class' first
data member, to a pointer to the entire class?
What if the class has virtual functions?

Thanks!
Apr 6 '07 #4
Christopher Pisz wrote:
"Pete Becker" <pe**@versatilecoding.comwrote in message
news:Zc******************************@giganews.com ...
>Christopher Pisz wrote:
>>That would get rid of thier C cast that they depend on (which is really
unsafe and is pretty much a reinterp cast imo).
Its behavior is well defined in both C and C++.

Not to be a smartass, but more out of curiosity, where would I find such a
definition?
In C99, 6.7.2.1/13. In C++, [class.mem]/18.
Does the same go for classes? Can you cast from a pointer to a class' first
data member, to a pointer to the entire class?
What if the class has virtual functions?
Only for POD-structs. But note that class and struct mean the same
thing, except for default access rules. So, yes, it goes for classes if
they satisfy the POD-struct requirements.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Apr 6 '07 #5

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

Similar topics

4
by: Steve | last post by:
I'll be the first to admit, I'm not entirely clear on the appropriate usage of either. From what I am reading in my books, a Struct and a Class are pretty much the same, with the difference being,...
3
by: Marcin Kowalewski | last post by:
Hi I've got a stupid problem with code below : using System; using System.Drawing; .... public class CsrcImage :Image { public CsrcImage() { //
4
by: Henke | last post by:
Hi! I have a abstract base class that derives from System.Web.UI.Page. In this class I have some abstract methods. My other web pages should derive from this class and implement the abstract...
13
by: olanglois | last post by:
Hi, I am trying to derive a new class that will add new functions but no new data members and the base class has overloaded operators (+,-,+=,-=,etc...) returning either (Base &) or (const Base)...
3
by: gyan | last post by:
Please go though following program: #include <iostream.h> class base { public: int basepublic; protected: int baseprotected; };
2
by: andy6 via DotNetMonster.com | last post by:
I took a c++ 6.0 project and converted it to c++ .net 2005 project. I want to make a web service out of it. One of the new files I created was a cpp where I have the webmethod pointing to a...
2
by: Ninereeds | last post by:
I'm messing around with using mixin-layers (look for papers by Yannis Smaragdakis and Don Batory) to define data structures. One issue is that nodes tend to have pointers to other nodes - the...
3
by: hufaunder | last post by:
Imagine you have a charting library that can draw lines, bars, floating bars, bands, etc. Lines and bars need only one input. Floating bars and bands need two inputs. There are two approaches: ...
2
by: Markus Dehmann | last post by:
I want to derive from std::set, like shown below. But when I try to declare an iterator over the contained elements I get an error, see the twp uncommented lines: #include <set> template<class...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...

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.