home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!pipex!bnr.co.uk!uknet!fulcrum!bham!bhamvx!mccauleyba
- From: mccauleyba@vax1.bham.ac.uk (Brian McCauley)
- Newsgroups: comp.lang.c++
- Subject: Re: Sizeof and Virtual functions
- Message-ID: <1992Dec24.132121.1@vax1.bham.ac.uk>
- Date: 24 Dec 92 13:21:21 GMT
- References: <1992Dec24.061254.9763@nuscc.nus.sg>
- Organization: University of Birmingham
- Lines: 53
- NNTP-Posting-Host: vax1.bham.ac.uk
-
- In article <1992Dec24.061254.9763@nuscc.nus.sg>, suresh@papaya.iss.nus.sg (Suresh Thennarangam - Research Scholar) writes:
- > One of the things that distinguishes virtual functions from other
- > normal member functions of a class is that some compilers that I
- > know of, store an extra pointer that is probably used to index
- > a vector table. This is done to facilitate late binding
- > at run-time depending on the type of the object being pointed to
- > rather than the type of the object pointer as opposed to normal
- > member functions calls which are bound at compile time.
- >
- > Is this an implementation-specific issue only ?
- Ish.
-
- >
- > A C++ programmer should not have to know of these ugly little pointers
- > lurking around in his structures. These monsters add 4 bytes each
- > (on a 32 bit machine) to your structure; sizeof no longer tells the
- > truth and seemingly innocent actions like fwrite'ing such structures
- > to a file can quickly assume rather pernicious connotations.
- >
- > If I write classes with virtual functions to interpret and save
- > file formats like XWD, TIFF, GIF , etc and innocently use sizeof()
- > to calculate the size and write to a file I will be writing along
- > addresses in my machines data area. Obviously, no one will be able
- > to read those files either. Of course an experienced C++ programmer
- > would know how work around this problem but this just an illustration.
- >
- > Isn't there a better way to implement virtual functions ?
- No. The only other way would be to include the type information in the pointer
- itself so all pointers to objects of classes with virtual functions would
- become bigger. This would also mean that if a pointer were converted to void*
- and back it would loose it's type information. (I'm not sure if a compiler
- with this property would be conforming but it would certainly be unpopular).
-
- Another solution would be to adjust the action of the `sizeof' and `(void*)'
- operators to refer to the object excluding its type indicator but
- this would break any code that used `malloc'.
-
- > Isn't the current implementation a clever hack at best ?
- Possibly but the only alternative would be a clumsy hack.
-
- It is better to have `sizeof' and interconversion to `void*' do what
- they do now rather than try to hide the type information outside of the
- object.
-
- Almost any use of void* and sizeof is implementation dependant, they are
- really part of C and C++ programmers should avoid them whenever possible.
- --
- \\ ( ) No Bullshit! | Email: B.A.McCauley@bham.ac.uk
- . _\\__[oo from | Voice: +44 21 471 3789 (home)
- .__/ \\ /\@ /~) /~[ /\/[ | Fax: +44 21 625 2175 (work)
- . l___\\ /~~) /~~[ / [ | Snail: 197 Harborne Lane,
- # ll l\\ ~~~~ ~ ~ ~ ~ | Birmingham, B29 6SS, UK
- ###LL LL\\ (Brian McCauley) | ICBM: 52.5N 1.9W
-