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

About void* usage in CLI/C++

Ed
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.
Jun 27 '08 #1
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.

Jun 27 '08 #2
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?
Jun 27 '08 #3
Ed
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.
Jun 27 '08 #4
Ed
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.
Jun 27 '08 #5

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

Similar topics

3
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:
81
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...
10
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...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
33
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...
7
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...
75
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...
4
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...
160
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;
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: 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$) { } ...
0
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...
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: 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
Oralloy
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,...

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.