Hi, guys,
I met a very strange problem while using void* in CLI\C++.
Code like this:
CLI\C++ Code:
static BaseClass^
BaseClass::ConstructWrapper(NativeNameSpace::BaseC lass* native)
{
//Test. Add a Reference Count. It's OK
native->AddRef();
//Assign native pointer to a void* pointer.
void* voidPtr = native;
//Convert the void* to ChildClass, derived from BaseClass
NativeNameSpace::ChildClass* child =
static_cast<NativeNameSpace::ChildClass*>(voidPtr );
//NativeNameSpace::ChildClass* child =
(NativeNameSpace::ChildClass*)voidPtr ;
//It's failded !!!!!!!
child->AddRef();
return gcnew ChildClass(child );
}
int main
{
NativeNameSpace::BaseClass* native = new
NativeNameSpace::ChildClass();
BaseClass::ConstructWrapper( native );
}
If I write the code like following. I don't use void* anymore, and
it's OK.
static BaseClass^
BaseClass::ConstructWrapper(NativeNameSpace::BaseC lass* native)
{
//Test. Add a Reference Count. It's OK
native->AddRef();
//Convert the void* to ChildClass, derived from BaseClass
NativeNameSpace::ChildClass* child =
(NativeNameSpace::ChildClass*)(native);
//It's OK!!!!!!!
child->AddRef();
return gcnew ChildClass(child );
}
int main
{
NativeNameSpace::BaseClass* native = new
NativeNameSpace::ChildClass();
BaseClass::ConstructWrapper( native );
}
Obviously, the void* usage break the code down. Is there any taboo
while using void* in CLI\C++?
Thanks,
Ed. 4 2112
Ed wrote:
Hi, guys,
I met a very strange problem while using void* in CLI\C++.
Code like this:
CLI\C++ Code:
static BaseClass^
BaseClass::ConstructWrapper(NativeNameSpace::BaseC lass* native)
{
//Test. Add a Reference Count. It's OK
native->AddRef();
//Assign native pointer to a void* pointer.
void* voidPtr = native;
//Convert the void* to ChildClass, derived from BaseClass
NativeNameSpace::ChildClass* child =
static_cast<NativeNameSpace::ChildClass*>(voidPtr );
//NativeNameSpace::ChildClass* child =
(NativeNameSpace::ChildClass*)voidPtr ;
//It's failded !!!!!!!
child->AddRef();
return gcnew ChildClass(child );
}
int main
{
NativeNameSpace::BaseClass* native = new
NativeNameSpace::ChildClass();
BaseClass::ConstructWrapper( native );
}
If I write the code like following. I don't use void* anymore, and
it's OK.
static BaseClass^
BaseClass::ConstructWrapper(NativeNameSpace::BaseC lass* native)
{
//Test. Add a Reference Count. It's OK
native->AddRef();
//Convert the void* to ChildClass, derived from BaseClass
NativeNameSpace::ChildClass* child =
(NativeNameSpace::ChildClass*)(native);
//It's OK!!!!!!!
child->AddRef();
return gcnew ChildClass(child );
}
int main
{
NativeNameSpace::BaseClass* native = new
NativeNameSpace::ChildClass();
BaseClass::ConstructWrapper( native );
}
Obviously, the void* usage break the code down. Is there any taboo
while using void* in CLI\C++?
I think you ought to use a dynamic_cast, otherwise the void* might be
getting the address of the base sub-object, not the full object, and that
would cause the static_cast to do the wrong thing.
>
Thanks,
Ed.
On May 22, 4:34 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Ed wrote:
Hi, guys,
I met a very strange problem while using void* in CLI\C++.
Code like this:
CLI\C++ Code:
static BaseClass^
BaseClass::ConstructWrapper(NativeNameSpace::BaseC lass* native)
{
//Test. Add a Reference Count. It's OK
native->AddRef();
//Assign native pointer to a void* pointer.
void* voidPtr = native;
//Convert the void* to ChildClass, derived from BaseClass
NativeNameSpace::ChildClass* child =
static_cast<NativeNameSpace::ChildClass*>(voidPtr );
//NativeNameSpace::ChildClass* child =
(NativeNameSpace::ChildClass*)voidPtr ;
//It's failded !!!!!!!
child->AddRef();
return gcnew ChildClass(child );
}
int main
{
NativeNameSpace::BaseClass* native = new
NativeNameSpace::ChildClass();
BaseClass::ConstructWrapper( native );
}
If I write the code like following. I don't use void* anymore, and
it's OK.
static BaseClass^
BaseClass::ConstructWrapper(NativeNameSpace::BaseC lass* native)
{
//Test. Add a Reference Count. It's OK
native->AddRef();
//Convert the void* to ChildClass, derived from BaseClass
NativeNameSpace::ChildClass* child =
(NativeNameSpace::ChildClass*)(native);
//It's OK!!!!!!!
child->AddRef();
return gcnew ChildClass(child );
}
int main
{
NativeNameSpace::BaseClass* native = new
NativeNameSpace::ChildClass();
BaseClass::ConstructWrapper( native );
}
Obviously, the void* usage break the code down. Is there any taboo
while using void* in CLI\C++?
I think you ought to use a dynamic_cast, otherwise the void* might be
getting the address of the base sub-object, not the full object, and that
would cause the static_cast to do the wrong thing.
Thanks,
Ed.
//Assign native pointer to a void* pointer.
void* voidPtr = native;
NativeNameSpace::ChildClass* child =
dynamic_cast<NativeNameSpace::ChildClass*>(voidPtr );
Right?
On May 22, 9:42 am, Ed <seah...@gmail.comwrote:
On May 22, 4:34 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Ed wrote:
Hi, guys,
I met a very strange problem while using void* in CLI\C++.
Code like this:
CLI\C++ Code:
static BaseClass^
BaseClass::ConstructWrapper(NativeNameSpace::BaseC lass* native)
{
//Test. Add a Reference Count. It's OK
native->AddRef();
//Assign native pointer to a void* pointer.
void* voidPtr = native;
//Convert the void* to ChildClass, derived from BaseClass
NativeNameSpace::ChildClass* child =
static_cast<NativeNameSpace::ChildClass*>(voidPtr );
//NativeNameSpace::ChildClass* child =
(NativeNameSpace::ChildClass*)voidPtr ;
//It's failded !!!!!!!
child->AddRef();
return gcnew ChildClass(child );
}
int main
{
NativeNameSpace::BaseClass* native = new
NativeNameSpace::ChildClass();
BaseClass::ConstructWrapper( native );
}
If I write the code like following. I don't use void* anymore, and
it's OK.
static BaseClass^
BaseClass::ConstructWrapper(NativeNameSpace::BaseC lass* native)
{
//Test. Add a Reference Count. It's OK
native->AddRef();
//Convert the void* to ChildClass, derived from BaseClass
NativeNameSpace::ChildClass* child =
(NativeNameSpace::ChildClass*)(native);
//It's OK!!!!!!!
child->AddRef();
return gcnew ChildClass(child );
}
int main
{
NativeNameSpace::BaseClass* native = new
NativeNameSpace::ChildClass();
BaseClass::ConstructWrapper( native );
}
Obviously, the void* usage break the code down. Is there any taboo
while using void* in CLI\C++?
I think you ought to use a dynamic_cast, otherwise the void* might be
getting the address of the base sub-object, not the full object, and that
would cause the static_cast to do the wrong thing.
Thanks,
Ed.
//Assign native pointer to a void* pointer.
void* voidPtr = native;
NativeNameSpace::ChildClass* child =
dynamic_cast<NativeNameSpace::ChildClass*>(voidPtr );
Right?
I know the reason. If ChildClass is multiple inheritation, the
static_cast from void* to ChildClass is incorrect.
So, do not use void* as much as possible.
On May 22, 9:42 am, Ed <seah...@gmail.comwrote:
On May 22, 4:34 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Ed wrote:
Hi, guys,
I met a very strange problem while using void* in CLI\C++.
Code like this:
CLI\C++ Code:
static BaseClass^
BaseClass::ConstructWrapper(NativeNameSpace::BaseC lass* native)
{
//Test. Add a Reference Count. It's OK
native->AddRef();
//Assign native pointer to a void* pointer.
void* voidPtr = native;
//Convert the void* to ChildClass, derived from BaseClass
NativeNameSpace::ChildClass* child =
static_cast<NativeNameSpace::ChildClass*>(voidPtr );
//NativeNameSpace::ChildClass* child =
(NativeNameSpace::ChildClass*)voidPtr ;
//It's failded !!!!!!!
child->AddRef();
return gcnew ChildClass(child );
}
int main
{
NativeNameSpace::BaseClass* native = new
NativeNameSpace::ChildClass();
BaseClass::ConstructWrapper( native );
}
If I write the code like following. I don't use void* anymore, and
it's OK.
static BaseClass^
BaseClass::ConstructWrapper(NativeNameSpace::BaseC lass* native)
{
//Test. Add a Reference Count. It's OK
native->AddRef();
//Convert the void* to ChildClass, derived from BaseClass
NativeNameSpace::ChildClass* child =
(NativeNameSpace::ChildClass*)(native);
//It's OK!!!!!!!
child->AddRef();
return gcnew ChildClass(child );
}
int main
{
NativeNameSpace::BaseClass* native = new
NativeNameSpace::ChildClass();
BaseClass::ConstructWrapper( native );
}
Obviously, the void* usage break the code down. Is there any taboo
while using void* in CLI\C++?
I think you ought to use a dynamic_cast, otherwise the void* might be
getting the address of the base sub-object, not the full object, and that
would cause the static_cast to do the wrong thing.
Thanks,
Ed.
//Assign native pointer to a void* pointer.
void* voidPtr = native;
NativeNameSpace::ChildClass* child =
dynamic_cast<NativeNameSpace::ChildClass*>(voidPtr );
Right?
I know the reason. If ChildClass is multiple inheritance, the
static_cast from void* to ChildClass is wrong.
So do not use void* as much as possible in C++ code. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: J Wang |
last post by:
Dear,
could you tell me about the usage of "##" in preprossor
give me some simple examples.
thanks.
I just got the example from "dissection C" as follows:
|
by: Matt |
last post by:
I have 2 questions:
1. strlen returns an unsigned (size_t) quantity. Why is an unsigned
value more approprate than a signed value? Why is unsighned value less
appropriate?
2. Would there...
|
by: glenn |
last post by:
I am use to programming in php and the way session and post vars are past
from fields on one page through to the post page automatically where I can
get to their values easily to write to a...
|
by: Minh Khoa |
last post by:
Please give me more information about delegate and its usage?
Why do i use it and when?
|
by: Lalatendu Das |
last post by:
Dear friends,
I am getting a problem in the code while interacting with a nested
Do-while loop
It is skipping a scanf () function which it should not. I have written
the whole code below. Please...
|
by: Yuri_Юрий |
last post by:
I'm confused about the VARIABLE LENGTH ARRAYS.
{scanf("%d",&n);float a;}
In which compiler can I use it?
I tried VC++6.0 SP6,but it's reported error:CONSTANT EXPRESSION!
Another question,
What...
|
by: Steven T. Hatton |
last post by:
No, this is not a troll, and I am not promoting Java, C-flat, D, APL, Bash,
Mathematica, SML, or LISP. A college teacher recently posted to this
newsgroup regarding her observation that there has...
|
by: Devon Null |
last post by:
I have been exploring the concept of abstract classes and I was curious
- If I do not define a base class as abstract, will it be instantiated
(hope that is the right word) when a derived class is...
|
by: raphfrk |
last post by:
Is this valid?
int a;
void *b;
b = (void *)a; // b points to a
b += 5*sizeof(*a); // b points to a
a = 100;
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |