Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > void* question

Reply
Thread Tools

void* question

 
 
Tomás Ó hÉilidhe
Guest
Posts: n/a
 
      02-11-2008
Janet:

> Maybe I should elaborate on the question. Basically I'm coming from
> JAVA and trying to implement polymorphism via an array of function
> pointers.



Rather than an array of function pointers, we simply have a struct
containing different kinds of function pointer, reason being that we
have the freedom to specify different return values and arguments for
each of the functions.


> The functions all take the same arguments, but they have different
> return values, e.g.
> int f1(int x, int y);
> double f2(int x, int y);



Yes, see, here is your problem. You should use a struct instead.

Imagine you had the following in Java:

(I don't actually know Java so I'm going to write C++ code, but
hopefully you'll get the idea)

class BaseClass {
public:

virtual void Speak(void) { WriteToScreen("I like ice-cream"); }
virtual double GetValue(void) { return 7.2; }
};

class Derived1 : BaseClass {
public:

virtual void Speak(void) { WriteToScreen("I like custard"); }
virtual double GetValue(void) { return 2.44; }
};

class Derived2 : BaseClass {
public:

virtual void Speak(void) { WriteToScreen("I like jelly"); }
virtual double GetValue(void) { return 63.8; }
};


If we wanted to implement this in C, then we'd have:


struct VTable {
void (*const Speak)(void);
double (*const GetValue)(void);
};

struct BaseClass {
VTable const *const pvt;
};

struct Derived1 {
VTable const *const pvt;
};

struct Derived2 {
VTable const *const pvt;
};

Then, if you wanted to invoke Speak on an object, you'd do:

void ExploitPolymorphism(Derived2 *const p)
{
p->pvt->Speak();

p->pvt->GetValue();
}


(And yes, the methods I've written should of course have been declared
const, but I left it out so as not to clutter things).

--
Tomás Ó hÉilidhe
 
Reply With Quote
 
 
 
 
Peter 'Shaggy' Haywood
Guest
Posts: n/a
 
      02-12-2008
Groovy hepcat Tomás Ó hÉilidhe was jivin' in comp.lang.c on Mon, 11 Feb
2008 11:17 am. It's a cool scene! Dig it.

> Janet:
>
>> What is the major reason for using void*?
>> When should we use void* for both input arguments and return value?
>> How can we cast the void* pointer to the type we need?
>> Thanx

>
> A void* is really just a char* that has few more "features" (...or
> even "lack of features").


Wrong, I'm afraid. A void * is not a char * at all. Although they do
have the same size and representation, this does not make them the same
thing, by any means.

> The differences between char* and void* are:
> 1) void* has implicit conversion to and from every other pointer type.


Wrong again. void * has implicit conversion to and from every pointer
to object and incomplete type, but not pointer to function type.

--
Dig the sig!

----------- Peter 'Shaggy' Haywood ------------
Ain't I'm a dawg!!
 
Reply With Quote
 
 
 
 
Tomás Ó hÉilidhe
Guest
Posts: n/a
 
      02-13-2008
Peter 'Shaggy' Haywood:

> Although they do have the same size and representation, this does not
> make them the same thing, by any means.



Same size and representation is good enough for me -- they're the same
thing just with a different interface to the programmer.


> void * has implicit conversion to and from every pointer
> to object and incomplete type, but not pointer to function type.



Nice catch, thanks for the correction.

--
Tomás Ó hÉilidhe
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      02-13-2008
Tomás Ó hÉilidhe wrote:
> Peter 'Shaggy' Haywood:
>
>> Although they do have the same size and representation, this does not
>> make them the same thing, by any means.

>
>
> Same size and representation is good enough for me -- they're the same
> thing just with a different interface to the programmer.
>

Try dereferencing them and see what you get...

--
Ian Collins.
 
Reply With Quote
 
Tomás Ó hÉilidhe
Guest
Posts: n/a
 
      02-13-2008
Ian Collins:

>> Same size and representation is good enough for me -- they're the same
>> thing just with a different interface to the programmer.
>>

> Try dereferencing them and see what you get...



I covered this in my list of differences between char* and void* upthread.

--
Tomás Ó hÉilidhe
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments