-2

This "base" class constructs for b , medium1 and medium2. When i use "top" class, it construct 2 times more. Totally constructing 5 times(writing to screen 5 times).

Question: What can i change in the "base" class to make it construct only once and for all.?

#include "stdafx.h"
#include<stdlib.h> 

class base
{
public:
    base(){x=5;printf(" %i ",x);}
    int x;
}b;

class medium1:public base{}m1;

class medium2:public base{}m2;

class top:public medium1,medium2{};


int main() {
    top ten;
    getchar();
    return 0;
}

what are pitfalls of virtual base class? Thanks for answering my trivial question.

huseyin tugrul buyukisik
  • 10,675
  • 3
  • 42
  • 85
  • 3
    1- `m1` , 2 - `m2` , 3 - `b`, 4,5 - `ten` due to inheritance from `medium1,medium2` – Mr.Anubis Aug 14 '12 at 21:28
  • 2
    Virtual base classes won't get the constructor count down to 1. There are four objects whose types are `base` or a type derived from `base`, so the least you can get is four constructions. – Pete Becker Aug 14 '12 at 21:29
  • 1
    @tuğrul büyükışık: Your question has no meaningful answer. It is not possible to have it constructed only once. If you meant something else from what you actually asked, please, clarify your question. – AnT Aug 14 '12 at 21:29
  • 2
    What is the actual problem that you're trying to solve? – Pete Becker Aug 14 '12 at 21:29
  • I want all of them to use only the original constructed base – huseyin tugrul buyukisik Aug 14 '12 at 21:33
  • If you want all those objects to use the same member `x`, just make `x` a `static` member. – aschepler Aug 14 '12 at 21:33
  • For example: base gets a timer data. All classes uses that same time data. Not the different times – huseyin tugrul buyukisik Aug 14 '12 at 21:33
  • 1
    @tuğrul büyükışık: Independent objects cannot share one common base subobject. The language has no such feature. So, as long as you implement the concept of "inheritance" through language feature known as "inheritance", you will have at least 4 separate bases (since you have 4 independent objects). – AnT Aug 14 '12 at 21:35
  • Then i have no other questions. Thank you all – huseyin tugrul buyukisik Aug 14 '12 at 21:36
  • 1
    @tuğrul büyükışık: One way to go is to make timer data `static` in the case class. You will still have many bases (4 or 5), but all these bases will share the same timer data. – AnT Aug 14 '12 at 21:37

5 Answers5

6

It is not possible to have base construct only once instead of all five original constructions. You have four complete objects in your program that contain base subobjects. This immediately means that regardless of what you do, you will have at least four base constructions.

Additionally, the suggestion to use virtual inheritance mentioned in other answers will actually require making changes to other classes, not to base class. Meanwhile your question insists that changes shall be made to base.

AnT
  • 302,239
  • 39
  • 506
  • 752
5

This will construct base only once when you create a top class.

class medium1:virtual public base{};
class medium2:virtual public base{};

The rest come from the fact that you create objects b, m1 and m2. You can't really prevent calling the destructor of the base class because, well, that's what you tell the compiler to do. If you don't want to create base objects, don't derive from it.

Luchian Grigore
  • 245,575
  • 61
  • 446
  • 609
2

You can make it a virtual base class, but that comes with a giant set of pitfalls.

Community
  • 1
  • 1
Jon
  • 413,451
  • 75
  • 717
  • 787
1

You can have a single base class object if you use virtual inheritance:

class medium1:virtual public base{}m1;

class medium2: virtual public base{}m2;
juanchopanza
  • 216,937
  • 30
  • 383
  • 461
1

This gets you down to two calls, but I think since you have double inheritance that's the best you'll get. If you eliminate the variable b in your base definition then you're there. http://codepad.org/pn016D7b

#include<stdlib.h> 

class base
{
public:
    base(){x=5;printf(" %i ",x);}
    int x;
}b;

class medium1:virtual public base 
{
public:
    medium1():base(){};
};

class medium2:virtual public base{};

class top:virtual public medium1,medium2{};


int main() {
    top ten;
    getchar();
    return 0;
}
MartyE
  • 646
  • 3
  • 7