I did know that the size of a class is increased by 4 bytes (32bit compiler) if the class has a virtual function. I wrote one program and it is giving strange output. I am using linux g++ compiler.
The program and output is as below
Code:
#include<iostream>
using namespace std;
class Base {
public:
void fun() {
[Code] ....
output=4
Code:
#include<iostream>
using namespace std;
class Base {
public:
virtual void fun() {
Imagine if there is an abstract class with a method (say output or print) which would be inherited by a few other classes. Later objects are created using the inherited classes, and the user wishes to call the above method twice, for eg (i) output/print to screen and (ii) output/print to a file. What is the best way to achieve that.
The compiler creates virtual table for the base class and also for the derived class whether we override it or not.
That means each class has separate virtual table. when we get the size of the each class with out any data members... the size of base is -- 4 bytes(64 bit) and the size of derived is -- 1
The size of base class 4 is correct since it creates the virtual pointer internally and its size is member data + virtual pointer, but it in this case I have included any data members so it has given 4 byts.
But why in case of derived is 1 byte, since it the derived class has overridden the virtual function from base, this will also contains the virtual pointer which will be pointing to derived class Vtable, it the size of the class suppose to be 4 instead of 1 byte.
What is the best / most efficient way to load polymorphic data from a file? I thought you could have an enumeration and for each item to load from a file you could have an integer at the start specifying the type of data, but I think there must be a better way I'm just not sure what.
Example of what I mean:
//The syntax isn't really that important for explanation class base; class a: base, b: base; enum polymorphicType { A, B };
and in the loading code you would have (this is the bit I think could be improved):
polymorphicType t; File >> t; if(t == A) { newObject = new A; } else if(t == B) { newObject = new B; }
I think there is probably a more efficient/better way of doing this I am just unaware of it.
I'm having an issue during the linking stage of the .exe during compile time, and it's because of a call to a function with a polymorphic parameter.
//here's how the classes are setup class grandpa {}; class mom public grandpa{}; class dad public grandpa {};
[Code]....
dad has sons in the vector and mom has daughter in the vector. What I want to do is have a function that can accept either of these vectors as one parameter like so: void func(const grandpa* aObject);
Finally, I have an error when I pass an object to the function like so: func( d[0] );
Linked lists seem to be the most erroneous and most frequent thing I use and post about nowadays. I've been wanting to handle data structures in my own library of functions, but I'm not sure how to imitate polymorphism without too many ambiguities. I could just pass some character or string value to represent a type, but I wanted to see if there was actually something more elegant I could use before I dive in.
I'm building a simple system management console application. I've abstracted the console "Menu" and derived from it a "WelcomeMenu" class with public inheritance.
The problem is that when instantiating a Menu* object and assigning it a new WelcomeMenu...I'm still not able to use WelcomeMenu's "ShowWelcomeMessage() with the Menu* object. Instead, I get "error: Class 'Menu' has no member function call 'ShowWelcomeMessage().' Which is true, but I thought a pointer-to-Menu object should be able to use the public methods of derived classes without casting in this case. Code follows.
// Menu and WelcomeMenu Classes #ifndef MENU_H #define MENU_H
This works if the function pointer being passed to the event manager is not a member function.
I have two other classes Scene and Object that could potentially use this EventManager to create callback events. Scene and Object are both pure virtual objects. How can I pass a pointer to a member function of the child classes of both Scene and Object? I am fine with just having two separate watchEvent functions for Scene and Object but how do I pass the type of the Object or the type of the Scene? The child classes are unknown as they are being created by someone using this game engine.
For example, if I want to make a player object it would be something like
class PlayerObject : public Object{...};
Now that PlayerObject type has to find its way to PlayerObject::functionToCall(). I think I need templates to do this but, since I never used them before
This is how I intend to use this
class OtherScene : public Scene{ void p_pressed(void){ //pause }
Why is the size of an empty class 1? Why is the class still one when I add a char member to the class?//using turbo c++ 3.0, yes I know I'm using a very old c++ compiler and software
Code: class A { public: virtual void foo(){} virtual void foo2(){} virtual void foo3(){} }; int main() { A a; int ret = sizeof(A); return 0; }
Basically object a contains a virtual table pointer which is of size 4 bytes. Since class A should have a virtual table which contains three pointers pointing to foo, foo2,foo3 separately. So the virtual table should be of size 12 bytes. I wonder where is virtual table located in memory?
I wanted to add that the template argument is needed because its a "special case" but if that doesn't work what would be the next best way to solve this problem. I want to be able to declare the const size of the array outside the class far removed from it actually. I'm actually going off this page
comparing with screen size the height is bigger but lenght is smaller. I don't understand.
I can understand that different printers process the fonts in different way and then to have different lenghts. That's not the problem. The problem is I need to simulate in screen the same behaviour i will have on printer because these texts are being aligned in the document, and I don't want to see that the text si aligned different in text than in paper.
What can I do to render the text on screen with the same size I will have on the printer? Print preview is doing it. Should I change the font parameters? is something related with pixels per inch?
I was wondering why, in C, the sizeof of a struct is larger than the the sum of all the sizeofs of it's members. It only seems to be by a few bytes, but as a bit of a perfectionist I fine this a bit annoying.
I've been looking into the file structure of BMP images and everything I'm reading says that the 4 bytes following the signature are designated as the filesize of the bmp file... It's always zero for me regardless of the BMP file. The signature is always correct though.
I'm trying to put all of the words in a text document into an array but this text document is 2,138 kb, and when my program is crashing when I try to put it into an string array. Could the file be too big to put into the array?
The problem is with that a. What is it, a pointer? What's the difference between the a in the main function and the a in the function?
#include <iostream> #include "Header.h" using namespace std; short int capacity(int* a) { int capacity;
[Code] ....
The function it returns i think the size of the pointer instead of returning the size of my array. I don't think i fully understood pointer arithmetic.
int numbers[] = {8, 2, 0, 4, 100, 5}; for(int i = 0; i < sizeof(numbers); i++){ cout << numbers[i] << endl; }
However the results in the console is: 8 2 0 4 ,What am I doing wrong? Am I using the wrong built in function or something? I googled this and one of the links that came up stated to just do something like
arrayName.size()
but that didnt work for me either...
[URL]
Also, I know that I just enter the size of the list manually, in this case make i < 6 but I still want to know if there is a built in function or something.