473,750 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Virtual Data?

Why doesn't c++ support virtual data? It supports class data of type
pointer to function (that's what virtual functions really are). In
terms of implementation, why can't I have other types of data in my vtable?

RTTI could be implemented this way instead of as a special case for the
compiler. It would also be useful for class specific memory management
implementations .

--
Daniel A. Graifer

Jul 22 '05 #1
3 3772

"Daniel Graifer" <gr*****@earthl ink.net> wrote in message
news:9I******** **********@news read2.news.atl. earthlink.net.. .
Why doesn't c++ support virtual data? It supports class data of type
pointer to function (that's what virtual functions really are). In
terms of implementation, why can't I have other types of data in my vtable?
RTTI could be implemented this way instead of as a special case for the
compiler. It would also be useful for class specific memory management
implementations .

--
Daniel A. Graifer


"Why" something is done (in the standard) probably isn't best discussed
here. But, here goes anyway...

I can't imagine where I would need to use "virtual data". Member data has a
type, and a size associated with it. If a descendant class uses the same
type of data for a member variable of the same name, then there's no need
for it to be virtual. If, however, that variable is to be of a different
type in the descendant class, then any use of the variable (i.e., any code
in a function) needs to support that different type. Then what? You end up
writing virtual functions to handle the virtual data anyway, so why use the
same variable? Why not just use a different variable, introduced in the
derived class? Or a pointer to another object (see discussion further
down)?

For example, say your base class uses the member variable x, declared as an
int. But your derived class needs a more complicated variable x, say of
type MyXClass. Now, you call a function in the class hierarchy, and that
function needs to operate on that member x. So you call some member
function ModifyX(). But that function has to be virtual, so that the
derived class can modify x as a MyXClass object and not as an int. So what
have you gained?

You can't pass the "virtual" x anywhere, because it has no known type (until
it is actually called), so no parameter list can be constructed that will
satisfy both the base class and the derived class.

If you need virtual "behavior" of a member, then make that member a pointer
to a base class object, and have your derived class create a derived-class
instance whereas your base class creates it as an instance of the base class
instance. Like this:

class BaseMember
{
int x;
BaseMember( const int initX ) : x(initX) {};
virtual ~BaseMember() {};
};

class DerivedMember : public BaseMember
{
DerivedMember( const int initX ) : BaseMember(x) {};
};

class BaseClass
{
BaseMember* p_x;
BaseClass() : x(NULL);
virtual Init( const int initX );
virtual ~BaseClass();
};

class DerivedClass : public BaseClass
{
virtual Init( const int initX );
};

BaseClass::Init ()
{
p_x = new BaseMember();
}

BaseClass::~Bas eClass()
{
delete p_x;
}

DerivedClass::I nit( const int initX )
{
p_x = new DerivedMember( initX );
}

(And, don't forget to add appropriate assignment operator and copy
constructors! I've left them out here.)
Now, p_x is a pointer to a data type which is, in effect, virtual. When you
make calls to its memebrs, they are handled by the virtual behavior of its
functions. You don't need to use the vtable for the data, because the
vtable (or whatever method is used to provide virtual behavior) is handled
in the method calling.

Will this work for you?

-Howard


Jul 22 '05 #2
Yep, that does it. Thanks.

Dan
---
Howard wrote:

Will this work for you?

-Howard

--
Daniel A. Graifer

Jul 22 '05 #3

"Daniel Graifer" <gr*****@earthl ink.net> wrote in message
news:NH******** ********@newsre ad2.news.atl.ea rthlink.net...
Yep, that does it. Thanks.
Excellent! You're welcome!

However, aside for my code not having the assigment operators and copy
constructors, I had (rather obvious) errors in the Init functions. They
should have been defined with a void return type, and the BaseClass
implementation should have had const int initX pass in, which it would then
pass to the BaseMember constructor.

Oops...sorry! :-)

-Howard
Dan
---
Howard wrote:

Will this work for you?

-Howard

--
Daniel A. Graifer

Jul 22 '05 #4

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

Similar topics

22
2957
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give me some hints on what might be wrong? // BEGIN OF SAMPLE CODE class Strategy
7
1681
by: Aguilar, James | last post by:
I've heard that virtual functions are relatively ineffecient, especially virtual functions that are small but get called very frequently. Could someone describe for me the process by which the address of a virtual function is resolved and what the hidden costs associated with virtual functions are? I've heard the same of typeof (that it is inefficient if used in high traffic code) -- if you've the time, could you explain how typeof works...
11
4367
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining it inline. Like this.
7
1778
by: Frank-René Schäfer | last post by:
Case: -- class X has occupies tiny amount of memory: sizeof(X) is only a little greater than sizeof(void*). -- X instantiates thousands of objects and memory does matter. -- The class has a virtual destructor, and therefore, a pointer to a virtual function table.
6
3137
by: Alden Pierre | last post by:
Hello, http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7 As per the link above it's wise to have a virtual deconstructor when creating an abstract class. Here is when I'm little confused. Am I deleting the right object when I call the virtual deconstructor? I was under impression when creating a class if I'm not specifically allocating memory, do not implement deconstructor and let the default take care of it...
2
1867
by: Heinz Ketchup | last post by:
Hello, I'm looking to bounce ideas off of anyone, since mainly the idea of using Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack of C++ Experience. Here is my scenario... I have 5 Derived Classes I have 3 Base Classes
7
2113
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from BufferBase, and customized in order to be able to a type of data (of any kind, such as chunks of audio samples from a sound card). The processor classes are derived from ProcessorBase, and are customized to handle BufferBase-derived objects. Each...
5
2625
by: Dennis Jones | last post by:
Hello, I have a couple of classes that look something like this: class RecordBase { }; class RecordDerived : public RecordBase {
23
4612
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
0
8999
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9575
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
9394
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...
1
9338
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6803
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...
1
3322
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
2798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.