0
Below is a pseudo declaration for a multilevel inheritance.

Base class ( protected int data)

derived1 : virtual public base ( protected int data1 )

derived2 : virtual public base ( protected int data2)

derived3 : derived1,derived2 ( private int data3 )

Main(){ base b;    derived1 d1;   derived2 d2;   derived3 d3; }

sizeof(b) // 4 which is correct as only int (4bytes)
sizeof(d1) // 12 why not 8 -> 4(base) + 4(derived)
sizeof(d2) // ??? whatever applies above should apply here
sizeof(d3) // 24 why not 12 -> 4(base) + 4(derived1/derived2) + 4(d3).

Does size also include virtual tables also. Again here there cannot be virtual table as no virtual function defined. Please help in clarifying my doubt.

PS: What I have understood till now:

Unless the function is declared virtual in base class,

base *bptr;
 derived d;
 bptr = &d;
 bptr->fun();  // will call the base class function.

But if the fun() is declared virtual then the above code will call derived class fun().
Haswell
  • 1,573
  • 1
  • 18
  • 45
  • 5
    A return type of `void` won't work. It should return the object. I don't see how you got that to work, though: http://coliru.stacked-crooked.com/a/be4074970d5da3cb. If you're looking for sample code, there's already [a question](http://stackoverflow.com/questions/4421706/operator-overloading) about operator overloading with sample code. – chris Feb 03 '14 at 16:10
  • Did you even try to compile it? How have you tired it? http://ideone.com/BUh2nh says `error: return-statement with a value, in function returning 'void' [-fpermissive]`. – luk32 Feb 03 '14 at 16:16
  • Sorry for double comment, but this "I have tried it" part seems like a big lie. -1 =(. – luk32 Feb 03 '14 at 16:28
  • Extremely sorry...yes copied and pasted the wrong overload function....please see the edited part now...removed the return keyword....the actual implementation has a count variable in the class. sorry for wasting your time...should have given the correct code... but now that is correct...please explain the use of both those methods of operator overloading – Haswell Feb 03 '14 at 17:00
  • @eyeanand The `void` return type still won't work for the overloading the postfix operator! – πάντα ῥεῖ Feb 03 '14 at 17:08
  • @πάνταῥεῖ Well, it does compile for me. with `void operator ++(int){count++;};` on [ideone](http://ideone.com/BUh2nh)(I hope the linking like that works) ... not sure what I do ... right. It even works as expected. Of course it will break when you try to embed `a++` in a more complex expression, but it does compile. – luk32 Feb 03 '14 at 17:21
  • @luk32 May be it compiles, but becomes semantically useless ... (see @ barakmanos answer) – πάντα ῥεῖ Feb 03 '14 at 17:28
  • @πάνταῥεῖ Of course, I guess it comes down to the definition of "what works". It is a great step forward from the uncompilable code =) I am actually surprised you can declare an overload with such signature, I wonder why it is not required to return the object of the same type. – luk32 Feb 03 '14 at 17:36

1 Answers1

1

First of all, in your implementation above, you need to return the type of count instead of void.

For example, suppose you've declared int count.

Then you need to return int in the 'postfix' version, and int& or const int& in the 'prefix' version.

Try b = a++ and b = ++a, and you will see (of course, you'll need each function to return a value).

The difference between these two versions is only in the return value. The 'prefix++' returns the value of count before the operation, and the 'postfix++' returns the value of count after the operation.

In addition, due to its nature, the 'postfix++' can only return a copy of the variable being incremented (e.g., int), whereas the 'prefix++' can also return a reference of that variable (e.g., int&).

Since you are not returning anything in your implementation, you cannot make any use of the difference between these two versions.

barak manos
  • 29,648
  • 10
  • 62
  • 114
  • please see the edited part now...removed the return keyword....the actual implementation has a count variable in the class. sorry for wasting your time...should have given the correct code... but now that is correct...please explain the use of both those methods of operator overloading – Haswell Feb 03 '14 at 17:02
  • Well, the whole idea behind the `++` operators is that they return a value. If they don't return anything, then obviously calling 'prefix++' and 'postfix++' will have the same impact. And as I mentioned in my answer above, I still don't see how your code is compiled without an error when you invoke `a++` without implementing the 'postfix++'. – barak manos Feb 03 '14 at 17:09
  • i edited the second part as well. :( – Haswell Feb 03 '14 at 17:16
  • So there is nothing asked in your question! You have implemented 'prefix++' and 'postfix++', and you are invoking both of them with `++a` and `a++`. What exactly happens in a different way than what you expect??? – barak manos Feb 03 '14 at 17:19
  • what is the use of void operator ++(int) { count++; } when only one of the overload can do both...why such type is given in literature – Haswell Feb 03 '14 at 17:23
  • The difference is only in the **return value**, which in the 'prefix' version is the value **before** the operation, and in the 'postfix' version is the value **after** the operation. Since you are not returning any value in your implementation, you cannot make any use of the difference between these two versions. – barak manos Feb 03 '14 at 17:27
  • Please see the last section of the revised answer above. – barak manos Feb 03 '14 at 17:35
  • If (for whatever reason) a programmer decides to return void in any of the increment operator, it may be reasonable –  Feb 03 '14 at 17:38
  • You can return `void`, but in that case, the only reason for you to implement both versions would be in order to allow the use of `a++` as well as `++a` (which will not compile otherwise). – barak manos Feb 03 '14 at 17:48