LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-04-2009, 04:36 AM   #1
jackandking
Member
 
Registered: Dec 2004
Posts: 92

Rep: Reputation: 15
Question virtual and sizeof


class A{
virtual void foo(){};
};
sizeof(A)=1

class B : public virtural A{
virtual void foo(){};
};
sizeof(B)=? and why
 
Old 05-04-2009, 05:13 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I do not no how you drew the conclusion that the size of class A is 1 byte.

Anytime a class has a virtual method declared within it, the class size will at a minimum, be 4 bytes. The is the number of bytes necessary to hold the address of the the location of the virtual table associated with this class.

When you subclass A, to form class B, the address of the virtual table is still 4 bytes; thus using the code as you have presented, the size of class B will also be 4 bytes.
 
Old 05-04-2009, 08:16 PM   #3
jackandking
Member
 
Registered: Dec 2004
Posts: 92

Original Poster
Rep: Reputation: 15
Yes, you are right about sizeof(A)=4, sorry for my thoughtless.

About sizeof(B)=4, I am confused, how about sizeof(C) list below:
class C : public A{
virtual void foo(){};
};
 
Old 05-05-2009, 04:56 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
It would not kill you to write a simple application with the classes that you have defined, include <iostream>, and print out the sizeof() value returned for each of the classes.

Class C will also have a sizeof() 4.
 
Old 05-05-2009, 09:08 PM   #5
jackandking
Member
 
Registered: Dec 2004
Posts: 92

Original Poster
Rep: Reputation: 15
sorry for my laziness.

why sizeof(C) is 4?
what's the difference between class B and C?
I thought virtually inheritant should import some difference to class B.
 
Old 05-06-2009, 11:37 AM   #6
amysaraantony
Member
 
Registered: Apr 2009
Posts: 42

Rep: Reputation: 16
Quote:
Originally Posted by jackandking View Post
sorry for my laziness.

Bad Boy jackandking .... Really Bad Boy !!!

Debian

Last edited by amysaraantony; 05-15-2009 at 08:12 PM.
 
  


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
C and sizeof exvor Programming 5 08-11-2007 11:27 PM
C++ template and sizeof dmail Programming 2 08-03-2006 08:01 AM
sizeof problem ltordsen Programming 8 12-05-2005 09:13 AM
sizeof( ) in c++ question amoor757 Programming 6 01-26-2004 02:42 PM
sizeof() and C/C++ boku Programming 2 09-01-2002 07:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:05 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