-
March 25th, 2013, 05:59 AM
#1
Passing variant variable through LPVOID
Dear Gurus,
I have create a function which able to return value for variant of variable through a LPVOID parameter function.
But some how it cannot work.
Maybe i have did wrong in some point.
Would like you to help me.
Thanks.
Here is my coding.
Code:
void main()
{
CClassA a;
BOOL bAssign=FALSE;
CString str;
a.GetValue("BOOL", &bAssign);
a.GetValue("STRING", &str);
//i just cannot get the value from class A.
}
CClassA
{
//these are member variable
CString string="Test";
BOOL bAssigned = TRUE;
//class func
void GetValue(CString strType, LPVOID value)
{
if(strType == "BOOL")
{
value = reinterpret_cast<BOOL*>(bAssigned);
}
else if(strType == "STRING")
{
value = string.GetBuffer(string.GetLength());
}
}
}
Is my way correct? if it is wrong kindly point out to me where is my mistake.
Thanks.
-
March 25th, 2013, 08:51 AM
#2
Re: Passing variant variable through LPVOID
Classes cannot have assignments made to member variables as part of the class definition. Only integral static members can have values assigned as part of the definiton. You will need to have some way of setting these class members from outside the class definition (eg SetValue(..)) before their value can be retrieved.
All elements of a class are private by default. That means that they cannot be accessed outside of the class. So the class func Getvalue is private and so cannot be accesssed in your main function. To make a function/member public and so can be accessed outside of the class then public: is used before the definition.
Also, a class definition starts with the word class. So you would have
Code:
class CClassA
{
//these are member variable
CString string;
BOOL bAssigned;
public:
//class func
void GetValue(CString strType, LPVOID value)
{
if(strType == "BOOL")
{
value = reinterpret_cast<BOOL*>(bAssigned);
}
else if(strType == "STRING")
{
value = string.GetBuffer(string.GetLength());
}
}
};
Also, trying to do what you are doing by returning a pointer to a class variable is not receommended. You are better doing it via function overload
Code:
class CClassA
{
//these are member variables - private
string strin;
BOOL bAssigned;
//these functions are public
public:
//get BOOL value
void GetValue(BOOL& bo) {
bo = bAssigned;
}
//get string value
void GetValue(string& str) {
str = strin;
}
//set BOOL value
void SetValue(BOOL bo) {
bAssigned = bo;
}
//set string value
void SetValue(const string& str)
{
strin = str;
}
};
int main()
{
CClassA a;
BOOL bAssign;
string str;
a.SetValue(TRUE);
a.SetValue("qwerty");
a.GetValue(bAssign);
a.GetValue(str);
cout << "bool is: " << bAssign << endl;
cout << "string is: " << str;
return (0);
}
Hope this helps your understanding.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 25th, 2013, 10:22 AM
#3
Re: Passing variant variable through LPVOID
Dear 2kaud,
Yes. I understand.
Regarding the coding, it was an example of what i trying to do.
Sorry that there are syntax errors...
About the answer that you suggest, it was an alternative way to do it.
But i was thinking just to create a func rather that many overload func.
Is there a way of doing what i'm trying to do by using a function which pass variant of type of variable?
Thanks.
-
March 25th, 2013, 11:02 AM
#4
Re: Passing variant variable through LPVOID
Dear 2Kaud,
I have solved my problem.
Code:
void main()
{
CClassA a;
BOOL bAssign=FALSE;
CString str;
a.GetValue("BOOL", &bAssign);
a.GetValue("STRING", &str);
//Now the value bAssign = TRUE, str="Test"
}
class CClassA
{
//these are member variable
CString string="Test";
BOOL bAssigned = TRUE;
//class func
void GetValue(CString strType, LPVOID value)
{
if(strType == "BOOL")
{
*(reinterpret_cast<BOOL*>(value)) = bAssigned; //just need to cast the LPVOID to the variable type. That's it.
}
else if(strType == "STRING")
{
*(reinterpret_cast<CString*>(value)) = string;
}
}
}
Thank you.
-
March 25th, 2013, 11:18 AM
#5
Re: Passing variant variable through LPVOID
I don't know what complier you are using, but the code you have posted doesn't comply with ANSI c++ and won't be portable across different compilers eg. your code doesn't compile with Microsoft Visual Studio. I would suggest strongly that unless there is a very good reason you should write ANSI compliant c++.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 28th, 2013, 09:00 PM
#6
Re: Passing variant variable through LPVOID
Dear 2Kaud,
I'm using Microsoft Visual Studio 2008 compiler.
Of cause you can not copy direct the coding from my post cause there are syntax error.
But the casting part is correct.
Thanks.
-
March 28th, 2013, 09:55 PM
#7
Re: Passing variant variable through LPVOID
Originally Posted by VbEndUser
Dear Gurus,
I have create a function which able to return value for variant of variable through a LPVOID parameter function.
The last resort should have been to go the LPVOID route.
First, you need to tell us eventually this: "who will really know what the data type should be?".
In your example program, you already know the data type. Where in your real application is it determined that the data type is indeed a CString, a bool, etc.? Once you tell us that, then you will more than likely get better, more typesafe, and basically more elegant solutions than supplying an endless bunch of if() statements (your current solution).
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|