473,505 Members | 16,800 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this virtual class defined in c++ standards?

#include <iostream>
using namespace std;

virtual class Class {
public:
void fun() { cout << "Class::fun" << endl; }
};

int main() {
Class objClass;
cout << "Size of Class = " << sizeof(objClass) << endl;
cout << "Address of Class = " << &objClass << endl;
return 0;
}

normal size of class with no variables is 1 if there is a virtual
function size will be 4, but here the scenario is different its a
virtual class instead of virtual function. The program compiled
successfully in Visual Studio 2005. I expected a vptr to be inserted
in it. instead nothing like that. size of Class was 1 instead of 4. Is
this some kind of Undefined Behaviour?

Jul 6 '07 #1
6 1357
On Jul 6, 2:40 pm, Anarki <Deepchan...@gmail.comwrote:
#include <iostream>
using namespace std;

virtual class Class {
public:
void fun() { cout << "Class::fun" << endl; }

};
Std C++ doesnot allow "vitual" classes, it allows virtual functions
virtual inheritance.
>
int main() {
Class objClass;
cout << "Size of Class = " << sizeof(objClass) << endl;
cout << "Address of Class = " << &objClass << endl;
return 0;

}

normal size of class with no variables is 1 if there is a virtual
function size will be 4, but here the scenario is different its a
virtual class instead of virtual function. The program compiled
successfully in Visual Studio 2005. I expected a vptr to be inserted
in it. instead nothing like that. size of Class was 1 instead of 4. Is
this some kind of Undefined Behaviour?

Jul 6 '07 #2
On Jul 6, 2:40 pm, Anarki <Deepchan...@gmail.comwrote:
#include <iostream>
using namespace std;

virtual class Class {
public:
void fun() { cout << "Class::fun" << endl; }

};
std C++ doesnot allow virtual classes.
It allows virtual functions and virtual inheritance.

Jul 6 '07 #3
Anarki wrote:
#include <iostream>
using namespace std;

virtual class Class {
That's a syntax error. You can't make classes virtual.
public:
void fun() { cout << "Class::fun" << endl; }
};

int main() {
Class objClass;
cout << "Size of Class = " << sizeof(objClass) << endl;
cout << "Address of Class = " << &objClass << endl;
return 0;
}

normal size of class with no variables is 1
Actually, it's some implementation-defined value that must not be zero.
if there is a virtual function size will be 4,
Again, this is some implementation-defined value.
but here the scenario is different its a virtual class instead of virtual
function. The program compiled successfully in Visual Studio 2005.
Then you probably didn't run it in standard C++ mode.

Jul 8 '07 #4
Rolf Magnus wrote:
Anarki wrote:
>>
normal size of class with no variables is 1

Actually, it's some implementation-defined value that must not be zero.
Implementation-specific, but not implementation-defined.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Jul 8 '07 #5
Pete Becker wrote:
Rolf Magnus wrote:
>Anarki wrote:
>>>
normal size of class with no variables is 1

Actually, it's some implementation-defined value that must not be zero.

Implementation-specific, but not implementation-defined.
I wonder, is it even guaranteed that there is a single value? or could
different classes all of which have no member variables have different
sizes?
Best

Kai-Uwe Bux
Jul 8 '07 #6
Kai-Uwe Bux wrote:
Pete Becker wrote:
>Rolf Magnus wrote:
>>Anarki wrote:
normal size of class with no variables is 1

Actually, it's some implementation-defined value that must not be zero.

Implementation-specific, but not implementation-defined.
IOW: It's unspecified.
I wonder, is it even guaranteed that there is a single value? or could
different classes all of which have no member variables have different
sizes?
They could all have different sizes. AFAICS, there is nothing in the C++
standard that forbids that.

Jul 9 '07 #7

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

Similar topics

7
1789
by: verbatime | last post by:
Please explain me how this works - or should work: Got my two classes - bcBasic (baseclass) and the derived cBasic. //--------------------------------------- class bcBasic { int number;...
17
1558
by: laniik | last post by:
Hi. I have a simple question, but I couldnt find the answer in any literature online. i have a base class A, that has some functions. now, my understanding of virtual functions means that in:...
11
4329
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...
4
385
by: aap | last post by:
Hi, I have the following code. #include <iostream> using namespace std; class Root { public: virtual void Root1() = 0; virtual void Root2() = 0;
14
12105
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
5
11335
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
23
4578
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; };
2
1235
by: ggroups_steve | last post by:
I was recently surprised about how a chunk of code compiled and executed, which lead me to wonder what would be "correct" from a C++ standards perspective. (I don't need help to arrive at sensible...
17
3510
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;"...
0
7218
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,...
0
7103
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
7370
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...
1
7021
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
5614
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,...
1
5035
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
3188
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...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
409
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...

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.