C++ question on polymorphism (long)

C++ question on polymorphism (long)

Post by jcfree.. » Fri, 29 Sep 2000 04:00:00



I'm running into a bind on how exactly polymorphism is suppose to
work.  Every example I've seen is a variant of the following setup (
I'm running this on a Solaris Unix system, btw, but I don't think it
matters):

Abstract: Employee
concrete: Boss, HourlyWorker, SalaryWorker, etc.

EXAMPLE 1:
---------
int main(int argc, char * argv[]) {

Boss B(first,last, earnings);
HourlyWorker H(first, last, earnings);
SalaryWorker S(first, last, earnings);

Employee * ptr = &B; // or &H or &S
ptr-> print(); // pure virtual function in Employee

Quote:}

This does dynamically bind to the pointer but what practical use is
it?  The entire reason I'd want to use polymorphism is so I wouldn't
have to create an object of each type.  I'd want to something like this:

EXAMPLE 2:
---------
int main(int argc, char * argv[]) {
   int EmpType = BOSS;
   Employee * ptr = NULL;

   // get object of specific concrete class needed
   if (EmpType == BOSS) {
      ptr = new Boss(first, last, earnings);
   }
   if (EmpType == HOURLY) {
      ptr = new HourlyWorker(first, last, earnings);
   }
   if (EmpType == SALARY) {
      ptr = new SalaryWorker(first, last, earnings);
   }

   // use object generically from this point on.
   ptr->print();

Quote:}

Or better yet:
EXAMPLE 3:
---------
int main(int argc, char * argv[]) {
   int EmpType = BOSS;

   // get employee object of whatever concrete class needed
   Employee * ptr = getemployee();

   // use pointer from here on out
   ptr->print();

Quote:}

Employee * getemployee(const int EmpType) {
   if (EmpType == BOSS) {
      Boss B(first, last, earnings);
      return &B;
   }
   if (EmpType == HOURLY) {
      HourlyWorker H(first, last, earnings);
      return &H;
   }
   if (EmpType == SALARY) {
      SalaryWorker S(first, last, earnings);
      return &S
   }
   return NULL;

Quote:}

The problem with both of these examples is scope.  In example 2, The
concrete value goes out of scope when it leaves the if statement and
leaves you a pointer to nothing.  In example 3, you have actual values
but it thinks it's a object of an abstract class.  Thus when you try to
use the function print() (which is pure virtual), it will error out
since it was trying to use a purely virtual function.  IE, it no longer
thinks of itself as a Boss, HourlyWorker, or SalaryWorker class but
ONLY as an Employee class.

My main question is this:  How do I get the functionality of the bottom
two examples without running into scope issues?  I don't see a use for
polymorphism without the ability to conditionally create a specific
object of one class out of a selection of classes.

If anyone could help me out, I'd appriciate it.  I posted this in
comp.programming as well since it isn't very unix-specific but this
newsgroup sounds much more intelegent and the answer has to work on a
Solaris system ;)

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

C++ question on polymorphism (long)

Post by Fletcher Glen » Fri, 29 Sep 2000 04:00:00


You're missing the point of polymorphism.  In your example
below, all of the employee types are just that - types of
employee.  They can all share the same base class that does not
have to be written more than once.  The derived classes only
need to supply those functions that are unique to the employee
type.  Examples of things that could be in the base class are:
Address, date of hire, age, sex, and emergency contact info.
In this example the polymorphic function is computation of
pay based on type.

--
Fletcher Glenn
to reply remove NOSPAM from my reply address


> I'm running into a bind on how exactly polymorphism is suppose to
> work.  Every example I've seen is a variant of the following setup (
> I'm running this on a Solaris Unix system, btw, but I don't think it
> matters):

> Abstract: Employee
> concrete: Boss, HourlyWorker, SalaryWorker, etc.

> EXAMPLE 1:
> ---------
> int main(int argc, char * argv[]) {

> Boss B(first,last, earnings);
> HourlyWorker H(first, last, earnings);
> SalaryWorker S(first, last, earnings);

> Employee * ptr = &B; // or &H or &S
> ptr-> print(); // pure virtual function in Employee
> }

> This does dynamically bind to the pointer but what practical use is
> it?  The entire reason I'd want to use polymorphism is so I wouldn't
> have to create an object of each type.  I'd want to something like this:

> EXAMPLE 2:
> ---------
> int main(int argc, char * argv[]) {
>    int EmpType = BOSS;
>    Employee * ptr = NULL;

>    // get object of specific concrete class needed
>    if (EmpType == BOSS) {
>       ptr = new Boss(first, last, earnings);
>    }
>    if (EmpType == HOURLY) {
>       ptr = new HourlyWorker(first, last, earnings);
>    }
>    if (EmpType == SALARY) {
>       ptr = new SalaryWorker(first, last, earnings);
>    }

>    // use object generically from this point on.
>    ptr->print();
> }

> Or better yet:
> EXAMPLE 3:
> ---------
> int main(int argc, char * argv[]) {
>    int EmpType = BOSS;

>    // get employee object of whatever concrete class needed
>    Employee * ptr = getemployee();

>    // use pointer from here on out
>    ptr->print();
> }

> Employee * getemployee(const int EmpType) {
>    if (EmpType == BOSS) {
>       Boss B(first, last, earnings);
>       return &B;
>    }
>    if (EmpType == HOURLY) {
>       HourlyWorker H(first, last, earnings);
>       return &H;
>    }
>    if (EmpType == SALARY) {
>       SalaryWorker S(first, last, earnings);
>       return &S
>    }
>    return NULL;
> }

> The problem with both of these examples is scope.  In example 2, The
> concrete value goes out of scope when it leaves the if statement and
> leaves you a pointer to nothing.  In example 3, you have actual values
> but it thinks it's a object of an abstract class.  Thus when you try to
> use the function print() (which is pure virtual), it will error out
> since it was trying to use a purely virtual function.  IE, it no longer
> thinks of itself as a Boss, HourlyWorker, or SalaryWorker class but
> ONLY as an Employee class.

> My main question is this:  How do I get the functionality of the bottom
> two examples without running into scope issues?  I don't see a use for
> polymorphism without the ability to conditionally create a specific
> object of one class out of a selection of classes.

> If anyone could help me out, I'd appriciate it.  I posted this in
> comp.programming as well since it isn't very unix-specific but this
> newsgroup sounds much more intelegent and the answer has to work on a
> Solaris system ;)

> Sent via Deja.com http://www.deja.com/
> Before you buy.


 
 
 

C++ question on polymorphism (long)

Post by David Schwart » Fri, 29 Sep 2000 04:00:00



> My main question is this:  How do I get the functionality of the bottom
> two examples without running into scope issues?  I don't see a use for
> polymorphism without the ability to conditionally create a specific
> object of one class out of a selection of classes.

        Using polymorphism for object creation is not usually useful. How you
create a circle is different from how you create a square. One has a
radius, one doesn't. One has sides whose length has to be specified, one
doesn't.

        The object creation code should be specific for each type and what's
needed to create it. However, once the object it created, it can be
handed to other layers as a 'generic object' of a more general type.

        Where polymorphism is useful is when you have to perform manipulations
on an object without having to know its exact type. For example, if you
have 100 different shapes, you can draw any one of them by calling its
'Draw' function, without having to worry about whether it's a square or
a circle.

        Polymorphism is most useful across layers of abstraction. For example,
you can have 'http connection', 'pop connections', 'smtp connection',
and more, and the I/O layer can just take a 'connection' (regardless of
which type it is) and call it's 'process this chunk of data I just
received' function and not have to worry about what occurs at the
protocol layer.

        DS

 
 
 

C++ question on polymorphism (long)

Post by jcfree.. » Fri, 29 Sep 2000 04:00:00




Quote:> You're missing the point of polymorphism.  In your example
> below, all of the employee types are just that - types of
> employee.  They can all share the same base class that does not
> have to be written more than once.  The derived classes only
> need to supply those functions that are unique to the employee
> type.  Examples of things that could be in the base class are:
> Address, date of hire, age, sex, and emergency contact info.
> In this example the polymorphic function is computation of
> pay based on type.

> --
> Fletcher Glenn
> to reply remove NOSPAM from my reply address

Actually, that would be the inheritance which I do understand is a huge
benefit.  Polymorphism is where you can create an abstract class
pointer which really points to an object of one of it's concrete child
classes.  It's suppose to override the pure virtual functions/variables
with it's childs version at runtime.  Unfortunately I can't figure out
how to create a child (exact type unknown at compile time) which
doesn't go out of scope by the time its assigned to a pointer.  I don't
want to have to have the whole program repeated for each type of
employee (in that example).  I'd rather have it set the Employee type
at the front and use the same code from then on. That's what I'm having
trouble with.

Jayce Carlisle Freedom

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

C++ question on polymorphism (long)

Post by jcfree.. » Fri, 29 Sep 2000 04:00:00





> > My main question is this:  How do I get the functionality of the
bottom
> > two examples without running into scope issues?  I don't see a use
for
> > polymorphism without the ability to conditionally create a specific
> > object of one class out of a selection of classes.

>    Using polymorphism for object creation is not usually useful.
How you
> create a circle is different from how you create a square. One has a
> radius, one doesn't. One has sides whose length has to be specified,
one
> doesn't.

>    The object creation code should be specific for each type and
what's
> needed to create it. However, once the object it created, it can be
> handed to other layers as a 'generic object' of a more general type.

>    Where polymorphism is useful is when you have to perform
manipulations
> on an object without having to know its exact type. For example, if
you
> have 100 different shapes, you can draw any one of them by calling its
> 'Draw' function, without having to worry about whether it's a square
or
> a circle.

>    Polymorphism is most useful across layers of abstraction. For
example,
> you can have 'http connection', 'pop connections', 'smtp connection',
> and more, and the I/O layer can just take a 'connection' (regardless
of
> which type it is) and call it's 'process this chunk of data I just
> received' function and not have to worry about what occurs at the
> protocol layer.

>    DS

Conceptionally I understand that but I've never actually seen it work
in anything approaching real world conditions.  Every example I've ever
seen creates a 'http connection', 'pop connections', 'smtp connection',
etc and then creates a pointer to them.  They change which connection
it points to but they still have created all of the objects ahead of
time.  They never show how to create an object of the correct
connection type (and only the correct type) and go from there.  That's
the part I'm having trouble with.  How do I get it to pick one out of 2
or 3 different types and be generic from that point on.  What I'm
looking for is more like example 2 in original post.  It isn't
syntactically correct but that's the gist of what I'm trying to do.

My rant (I was very frustrated when I wrote the post) is suppose to be
more on the line of "it isn't very useful if it only works the way all
the examples I've seen work".  I know the concept of Polymorphism is
great but I've only seen it used badly.  I've never found out how to do
it correctly.

If anyone could post an example of polymorphism where you create only
one object (although there were more choices) and then create the
pointer to that object, I'd appriciate it.  It don't matter to me what
the specifics are (shapes, employees, connections, etc.), I'm just
looking for basic implementation ideas.

Thanks for your reply.
JC Freedom

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

C++ question on polymorphism (long)

Post by David Schwart » Fri, 29 Sep 2000 04:00:00



> Conceptionally I understand that but I've never actually seen it work
> in anything approaching real world conditions.  Every example I've ever
> seen creates a 'http connection', 'pop connections', 'smtp connection',
> etc and then creates a pointer to them.  They change which connection
> it points to but they still have created all of the objects ahead of
> time.  They never show how to create an object of the correct
> connection type (and only the correct type) and go from there.  That's
> the part I'm having trouble with.  How do I get it to pick one out of 2
> or 3 different types and be generic from that point on.  What I'm
> looking for is more like example 2 in original post.  It isn't
> syntactically correct but that's the gist of what I'm trying to do.

class Server
{
 public:
 virtual Connection *MakeConnection(void)=0;

Quote:}

class Connection
{
 public:
 virtual void TakeData(void *data, int len)=0;

Quote:}

class MyConnection : public Connection
{
 public:
 MyConnection(void) { ; }
 void TakeData(void *data, int len)
 {
  /* code for handling MyProtocol */
 }

Quote:}

class MyServer : public Server
{
 public:
 virtual Connection *MakeConnection(void)
 {
  return new MyConnection();
 }

Quote:}

        With this, the I/O layer can do this:

class MyTCPClass
{
 protected:
 Connection *c;

 public:

 MyTCPClass(Server *s)
 {
  c=s->MakeConnection();
 }
 SomeNetworkFunction(void)
 {
  int i=read(socket, buf, 100);
  if(i>0) c->TakeData(buf, i);
 }

Quote:}

        So your network layer (MyTCPClass) can do network I/O for any server
and protocol. To implement a protocol, you just derive one class from
Server and one from Connection. The object hierarchy hooks it all up for
you.

Quote:> My rant (I was very frustrated when I wrote the post) is suppose to be
> more on the line of "it isn't very useful if it only works the way all
> the examples I've seen work".  I know the concept of Polymorphism is
> great but I've only seen it used badly.  I've never found out how to do
> it correctly.

        Fair enough.

Quote:> If anyone could post an example of polymorphism where you create only
> one object (although there were more choices) and then create the
> pointer to that object, I'd appriciate it.  It don't matter to me what
> the specifics are (shapes, employees, connections, etc.), I'm just
> looking for basic implementation ideas.

        See my code above.
 
 
 

C++ question on polymorphism (long)

Post by Phil Edwar » Sat, 30 Sep 2000 13:00:51


[cc'd to poster]

I've rearranged your comments a little bit, so that your comments are
closer to the relevent code.

+
+ EXAMPLE 2:
+ ---------
+ int main(int argc, char * argv[]) {
+    int EmpType = BOSS;
+    Employee * ptr = NULL;
+
+    // get object of specific concrete class needed
+    if (EmpType == BOSS) {
+       ptr = new Boss(first, last, earnings);
+    }
+    if (EmpType == HOURLY) {
+       ptr = new HourlyWorker(first, last, earnings);
+    }
+    if (EmpType == SALARY) {
+       ptr = new SalaryWorker(first, last, earnings);
+    }
+
+    // use object generically from this point on.
+    ptr->print();
+ }
[...]
+ The problem with both of these examples is scope.  In example 2, The
+ concrete value goes out of scope when it leaves the if statement and
+ leaves you a pointer to nothing.

Ummmmmm... Example 2 is perfectly correct code.  The only variables which
"go out of scope" are local, automatic, stack variables.  There are only two
such variables here:  EmpType and ptr, both of which are local to main(),
not to any of the conditional bodies.

Objects created by new are on the heap.  They last until you deallocate
them, the program terminates, or the universe ends, whichever comes first.

A lot of perfectly reasonable polymorphic code works just like that, except
that the conditionals are usually in some other function, like what you
have here:

+ Or better yet:
+ EXAMPLE 3:
+ ---------
+ int main(int argc, char * argv[]) {
+    int EmpType = BOSS;
+
+    // get employee object of whatever concrete class needed
+    Employee * ptr = getemployee();
+
+    // use pointer from here on out
+    ptr->print();
+ }
+
+ Employee * getemployee(const int EmpType) {
+    if (EmpType == BOSS) {
+       Boss B(first, last, earnings);
+       return &B;
+    }
+    if (EmpType == HOURLY) {
+       HourlyWorker H(first, last, earnings);
+       return &H;
+    }
+    if (EmpType == SALARY) {
+       SalaryWorker S(first, last, earnings);
+       return &S
+    }
+    return NULL;
+ }

Maybe you meant to write "3" instead of "2" above?  There /is/ a scoping
problem here; B/H/S are local variables, and they will vanish when
getemployee() returns.  You could have getemployee() do what you do
previously, returning a newly-allocated on-the-heap object.

Pointer-to-derived can be implicitly converted to a pointer-to-base, so
that would be legal.  In fact, polymorphism wouldn't work without that
implicit conversion.

+ In example 3, you have actual values
+ but it thinks it's a object of an abstract class.  Thus when you try to
+ use the function print() (which is pure virtual), it will error out
+ since it was trying to use a purely virtual function.  IE, it no longer
+ thinks of itself as a Boss, HourlyWorker, or SalaryWorker class but
+ ONLY as an Employee class.

This sounds like a coding or design error, maybe.  If the worker classes
are publicly descended from Employee, and have implemented all the abstract
virtual member functions, then you effectively have this

    Employee * ptr;
    [....]
        ptr = new Boss;
    [....]
    ptr->print();

which is perfect code.  Can you post your class declarations?  Maybe there's
something you left out.

Luck++;
Phil

--
pedwards at disaster dot jaj dot com  |  pme at sources dot redhat dot com
devphil at several other less interesting addresses in various dot domains
The gods do not protect fools.  Fools are protected by more capable fools.

 
 
 

C++ question on polymorphism (long)

Post by jcfree.. » Sat, 30 Sep 2000 04:00:00





> +
> + EXAMPLE 2:
> + ---------
> + int main(int argc, char * argv[]) {
> +    int EmpType = BOSS;
> +    Employee * ptr = NULL;
> +
> +    // get object of specific concrete class needed
> +    if (EmpType == BOSS) {
> +       ptr = new Boss(first, last, earnings);
> +    }
> +    if (EmpType == HOURLY) {
> +       ptr = new HourlyWorker(first, last, earnings);
> +    }
> +    if (EmpType == SALARY) {
> +       ptr = new SalaryWorker(first, last, earnings);
> +    }
> +
> +    // use object generically from this point on.
> +    ptr->print();
> + }
> [...]
> + The problem with both of these examples is scope.  In example 2, The
> + concrete value goes out of scope when it leaves the if statement and
> + leaves you a pointer to nothing.

> Ummmmmm... Example 2 is perfectly correct code.  The only variables
which
> "go out of scope" are local, automatic, stack variables.  There are
only two
> such variables here:  EmpType and ptr, both of which are local to main
(),
> not to any of the conditional bodies.

The way I understand it is any time you have a {} combination, you're
defining a new block which will have it's own scope.  I've actually
compiled the code and traced it through.  Whenever it leaves the if
block, the data disappears.  Maybe this isn't true on all systems
(although I think it is) but it does create a scope block on Solaris
platform.

- Show quoted text -

Quote:> Objects created by new are on the heap.  They last until you
deallocate
> them, the program terminates, or the universe ends, whichever comes
first.

> A lot of perfectly reasonable polymorphic code works just like that,
except
> that the conditionals are usually in some other function, like what
you
> have here:

> + Or better yet:
> + EXAMPLE 3:
> + ---------
> + int main(int argc, char * argv[]) {
> +    int EmpType = BOSS;
> +
> +    // get employee object of whatever concrete class needed
> +    Employee * ptr = getemployee();
> +
> +    // use pointer from here on out
> +    ptr->print();
> + }
> +
> + Employee * getemployee(const int EmpType) {
> +    if (EmpType == BOSS) {
> +       Boss B(first, last, earnings);
> +       return &B;
> +    }
> +    if (EmpType == HOURLY) {
> +       HourlyWorker H(first, last, earnings);
> +       return &H;
> +    }
> +    if (EmpType == SALARY) {
> +       SalaryWorker S(first, last, earnings);
> +       return &S
> +    }
> +    return NULL;
> + }

> Maybe you meant to write "3" instead of "2" above?  There /is/ a
scoping
> problem here; B/H/S are local variables, and they will vanish when
> getemployee() returns.  You could have getemployee() do what you do
> previously, returning a newly-allocated on-the-heap object.

You know, I never did try that specific scenario, will tell you how
that works later.

- Show quoted text -

Quote:> Pointer-to-derived can be implicitly converted to a pointer-to-base,
so
> that would be legal.  In fact, polymorphism wouldn't work without that
> implicit conversion.

> + In example 3, you have actual values
> + but it thinks it's a object of an abstract class.  Thus when you
try to
> + use the function print() (which is pure virtual), it will error out
> + since it was trying to use a purely virtual function.  IE, it no
longer
> + thinks of itself as a Boss, HourlyWorker, or SalaryWorker class but
> + ONLY as an Employee class.

> This sounds like a coding or design error, maybe.  If the worker
classes
> are publicly descended from Employee, and have implemented all the
abstract
> virtual member functions, then you effectively have this

>     Employee * ptr;
>     [....]
>         ptr = new Boss;
>     [....]
>     ptr->print();

> which is perfect code.  Can you post your class declarations?  Maybe
there's
> something you left out.

I think what happens is that when it loses the concrete class (by going
out of scope), it doesn't know what to use so it assumes that the
pointer is pointing to a base (in this case abstract) class.  When the
program tries to use the virtual function, it generates a runtime
exception. The derived classes are perfectly decending from the base
class but when the derived class leave scope, it no longer knows what
derived class it is suppose to be.  I was supprised it even got as far
as it did by accessing a pointer to nothing.  The error message
read, "Pure virtual function called".

Quote:> Luck++;
> Phil

cute ;)
> --
> pedwards at disaster dot jaj dot com  |  pme at sources dot redhat
dot com
> devphil at several other less interesting addresses in various dot
domains
> The gods do not protect fools.  Fools are protected by more capable
fools.

Sent via Deja.com http://www.deja.com/
Before you buy.
 
 
 

C++ question on polymorphism (long)

Post by jcfree.. » Sat, 30 Sep 2000 04:00:00




> > + EXAMPLE 3:
> > + ---------
> > + int main(int argc, char * argv[]) {
> > +    int EmpType = BOSS;
> > +
> > +    // get employee object of whatever concrete class needed
> > +    Employee * ptr = getemployee();
> > +
> > +    // use pointer from here on out
> > +    ptr->print();
> > + }
> > +
> > + Employee * getemployee(const int EmpType) {
> > +    if (EmpType == BOSS) {
> > +       Boss B(first, last, earnings);
> > +       return &B;
> > +    }
> > +    if (EmpType == HOURLY) {
> > +       HourlyWorker H(first, last, earnings);
> > +       return &H;
> > +    }
> > +    if (EmpType == SALARY) {
> > +       SalaryWorker S(first, last, earnings);
> > +       return &S
> > +    }
> > +    return NULL;
> > + }

> > Maybe you meant to write "3" instead of "2" above?  There /is/ a
> scoping
> > problem here; B/H/S are local variables, and they will vanish when
> > getemployee() returns.  You could have getemployee() do what you do
> > previously, returning a newly-allocated on-the-heap object.

> You know, I never did try that specific scenario, will tell you how
> that works later.

That was exactly what my problem was!  When I used new to create the
child in the function, it didn't disappear when I passed it back up.
It's the simple things that always trip me up.  I should have realized
that earlier but the heap always did cause me problems.  Not sure I
have a great grasp on exactly how that works.

Thanks again for your help (and everyone else for that matter).  My
problem is solved and all is right with the world!

JC

PS: See, I knew you guys were smarter ;)

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

C++ question on polymorphism (long)

Post by Phil Edwar » Sat, 30 Sep 2000 04:00:00


[cc'd to poster]

+ > > Maybe you meant to write "3" instead of "2" above?  There /is/ a
+ > scoping
+ > > problem here; B/H/S are local variables, and they will vanish when
+ > > getemployee() returns.  You could have getemployee() do what you do
+ > > previously, returning a newly-allocated on-the-heap object.
+ >
+ > You know, I never did try that specific scenario, will tell you how
+ > that works later.
+
+ That was exactly what my problem was!  When I used new to create the
+ child in the function, it didn't disappear when I passed it back up.
+ It's the simple things that always trip me up.  I should have realized
+ that earlier but the heap always did cause me problems.  Not sure I
+ have a great grasp on exactly how that works.

Have you taken any classes or read any textbooks on data structures?
If not, I /highly/ recommend that you do.  If you have, then it's not too
bad once somebody explains it to you, trust me.  :-)  I understand it but
can't explain it very well.

Basically, the heap is where all the dynamically-allocated stuff goes.
There's no "top" or "bottom" like there is with the program's runtime stack.
(Well, there are, but they're not of normal concern to the programmer.)
When you malloc/new an object, "the system" makes space in the heap and
puts it there.  And it stays there until you free/delete it.

The only automatic allocation/deallocation happens on the stack.

I've skipped so many details in the last two paragraphs it's frightening.
:-)

I'd also recommend reading some good C++ texts to see how some of the
funkier rules interact with all of this.  The two "Effective" books (or
one CD) from Scott Meyers are excellent.

+ PS: See, I knew you guys were smarter ;)

Plaugh.  Smarts have nothing to do with it.  Hard-won experience, is all,
and we've the scars to show it.  (Someday I'll tell you the story of when
I was trying to call 'delete' on an object... that was actually sitting
on the stack instead of the heap.  That was a /*/ to debug.)

Phil

 
 
 

1. Polymorphism is C++

Hello all out there in Linux netherLand!!!
I have a simple question:

I am trying to create a linked matrix of rows.

rows are linked units of a polymorphic base object.

I would like the base object to be able to hold a variety of types ie int,
double, char, and maybe char[] if possible.

Is there a way to inherit base objects and cast them to derived object
types at run time?   This way you you minimize overhead memory
requirements and maximize efficiency?

My project is to make a spreadsheet process which is capable of handling
the above types.  The linked matrix setup is to facilitate editing, I have
proveded functions that  would dynamically allocate the array (2
dimentions of it) and  pass a pointer of it to a variable which is then
used for all other manipulation.  This way, I reduce the need to  make n
explicit calls to the nextPointer() function  to get to the nth  unit.

To edit the matrix again, I will provide a mechanism for such an array to
be fed back into a linked list...

Anyway, any suggestions to the polymorphism problem, or other methods of
implementing this?

LCamBilARgiu

2. PPPD problem

3. Using the long long type in C and C++ functions

4. Can someone help ? (Linux1.2.1/gcc 2.5.8 TCP/IP gethostent/byname)

5. long long & long double types in Linux GCC

6. News Feed Problems

7. RH7.2 2.4.X off_t: long or long long?

8. httpd apache

9. Linux has a long, long, long way to go

10. these 3 questions from my C++ E-mail group, and my reply to C++ group

11. Question re: Best compiler...Microsoft Visual C/C++ vs. Symantec's Visual C/C++

12. Shared Object + polymorphism

13. Sunpro C++ 4.2: Dependency.state file format and long compilation times