473,856 Members | 1,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A easy problem about Polymorphic

Compiler store a pointer table in each object with virtual fuction to
implement polymorphic,isn 't it? But where is the pointer table?
It seems that object only store a pointer to the table,the output of
this code is 88.
#include<iostre am>
using namespace std;
class MyClass{
public:
MyClass(){ a=0;}
virtual void tst1() {a-=1;}
virtual void tst2(){a*=2;}
virtual void tst3(){a*=1;}
private:
int a;
};
class Tst:public MyClass{
void tst1() {std::cout<<"Ts t"<<endl;}
void tst2(){std::cou t<<"Tst"<<endl; }
void tst3(){std::cou t<<"Tst"<<endl; }
};
int main()
{
//Tst tstObject;
//MyClass *tstp=&tstObjec t;
//tstp->sub();
Tst tstObject;
MyClass myObject;
cout<<sizeof(my Object);
cout<<sizeof(ts tObject);
return 1;
}

Nov 9 '06 #1
4 1706
when base class has virtual functions, a v_ptr that point to the
v_table will be add
so, the object's size is the sizeof(v_ptr) + sizeof(data member)
"Osamede.Zh ang д
"
Compiler store a pointer table in each object with virtual fuction to
implement polymorphic,isn 't it? But where is the pointer table?
It seems that object only store a pointer to the table,the output of
this code is 88.
#include<iostre am>
using namespace std;
class MyClass{
public:
MyClass(){ a=0;}
virtual void tst1() {a-=1;}
virtual void tst2(){a*=2;}
virtual void tst3(){a*=1;}
private:
int a;
};
class Tst:public MyClass{
void tst1() {std::cout<<"Ts t"<<endl;}
void tst2(){std::cou t<<"Tst"<<endl; }
void tst3(){std::cou t<<"Tst"<<endl; }
};
int main()
{
//Tst tstObject;
//MyClass *tstp=&tstObjec t;
//tstp->sub();
Tst tstObject;
MyClass myObject;
cout<<sizeof(my Object);
cout<<sizeof(ts tObject);
return 1;
}
Nov 9 '06 #2

"Bluse.huan g д
"
when base class has virtual functions, a v_ptr that point to the
v_table will be add
so, the object's size is the sizeof(v_ptr) + sizeof(data member)
But where is v_table ?heap??textseg?

Nov 9 '06 #3
Osamede.Zhang wrote:
Compiler store a pointer table in each object with virtual fuction to
implement polymorphic,isn 't it? But where is the pointer table?
It seems that object only store a pointer to the table,the output of
this code is 88.
Making a class polymorphic (at least one virtual function) is
likely to incur overhead. The nature of the overhead is
implementation specific. While vtables and vptrs are a
common way to do this, do not be fooled into believing that
this is defined by the language.

In the common implementation, each object of a polymorphic
type will have a pointer to a table. Each defined class
will create this table somewhere in memory and set it's
objects pointer to it during construction.
Nov 9 '06 #4
Bluse.huang wrote:
when base class has virtual functions, a v_ptr that point to the
v_table will be add
so, the object's size is the sizeof(v_ptr) + sizeof(data member)

There might also be some padding. The object size is not necessarily
the sum of it's non-static members.
Nov 9 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
27669
by: Maurice Termeer | last post by:
Hi, suppose i've got this: class a { public: int n; }; class b : public a { public: };
20
2238
by: verec | last post by:
One problem I've come accross in designing a specific version of auto_ptr is that I have to disntiguish between "polymorphic" arguments and "plain" ones, because the template has to, internally, cast to void *. Specifically, template <typename T> void f(T * t) { void * p = dynamic_cast<void *>(t) ; } will not compile if T isn't of a class that has somewhere at least
1
2266
by: verec | last post by:
Last week I asked here how I could detect that a T was polymorphic, and received very thoughtful and useful replies that I used straight away. Thanks to all who answered. This week, it turns out that detecting whether T is polymorphic clashes with a new requirement, that T be allowed to not be complete. Last week, this code used to compile:
7
2084
by: James Fortune | last post by:
In response to different users or situations (data context) I transform the appearance and characteristics of Access Forms through code. This seems to fit in with the idea of polymorphism. Do people consider Access Forms to be Polymorphic? James A. Fortune
36
2567
by: utab | last post by:
Dear, I have experince in C( numerical projects, like engineering problems, scientific applications) I have the basic notion of C++ also, I have read Accelerated C++ until Chapter 7, however it seems that it discusses the std and the other part of the language with your own abstractions. Is that better to read a book first on the basic concepts of C++ language (but not the C part) that gives the basics as if the reader is a beginner...
27
2163
by: Chad | last post by:
The problem is: Write a recursive version of the function reverse(s), which reverses the string s in place. In "The C Answer Book", Second Edition, near the bottom of page 95, the authors say "This is not a good application of recursion". Just for the record, I did make an attempt at the solution before I broke down and looked at the solution in the book.
17
3558
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
7
1626
by: Arindam | last post by:
#include <cstdio> struct Test { void bar() { foo(); } private: virtual void foo() { printf("Test\n"); }
7
1433
by: tradevol | last post by:
as this example, question: If my purpose is initialize data from xml files and store them in the vector, so they can be used in class B by other member functions, do you think functionP is a viable function(will a could go away after out of the function)? If not, is there a better solution than using functionPt?
0
11061
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10699
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7932
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7098
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5762
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5960
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4578
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4176
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.