-
June 17th, 2005, 01:56 AM
#1
void* in a struct
hi
i am using a struct which contains "void *" I am using void * bcoz i need to assign any data type to the vdptrVal
Code:
struct myStruct
{
CString strVal;
void *vdptrVal;
};
typedef myStruct testStruct;
assume a class foo
Code:
class foo
{
private:
foo();
virtual ~foo();
CString valStr;
int valInt;
testStruct temp;
public:
void someFoo()
{
..
}
};
foo::foo()
{
CString str = "HELLO";
valInt = 10;
temp.strVal = str;
temp.vdptrVal = (void*) &valInt;//here acutally i want to store the address of the variable valInt whose value keeps chgning
}
Now can i retrieve the value of valInt from the struct variable temp. At any point of time when the app is executing.
-
June 17th, 2005, 02:09 AM
#2
Re: void* in a struct
If a pointer's type is void *, the pointer can point to any variable that is not declared with the const or volatile keyword.
A void pointer cannot be dereferenced unless it is cast to another type.
A void pointer can be converted into any other type of data pointer.
You should be very careful when using void pointer, you must know the exact datatype the void pointer is holding when you cast back from void to specfic data type.
For Example
Code:
void *pV;
int i = 7;
int *pInt;
pV = &i;
// Cast optional in C required in C++
pInt = (int *)pV;
-
June 17th, 2005, 02:16 AM
#3
Re: void* in a struct
Is there is any problem doing this?
Code:
int i =*((int*)temp.vdptrVal) ///within member function i assume it is int GetValInt();
Life is what u make it and u can make it more simple..........and woderful.
Rate is what give and it give me pleasure.
-
June 17th, 2005, 02:23 AM
#4
Re: void* in a struct
Originally Posted by santoct2002
If a pointer's type is void *, the pointer can point to any variable that is not declared with the const or volatile keyword.
A void pointer cannot be dereferenced unless it is cast to another type.
A void pointer can be converted into any other type of data pointer.
hi thnx,
its a typo thr in my original post instead of void* say int*
Originally Posted by venkyhyd
foo::foo()
{
CString str = "HELLO";
valInt = 10;
temp.strVal = str;
temp.vdptrVal = (void*) &valInt;//here acutally i want to store the address of the variable valInt whose value keeps chgning
}
Originally Posted by upashu2
Is there is any problem doing this?
Code:
int i =*((int*)temp.vdptrVal) ///within member function i assume it is int GetValInt();
there is no problem using this but now assume if ur using this vector of type myStruct, then how like
class foo
{
private:
foo();
virtual ~foo();
CString valStr;
int valInt;
testStruct temp;
vector<testStruct > vectStruct;
public:
void someFoo()
{
..
}
};
foo::foo()
{
CString str = "HELLO";
valInt = 10;
temp.strVal = str;
temp.vdptrVal = (void*) &valInt;
vectStruct.push_back(temp);
}
here how do i retrieve the value from the vector vectStruct and assume that thr r different types variables like int, float, double ...etc...then how...
-
June 17th, 2005, 02:24 AM
#5
Re: void* in a struct
Why would you make tempStruct a member of the class Foo which contains the integer variable you are dealing with???
Also, if you know it is supposed to deal with an integer variable then why would you make a void*??? Else santoct2002's post gives the answer.
Cheers,
Exterminator.
-
June 17th, 2005, 02:30 AM
#6
Re: void* in a struct
Originally Posted by venkyhyd
here how do i retrieve the value from the vector vectStruct and assume that thr r different types variables like int, float, double ...etc...then how...
You might need to keep track of the datatype that you would be storing in testStruct's vdptrVal member. For this you could add another member in testStruct. I hope there must be some better way to accomplish this though.
Cheers
-
June 17th, 2005, 03:08 AM
#7
Re: void* in a struct
Have you anytime heard about VARIANT type?? variant data type is used to extensively in COM programming. This may help you to hold different data type value. but It needs some considerable time to spend on this datatype to use your requirements, that I leave it to you. You can get more details in MSDN and in Codeguru also. Search for Variant.
Variant
-
June 17th, 2005, 03:13 AM
#8
Re: void* in a struct
Originally Posted by santoct2002
Have you anytime heard about VARIANT type?? variant data type is used to extensively in COM programming. This may help you to hold different data type value. but It needs some considerable time to spend on this datatype to use your requirements, that I leave it to you. You can get more details in MSDN and in Codeguru also. Search for Variant.
i was giving thought on variant too but as i have never used it and never had a free hand on it...i thought of using vector
anyhow shld work on variant too, any small example will do good in using variant for me.
thnx
venky
-
June 17th, 2005, 03:30 AM
#9
Re: void* in a struct
How about a thin template?
Code:
template <typename T>
struct foo
{
void setval(T* v)
{
val_ = v;
}
T* getval() const
{
return static_cast<T*>(val_);
}
private:
void* val_;
};
Some people claim that thin templates are more space-efficient than full-blown templates, but I remain to be convinced...
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
June 17th, 2005, 03:39 AM
#10
Re: void* in a struct
Originally Posted by Graham
Some people claim that thin templates are more space-efficient than full-blown templates, but I remain to be convinced...
THIN TEMPLATE needto browse on this first then i shll try this suggestion, thnx
venky
-
June 17th, 2005, 03:46 AM
#11
Re: void* in a struct
I've never heard of a "thin" template. And with your template since you're tied to type T anyway why not use a T* pointer. Or is that the concept of a thin template (no typed data-members).
I prefer to use a binary buffer and have your class know how to create one and create itself from one such that it is a reversible process. You can then specialise common cases such as int, float, double, std::string whilst keeping it extensible.
-
June 17th, 2005, 03:51 AM
#12
Re: void* in a struct
I just looked up thin templates and found it on the Symbian page (and I know Graham works on the Symbian platform which is used for mobile-phone software, because he's stated that a few times).
The concept is that the compiler will only create one set of code for all template parameter types.
-
June 17th, 2005, 05:20 AM
#13
Re: void* in a struct
Originally Posted by Graham
Some people claim that thin templates are more space-efficient than full-blown templates, but I remain to be convinced...
I got some stuff about thin templates at this link :
http://www.codeguru.com/Cpp/Cpp/cpp_...cle.php/c7927/
There is a small note there:
Some compilers can be configured to automatically inline class member functions if they are called from class itself. This can eliminate "thin templates" effects. In the "Microsoft C++ compiler," you can use the /Ob0 command-line switch to disable inline function expansion or use functions with variable number of arguments (for example - void ThinAdd(SThinItem *a_poDst, ...) ); such functions can't be inlined.
Is this the reason (in-lining) why you are not convinced if this can be space-efficient? Any other reasons?
I just read about "thin templates" and i feel if you are separting out the template-dependent code and the template-independent code, then for sure it is going to save some extra copies of the template-independent code into the final executable.
How do we implement thin templates? I mean how is their implementation different from usual templates? The declarations etc seem same to me or am I missing something?
Cheers
-
June 17th, 2005, 05:49 AM
#14
Re: void* in a struct
Originally Posted by venkyhyd
Code:
struct myStruct
{
CString strVal;
void *vdptrVal;
};
typedef myStruct testStruct;
assume a class foo
Code:
class foo
{
private:
foo();
virtual ~foo();
CString valStr;
int valInt;
testStruct temp;
public:
void someFoo()
{
..
}
};
foo::foo()
{
CString str = "HELLO";
valInt = 10;
temp.strVal = str;
temp.vdptrVal = (void*) &valInt;//here acutally i want to store the address of the variable valInt whose value keeps chgning
}
The problem with your code is that you give another function the pointer to a function foo local variable which get's destroyed after the function foo returns, thus any other function after returning from foo cannot use this pointer anymore. If you want to store an int in the void* you can simply store the value of the integer as pointer...:
Code:
temp.vdptrVal = (void*)valInt;
And reuse it in another function.:
Code:
void foo::foo2 ( const testStruct& argument )
{
int intVal = (int)argument.vdptrVal;
// ...
}
This is just a fix for your code for understanding. There are many better solutions here than your's for example creating a template or using VARIANT type if you work on Windows.
I would go for the template.
Best Regards
-
June 17th, 2005, 05:49 AM
#15
Re: void* in a struct
Originally Posted by venkyhyd
Code:
struct myStruct
{
CString strVal;
void *vdptrVal;
};
typedef myStruct testStruct;
Just one small thing...basically there is no need for 'typedef'ing structure that way in C++....the keyword 'struct' already defines a new type...thus, you can use it directly...
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
|