473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

General C++ Questions

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
mu********************@yahoo.com
Jul 22 '05 #1
9 1821

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

Jul 22 '05 #2
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
Jul 22 '05 #3

"Tobias Erbsland" <ne***********@tracker.drzoom.ch> wrote in message
news:ca**********@atlas.ip-plus.net...

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;


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/
Jul 22 '05 #4
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #5
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
"JKop" <NU**@NULL.NULL> wrote in message
news:4U*****************@news.indigo.ie...
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

Jul 22 '05 #6
> 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
Jul 22 '05 #7
Murali writes:
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 :-(
Jul 22 '05 #8
al***@start.no (Alf P. Steinbach) wrote in message

1) Multiple inheritance - objects contain how many multiply inherited
ancestor?
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.

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.


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

Murali
Jul 22 '05 #9
"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.

--
Ayaz Ahmed Khan

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: gcook | last post by:
Hi, I've got an old perl program running on my webserver - so old that I haven't used a perl programmer in about two years :) (we've gone all php for a variety of reasons). Anyway, I'm...
1
by: Andrew MacLean | last post by:
Hello all, I am fairly new to .NET, but not to VB. I have a couple of questions. 1. Shared Code Base I have a solution with several different applications within it. Many of the...
2
by: Ross Micheals | last post by:
All I have some general .NET questions that I'm looking for some help with. Some of these questions (like the first) are ones that I've seen various conflicting information on, or questions that...
9
by: pankaj_wolfhunter | last post by:
Hi, I need some clearance on the following questions: 1) Does LOAD command updates indexes defined for a table? 2) Is REPLACE option in the LOAD command a logged operation? Help will be...
1
by: jason | last post by:
Hello everyone, I have some general questions about the DataTable object, and how it works. Moderately new to C#, I have plenty of texts describing the language, but not so much to reference...
6
by: Alex | last post by:
Hello I am intersting in developing and my background is VBA used in Excel and a brief intro to Java. I am interested in learning beyond VB and feel that C++ would be a very good language to...
6
by: Andy | last post by:
Someone posted this official proposal to create comp.databases.postgresql.general again. He wrote his own charter. As far as I know, he did not consult any of the postgresql groups first. There...
4
by: Viviana Vc | last post by:
Hi all, I've read the WindowsVistaUACDevReqs.doc documentation and I have done different small tests on Vista to understand the bahaviour and now I have a few questions. 1) If I create a...
2
by: SpotNet | last post by:
Scott M. Have your experiences shown you that programmers may give the best computer advice, analysis, and solutions even at the worst of times, for a PC user? ;~D Haven't crossed many Network...
3
by: =?Utf-8?B?Ymxi?= | last post by:
I am posting to the general discussion group - but I cannot find my postings... or replies...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.