473,395 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 3748

"Daniel Graifer" <gr*****@earthlink.net> wrote in message
news:9I******************@newsread2.news.atl.earth link.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::~BaseClass()
{
delete p_x;
}

DerivedClass::Init( 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*****@earthlink.net> wrote in message
news:NH****************@newsread2.news.atl.earthli nk.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
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...
7
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...
11
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...
7
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...
6
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...
2
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...
7
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...
5
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
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,...

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.