General C++ Questions

M

Murali

Hi all,

I happened to come across these two questions in a site, and I couldn't
think or get hold of an answer to these two questions.

1) Multiple inheritance - objects contain how many multiply inherited
ancestor?

2) How would you find out the no of instance of a class?

Any help would be greatly appreciated.

Thanks
Muralidhar
(e-mail address removed)
 
T

Tobias Erbsland

Hello

I don't understand your first question.
2) How would you find out the no of instance of a class?

Example:

class A
{
public:
A();
~A();

unsigned int getInstances();

private:
static unsigned int instances;
}

A::A()
{
++instances;
}

A::~A()
{
--instances;
}

A::getInstances()
{
return instances;
}

unsigned int A::instances = 0;


best regards
Tobias
 
J

JKop

Murali posted:
2) How would you find out the no of instance of a class?


You can't.


You'd have to improvise as so:


class Kata
{
protected:
static unsigned short int amount_objects;

public:

Kata(void)
{
++amount_objects;
}

~Kata(void)
{
--amount_objects;
}

};

unsigned short int Kata::amount_objects = 0;


Or perhaps there's some sort of template out there that'll allow you to do
this with any class.


-JKop
 
S

Sharad Kala

Tobias Erbsland said:
Hello

I don't understand your first question.


Example:

class A
{
public:
A();
~A();

unsigned int getInstances();

private:
static unsigned int instances;
}

A::A()
{
++instances;
}

A::~A()
{
--instances;
}

A::getInstances()
{
return instances;
}

unsigned int A::instances = 0;

One obvious mistake in this solution is that it does not take into account
copies of objects being made.
Scott Meyers has given a good solution -
http://www.cuj.com/documents/s=8066/cuj9804meyers/
 
A

Alf P. Steinbach

* Murali:
I happened to come across these two questions in a site, and I couldn't
think or get hold of an answer to these two questions.

1) Multiple inheritance - objects contain how many multiply inherited
ancestor?

I'm sure you have left out the context of that question.

2) How would you find out the no of instance of a class?

Count them in a static member updated by every constructor and
destructor in the topmost base class. Pitfall: thread safety. Note
that as of 2004 the C++ standard says nothing about threads.
 
F

Frane Roje

You forgot the copy constructor.

What if I wrote

Kata x;
Kata y = x;

How many object there are and how much does the
amount_object show?

--
Frane Roje

Have a nice day

Remove (d*el*ete) from email to reply
 
M

Marcin Kalicinski

One obvious mistake in this solution is that it does not take into account
copies of objects being made.
Scott Meyers has given a good solution -
http://www.cuj.com/documents/s=8066/cuj9804meyers/

In the article Scott is exploring various possibilities of implementing an
instance counter. Finally he finds out that the best one is to use curiously
recurring template pattern and private inheritance. The only unsolved
problem is that howMany() member function is not accessible, and must be
made so with an explicit using declaration inside the class body.

One thing I do not understand is why does he not use a static member
function howMany() from Counter class template directly, but insists on
having a public howMany() member in all derived classes?

It's use might look like that:

Counter<Widget>::getCount();

instead of

Widget::howMany();

Best regards,
Marcin
 
O

osmium

Murali said:
I happened to come across these two questions in a site, and I couldn't
think or get hold of an answer to these two questions.

1) Multiple inheritance - objects contain how many multiply inherited
ancestor?

For starters, revise that into a sentence that can be read in an unambiguous
fashion.
2) How would you find out the no of instance of a class?

I have read several of the answers despite the placement of this post in the
thread. Several years ago this was sometimes used as an illustration of a
candidate use for static variables in objects. IOW, why does static exist
in objects? Because ..... . I would expect that was the desired answer
even though there are potential problems, as noted earlier.

There are not only wrong answers, there are wrong questions :-(
 
M

Murali

(e-mail address removed) (Alf P. Steinbach) wrote in message
I'm sure you have left out the context of that question.

I just copy pasted the question from the site. My assumption was that
they were asking what depth can the inheritance go. i.e. How many
derived classes can be from a base class in a single branch of the
hierarchy.
Count them in a static member updated by every constructor and
destructor in the topmost base class. Pitfall: thread safety. Note
that as of 2004 the C++ standard says nothing about threads.

I thot so,and there was the copy constructor problem too.

Murali
 
A

Ayaz Ahmed Khan

"Murali" typed:
Hi all,

I happened to come across these two questions in a site, and I
couldn't think or get hold of an answer to these two questions.

1) Multiple inheritance - objects contain how many multiply
inherited ancestor?


More than one, I suppose.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top