Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pure member functions

Reply
Thread Tools

pure member functions

 
 
Pete Becker
Guest
Posts: n/a
 
      12-19-2007
On 2007-12-18 22:31:02 -0500, Rahul <(E-Mail Removed)> said:

> On Dec 19, 12:00 am, Kira Yamato <(E-Mail Removed)> wrote:
>> On 2007-12-18 13:52:49 -0500, Rahul <(E-Mail Removed)> said:
>>
>>
>>> I'm trying to think the reason behind this restriction... i just want
>>> to want a base class to be abstract so as to avoid object slicing into
>>> the base type from derived type, and in my case base class doesn't
>>> need to have a virtual function.

>>

>
> fun(A obj)
> {
> }
>
> int main()
> {
> B obj;
> fun(obj); // Object of type B is upcasted into type of A and it
> is sliced. If i had a pure virtual member function in A, this could
> have been avoided, i would have basically got a compilation error...
> }


If A was an abstract class, you wouldn't be able to call fun at all,
because you wouldn't be able to create an A object for its argument.

A obj; // illegal: A is abstract
fun(obj); // can't get here

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

 
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
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
pure virtual member functions Rahul C++ 3 12-02-2007 06:57 AM
private virtual functions and pure virtual functions with bodies John Goche C++ 10 12-08-2006 04:00 PM
Pure functions still pure after definition Todd Aspeotis C++ 3 05-30-2005 03:53 AM



Advertisments