|
I created a structure inside a function definition (which means that the structure will be destroyed when I leave that function scope). While I'm inside that function however, I would like to activate another function (let's call it 'DoSomethingWithStruct') to handle certain activities on that newly created structure.
In the class definition, I am able to get away declaring 'DoSomethingWithStruct' by using a 'void*' as the parameter to pass data between one function and the other.
My problem is, "How do I cast the 'void*' back to the structure when I'm inside 'DoSomethingWithStruct'? (Here's some code.)
class MyClass
{
...
void DoSomethingWithStruct(void* pMyStructure);
};
===============================
void MyClass::FirstFn()
{
struct MyStruct {...};
...
DoSomethingWithStruct(pMyStruct);
...
};
================================
void MyClass::DoSomethingWithStruct(void* pMyStructure)
{
// Here is where I need to cast 'pMyStructure' back from a 'void', to a 'MyStruct' type.
}
================================
I DO NOT want to declare 'MyStruct' as global.
Thanks for any insight.
William
Fortes in fide et opere!
|
|
|
|
|
This would be better
class MyClass
{
...
void DoSomethingWithStruct(MyStruct* pMyStructure);
};
|
|
|
|
|
Thanks for replying.
'MyClass' does not know of 'MyStruct' as a type, hence the reason I used 'void'.
William
Fortes in fide et opere!
|
|
|
|
|
Maybe you can just define the struct inside the class declaration instead of inside the function definition? You can make the struct private, so only members of the class can access it.
|
|
|
|
|
Thanks for replying.
Your answer would presume that I own the class. (My error for referring to the class as 'MyClass'.)
William
Fortes in fide et opere!
|
|
|
|
|
Hi
If you don't own the class, how can you change DoSomethingWithStruct() function definition, since it's a MyClass member?
However, if DoSomethingWithStruct() is not member of MyClass, I don't know of any way (besides making a global structure) to make it aware of the struct. Sorry.
|
|
|
|
|
The point you make is a very good point, and the only answer I can give is that I've become a victim of my own cleverness.
'DoSomethingWithStruct' is a function I recognized I could use, and which I did, except I now find myself limited in what I could use it for.
There are a couple other ways I could go (inheritance being one), but I would like to first explore if a simpler solution might still be available (even if it means applying additional creativity).
Thanks for your replies anyway, I appreciate them.
William
Fortes in fide et opere!
|
|
|
|
|
The definition of the MyStruct type is local to MyClass::FirstFn() and it's not visible in MyClass::DoSomethingWithStruct() . No way around it, them's the rules of the language. You'll need to move the definition of the type into MyClass (type definitions can be nested).
--Mike--
Personal stuff:: Ericahist | Homepage
Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt
CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ
----
If my rhyme was a drug, I'd sell it by the gram.
|
|
|
|
|
Thanks for replying.
I know what you're saying, but I've seen it too many times, when the doors seemed to be closed, there's a book somewhere holding a secret of one of the masters offering something the person didn't know about.
The easiest thing here is for me to say, "Yes, I agree with you," and go the way of tradition (which is NOT always in keeping with what the person wants to achieve). I don't believe I've reached either that bridge, or the end of my line.
Appreciate your response however.
William
Fortes in fide et opere!
|
|
|
|
|
ok, lets see. you pass a struct to your function, but the définition of your struct is not know into the function using it. well, you can do the manipulation by hand, but you (the writer of the function) must know the types of the members. this way, you could do some pointing...
but if one day you change the type of one member in the struct, the function won"t be usable anymore...
TOXCCT >>> GEII power
|
|
|
|
|
Thanks for replying.
There's a lot of permutations to the things you're saying, but don't forget I can overload a function and apply my own set of parameters to it, so long as I make sure I can identify with at least one of the functions already declared in the class.
That was one of the things that attracted me to the function with the 'void*' parameter (because it was already declared and I could use its 'void*' to transfer the struct data to it, and do my work where it's defined).
Considering the amount of effort I might have to invest in order to make this creative idea of mine work, even if I were to succeed, in the end the poor person coming behind me might become lost (most likely would become lost) in the convolution behind the cleverness. That said, I am beginning to feel maybe I should just stick to something simpler; ESPECIALLY if it works!!
William
Fortes in fide et opere!
|
|
|
|
|
TOXCCT >>> GEII power
|
|
|
|
|
WREY wrote:
I DO NOT want to declare 'MyStruct' as global
just curious but, WHY ???
TOXCCT >>> GEII power
|
|
|
|
|
To preserve the "principle of encapsulation".
Bikram Singh
|
|
|
|
|
but we never declare types into a function
you answer, you answer, but do you try to understand the subject ?
TOXCCT >>> GEII power
|
|
|
|
|
first, never say never!
i admit i did not read the the thread completely, mu fault.
but, it is possible to declare a struct inside of a function. sure, the uses may be limited, but it can be useful sometimes. one case i can think of is a simple log file which uses the struct as a "record".
Sure, the "log reader" would need access to this struct too, which is why it's silly to have the struct inside the function.
But it can have uses. Not that I can think of any at the moment!
Bikram Singh
|
|
|
|
|
I apply ‘HIWORD’ macro to get the high byte from a WPARAM value,
HOW can I get a negative value?
For example: The value is –120,but what I get is 65416.
Thank you!!!
|
|
|
|
|
You want to find some material for C/C++ that discusses signed and unsigned types. Maybe you can look at cplusplus.com
-pete
|
|
|
|
|
Thanks a lot.
But I hope that someone can tell me a little about it.
|
|
|
|
|
Thanks,I get it.
DWORD dwWp = 4287102976;
short int aaa = (short int)(HIWORD(dwWp));
TRACE("value = %d\n",aaa);
It's OK!!
|
|
|
|
|
i use smtp send emails to our customers.
i am confused with cc and bcc, do you know differeces between cc and bcc? they look the same
cheers
includeh10
|
|
|
|
|
Courtesy copy. Use when you just want to let someone to know the mail has been sent to other people. If some one has an action to perform, do not CC them, sent it to them instead. Addressees in the To: list will see all the addresses in the CC: list. If you told Andy you would ask Bill something, send it To: Bill and CC: Andy.
Blind Courtesy Copy. Similar to the above, but the To:’s and CC:’s cannot see the BCC:’s. If Bill does not reply, send another message To: Bill and BCC: Bill’s boss. Bill will not know you ratted him out.
AGN
|
|
|
|
|
|
LOL! I almost mentioned this in the first post. Perhaps I should have.
CC’s have been part of standard business practice since way back, when the courtesy copy was created using sheets of carbon paper and typewriters, etc. This has led to the confusion that that CC stands for carbon copy.
AGN
|
|
|
|
|