LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-12-2013, 03:58 AM   #1
v3ct0r
Member
 
Registered: Jun 2013
Location: China
Distribution: Archlinux
Posts: 67

Rep: Reputation: Disabled
About polymorphism in C


Hey guys, I defined a stack data structure a couple days ago,
Code:
struct linknode {
        elemtype data;
        struct linknode *
                last;
};

struct linkstack {
        struct linknode *top;
};
typedef struct linknode *lnode;
typedef struct linkstack *lstack;
;
And I've also defined some functions for these two.

Look I didnt assign what elemtype is in my structure. Now I have to use two stack and their elemtype are different(one is double, one is char)!!!

The problem is I dont wanna re-write two kind of stack, and replace all elemtype with char(or double). To do this, I'll have to implement functions for each one.

Is there a way out?

Some guys says I should use (void *) for elemtype, but there are two problem with it.

1: gcc dont let me assign double to void *.

2: void *data is a pointer, it must point to somewhere(probably in heap memory).
When I need to change the data 'data' pointer to, and 'data' probably dont know that.
eg:
Code:
int c =3;
void *a = c;
c = 4;     //I need c to change for something else, of course.
So guys, is there really a way out?
 
Old 10-12-2013, 05:57 AM   #2
v3ct0r
Member
 
Registered: Jun 2013
Location: China
Distribution: Archlinux
Posts: 67

Original Poster
Rep: Reputation: Disabled
Ha, I found that type casting just slove my problem, though it looks a little weird. Never mind this thread, pal.
 
Old 10-12-2013, 06:25 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by xeechou View Post
Ha, I found that type casting just slove my problem, though it looks a little weird. Never mind this thread, pal.
You are making an assumption there.

On most systems the sizeof(double) is the same as sizeof(void *)... But that isn't true on 32 bit systems. Fortunately, sizeof(double) > sizeof(void *) on those systems.

You would do better to use a tagged structure. Something like:
Code:
struct data {
    int     tag;        /* the tag */
    union {
        double   df;    /* tag = 0 */
        void     *ptr;  /* tag = 1 */
        ...
    }datum;
}
This avoids the casting, AND you can identify which data type you actually have in the tag value. For instance, if tag is 0 (for double) then your code will know not to use the datum.ptr reference--- it is invalid. And if the tag is 1, the code should not datum.df -- it is likely an invalid format for a double.
 
1 members found this post helpful.
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
polymorphism icecubeflower Programming 20 09-10-2009 12:23 AM
overloading vs polymorphism lbdgwgt Programming 1 03-06-2007 02:30 PM
Polymorphism problem lucky6969b Programming 3 12-29-2005 02:59 AM
Polymorphism in VBA ashirazi Programming 4 07-05-2005 04:44 PM
polymorphism in java jwn7 Programming 4 11-17-2004 04:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration