473,410 Members | 1,914 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,410 software developers and data experts.

help!! to solve this c++ code

#include <iostream>
using namespace std;

class Base1
{
public:
virtual void f() { }
};

class Base2
{
public:
virtual void f() { }
};

class Base3
{
public:
virtual void f() { }
};

class Drive : public Base1, public Base2, public Base3 { };

// any non zero value because multiply zero with any no is zero

#define SOME_VALUE 1

int main()
{
cout << (int)static_cast<Base1*>((Drive*)SOME_VALUE)-SOME_VALUE <<
endl;
cout << (int)static_cast<Base2*>((Drive*)SOME_VALUE)-SOME_VALUE <<
endl;
cout << (int)static_cast<Base3*>((Drive*)SOME_VALUE)-SOME_VALUE <<
endl;
return 0;
}
output is
0
4
8
the memory size of each virtual pointer but i cant understood how the
statements in cout calculated it
Jul 22 '05 #1
2 987
On 2 May 2004 11:45:11 -0700, sr********@lycos.com (srikanth) wrote:
#include <iostream>
using namespace std;

class Base1
{
public:
virtual void f() { }
};

class Base2
{
public:
virtual void f() { }
};

class Base3
{
public:
virtual void f() { }
};

class Drive : public Base1, public Base2, public Base3 { };

// any non zero value because multiply zero with any no is zero
Well, that value isn't really being multiplied; I suspect the results you
get by setting SOME_VALUE to 0 has more to do with how NULL pointers are
special-cased (or produce undefined behavior, perhaps) than anything
relating to multiplication by zero. And if you try substituting 2, or 3,
etc., for SOME_VALUE, you'll see it increases the results by that much for
each pointer, and won't multiply it.

#define SOME_VALUE 1

int main()
{
cout << (int)static_cast<Base1*>((Drive*)SOME_VALUE)-SOME_VALUE <<
endl;
cout << (int)static_cast<Base2*>((Drive*)SOME_VALUE)-SOME_VALUE <<
endl;
cout << (int)static_cast<Base3*>((Drive*)SOME_VALUE)-SOME_VALUE <<
endl;
return 0;
}
output is
0
4
8
the memory size of each virtual pointer but i cant understood how the
statements in cout calculated it


Not "memory size", /offset/. The cast is going to offset the value you're
casting (presumed to point to the beginning of a Drive object) by the
amount necessary to point to the appropriate sub-object part you're casting
to (Base1, Base2 or Base3). To get there, it ends up having to "pass over"
the "previous" sub-objects. Each subobject has a vtbl for its one virtual
function; that accounts for the 4-bytes each occupies that must be passed
over.
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
Leor Zolman wrote:

On 2 May 2004 11:45:11 -0700, sr********@lycos.com (srikanth) wrote:
#include <iostream>
using namespace std;

class Base1
{
public:
virtual void f() { }
};

class Base2
{
public:
virtual void f() { }
};

class Base3
{
public:
virtual void f() { }
};

class Drive : public Base1, public Base2, public Base3 { };

// any non zero value because multiply zero with any no is zero


Well, that value isn't really being multiplied; I suspect the results you
get by setting SOME_VALUE to 0 has more to do with how NULL pointers are
special-cased (or produce undefined behavior, perhaps) than anything
relating to multiplication by zero. And if you try substituting 2, or 3,
etc., for SOME_VALUE, you'll see it increases the results by that much for
each pointer, and won't multiply it.

[snip]

Null pointers are converted to null pointers, provided the conversion is
well-formed (as it is in the present case); so yes, they are "special-cased".

Denis
Jul 22 '05 #3

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

Similar topics

3
by: JGBNS via DotNetMonster.com | last post by:
Hi, I am new to this forumand I apologize as i am not a .net programmer but we have a program being developed by a .net programmer. Nowwe have run into an ftp snag and I think it is part ftp and...
6
by: Mark Reed | last post by:
Hi all, I am trying to learn a little about programming (I know next to nothing so far) and have found some code which hides the toolbars. However, this bit of code is a little too effective and...
1
by: Andrej Hocevar | last post by:
Hello, below I've attached a test program reading data from a file and storing that information in a linked list. First problem is that current->p_name will print only the last item in the file...
3
by: Marek | last post by:
Hello gurus! I wrote a code in VBS, that will check, that current user is in one from three groups. But i don't know how asimilate it with asp.net. This page will be a bridge between 2 - main...
7
by: MLH | last post by:
Would love to be able to click on any If-statement and launch a utility that would find what A97 believes to be its corresponding EndIf. Such a tool would save me lots of time as I am forever...
2
by: Simon | last post by:
Hi, I need some help form someone. I use my database for my online shop, so once I have entered a order onto the database I have a button that creates the Email to the customer to let them know...
1
by: dtefran3 | last post by:
The problem Implement a SortedList class which stores a list of int data in ascending order. Program requirements The public interface of your class is as follows: class SortedList{ public:...
9
by: shapper | last post by:
Hello, I am displaying a poll using CSS and lists: http://www.27lamps.com/Beta/Poll/Poll.html At the moment I have two problems: 1. I am not able to set a padding between each Bar and the...
2
by: abdulwajeed3804 | last post by:
I am searching the code of php and mysql i have created a employee details form like id, firstname, lastname, gender, age, qualification. I have created radio buttons to male and female and if i am...
0
by: dbphydb | last post by:
Hi, The below code is doing the following 1. Reading the branch name and the destination from a txt file 2. Parsing thru HTML pages Basically, i want to deploy the build of the branch (supplied...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
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
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,...
0
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...
0
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...

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.