![]() |
| | #16 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Quote:
All you need is a pointer or reference to the base class. Thus this will work: Code: Derived a; Base* pa = &a; pa->myfunction(); // Or Base& ra = a; ra.myfunction(); If you don't want to do manual deleting, then use smart pointers.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
Last edited by Elysia; 07-09-2009 at 12:37 PM. | ||
| Elysia is offline | |
| | #17 |
| Registered User Join Date: Apr 2006 Location: United States
Posts: 3,201
| Well that's a crap way to demonstrate polymorphism, since that's not polymorphic at all. Imagine two classes to represent the time, TwelveHourClock and TwentyFourHourClock, related through the abstract data type Time, which at minimum specifies for its children a timestamp method. One way to dump the time for all the Time types is through a polymorphic function. Code: void print (std::ostream& os, const Time& now) {
os << "The time is " << now.timestamp() << '\n';
}
__________________ Os iusti meditabitur sapientiam Et lingua eius loquetur indicium "There is nothing either good or bad, but thinking makes it so." (Shakespeare, Hamlet, Act II scene ii) http://www.myspace.com/whiteflags99 |
| whiteflags is offline | |
| | #18 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| It was a way to demonstrate that you don't have to use new to use polymorphism.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #19 |
| Registered User Join Date: Apr 2006 Location: United States
Posts: 3,201
| But you didn't use polymorphism, so sorry if it's really my fault that you demonstrated so little.
__________________ Os iusti meditabitur sapientiam Et lingua eius loquetur indicium "There is nothing either good or bad, but thinking makes it so." (Shakespeare, Hamlet, Act II scene ii) http://www.myspace.com/whiteflags99 |
| whiteflags is offline | |
| | #20 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Alright, perhaps it was a bad example. I realize that now... I used a single class. Well, I edited it to use Derived and Base to demonstrate that we're talking about polymorphism.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #21 | |
| Super Moderator Join Date: Aug 2001
Posts: 7,470
| Quote:
__________________ If you aim at everything you will hit something but you won't know what it is. | |
| Bubba is offline | |
![]() |
| Tags |
| class, namespace |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| question about a working linked list | cold_dog | C++ Programming | 23 | 09-13-2006 01:00 AM |
| data validation & namespace | tiange | C++ Programming | 4 | 07-05-2005 02:45 AM |
| Templated Binary Tree... dear god... | Nakeerb | C++ Programming | 15 | 01-17-2003 02:24 AM |
| can't insert data into my B-Tree class structure | daluu | C++ Programming | 0 | 12-05-2002 06:03 PM |
| Post programs | GaPe | C# Programming | 8 | 05-12-2002 11:07 AM |