6 Replies - 237 Views - Last Post: 21 April 2013 - 07:28 AM Rate Topic: -----

#1 squarepenguin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 16-May 12

Derive multiple classes from single base instance

Posted 20 April 2013 - 09:49 AM

Hey everyone,
I'm struggeling how exactly inheritance works in C++. Consider the following:
class Base{
public:
	Base(){/*create/initialize resource*/}
	virtual ~Base(){}
	VeryExpensiveResource resource;
	virtual void foo(){/*code*/}
}

class A:Base{
public:
	virtual void foo(){/*code using resource*/}
}

class B:Base{
public:
	virtual void foo(){/*code using resource*/}
}

int main(){
	A a;
	B b;
	a.foo();
	b.foo();
}


I want only a single Base instance of which a and b are derived so I have only one VeryExpensiveResource. Is this done like so? is it done differently? or is this just The Wrong Thing To Do?

Is This A Good Question/Topic? 0
  • +

Replies To: Derive multiple classes from single base instance

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Derive multiple classes from single base instance

Posted 20 April 2013 - 10:03 AM

Have you tried printing stuff from your foo()s to see what happens? You have to apply some effort here.
Was This Post Helpful? 0
  • +
  • -

#3 squarepenguin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 16-May 12

Re: Derive multiple classes from single base instance

Posted 20 April 2013 - 10:54 AM

I appologise, I know that this code creates two instances of Base. I will be clearer in future.
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,401
  • Joined: 19-February 09

Re: Derive multiple classes from single base instance

Posted 20 April 2013 - 04:42 PM

There is the idea of a Singleton as a possibility.
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1051
  • View blog
  • Posts: 4,458
  • Joined: 09-June 09

Re: Derive multiple classes from single base instance

Posted 20 April 2013 - 04:47 PM

You can declare your VeryExpensiveResource as static and it will be shared among all instances of your class.
Was This Post Helpful? 0
  • +
  • -

#6 squarepenguin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 16-May 12

Re: Derive multiple classes from single base instance

Posted 21 April 2013 - 12:53 AM

Firsty, thanks for your replies.

@jjl
That would work great, but it would still create multiple intances of Base.

@#define (awesome name)
That is the best solution so far it seems, but I hear a lot of controvercy about Singletrons which makes me hesitant to use it.


Also: Im thinking about passing functors to the Base class and avoid inheritance altogether, but this also sounds like a hack.
Was This Post Helpful? 0
  • +
  • -

#7 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Derive multiple classes from single base instance

Posted 21 April 2013 - 07:28 AM

You are never going to find the perfect solution to whatever it is you are looking for. It's easy to get tied up in the best way to code something when you should be looking to:

1. Maintainability.
2. Efficiency.
3. Extensibility.

In extremely complicated software like games, singletons are used heavily in a responsible manner. Maybe you should be looking at their benefits over all the complaints against them.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1