473,473 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Am I inherited?

Within class MyClass, I can think of two ways
to tell if MyClass is inherited in a particular
use:

1. pass an appropriate bool in the ctor args;

2. use a virtual method that returns, for
example, a siring containing the class name.

Is there a better way?

Thanks,
Mike.

Jul 21 '06 #1
12 1486
* Mike - EMAIL IGNORED:
Within class MyClass, I can think of two ways
to tell if MyClass is inherited in a particular
use:

1. pass an appropriate bool in the ctor args;

2. use a virtual method that returns, for
example, a siring containing the class name.

Is there a better way?
Design the class so it doesn't need that knowledge.

--
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 21 '06 #2
On Sat, 22 Jul 2006 00:42:59 +0200, Alf P. Steinbach wrote:
* Mike - EMAIL IGNORED:
>Within class MyClass, I can think of two ways
to tell if MyClass is inherited in a particular
use:

1. pass an appropriate bool in the ctor args;

2. use a virtual method that returns, for
example, a siring containing the class name.

Is there a better way?

Design the class so it doesn't need that knowledge.
That is often a good idea; but in this case, it is not.

Jul 21 '06 #3
* Mike - EMAIL IGNORED:
On Sat, 22 Jul 2006 00:42:59 +0200, Alf P. Steinbach wrote:
>* Mike - EMAIL IGNORED:
>>Within class MyClass, I can think of two ways
to tell if MyClass is inherited in a particular
use:

1. pass an appropriate bool in the ctor args;

2. use a virtual method that returns, for
example, a siring containing the class name.

Is there a better way?
Design the class so it doesn't need that knowledge.

That is often a good idea; but in this case, it is not.
That would be interesting, if true. However, I'm disinclined to believe
that claim without some concrete evidence. Please post a small example
that compiles, or where the compilation error illustrates the problem.

--
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 '06 #4
On Sat, 22 Jul 2006 02:28:41 +0200, Alf P. Steinbach wrote:
* Mike - EMAIL IGNORED:
>On Sat, 22 Jul 2006 00:42:59 +0200, Alf P. Steinbach wrote:
>>* Mike - EMAIL IGNORED:
Within class MyClass, I can think of two ways
to tell if MyClass is inherited in a particular
use:

1. pass an appropriate bool in the ctor args;

2. use a virtual method that returns, for
example, a siring containing the class name.

Is there a better way?
Design the class so it doesn't need that knowledge.

That is often a good idea; but in this case, it is not.

That would be interesting, if true. However, I'm disinclined to believe
that claim without some concrete evidence. Please post a small example
that compiles, or where the compilation error illustrates the problem.
Off point: the question is how, not whether to do it. There is no error.
Jul 22 '06 #5
* Mike - EMAIL IGNORED:
On Sat, 22 Jul 2006 02:28:41 +0200, Alf P. Steinbach wrote:
>* Mike - EMAIL IGNORED:
>>On Sat, 22 Jul 2006 00:42:59 +0200, Alf P. Steinbach wrote:

* Mike - EMAIL IGNORED:
Within class MyClass, I can think of two ways
to tell if MyClass is inherited in a particular
use:
>
1. pass an appropriate bool in the ctor args;
>
2. use a virtual method that returns, for
example, a siring containing the class name.
>
Is there a better way?
Design the class so it doesn't need that knowledge.
That is often a good idea; but in this case, it is not.
That would be interesting, if true. However, I'm disinclined to believe
that claim without some concrete evidence. Please post a small example
that compiles, or where the compilation error illustrates the problem.

Off point: the question is how, not whether to do it. There is no error.
I think that protest means you know your design is triple UnGood, and
just want a quick and dirty kludge.

Which will hopefully give an endless nightmare of problems so that you
learn to do things properly. :-)

For now, if a virtual class name member function is one solution, as you
have indicated, then a version of the same idea using built-in support
is the typeid operator.

--
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 '06 #6
Mike - EMAIL IGNORED wrote:
On Sat, 22 Jul 2006 02:28:41 +0200, Alf P. Steinbach wrote:
>* Mike - EMAIL IGNORED:
>>On Sat, 22 Jul 2006 00:42:59 +0200, Alf P. Steinbach wrote:

* Mike - EMAIL IGNORED:
Within class MyClass, I can think of two ways
to tell if MyClass is inherited in a particular
use:
>
1. pass an appropriate bool in the ctor args;
>
2. use a virtual method that returns, for
example, a siring containing the class name.
>
Is there a better way?
Design the class so it doesn't need that knowledge.

That is often a good idea; but in this case, it is not.

That would be interesting, if true. However, I'm disinclined to believe
that claim without some concrete evidence. Please post a small example
that compiles, or where the compilation error illustrates the problem.

Off point: the question is how, not whether to do it. There is no error.
Not off at all:

a) If we get to see your real problem, we might be able to suggest a better
design.

b) You should also consider the motivation we might have to provide you with
a solution, while there is this nagging feeling that you may be chasing a
red herring.
Best

Kai-Uwe Bux
Jul 22 '06 #7
[...]
For now, if a virtual class name member function is one solution, as you
have indicated, then a version of the same idea using built-in support
is the typeid operator.
I just tried typeid. It appears that it doesn't know where I
would like to know: in the constructor.

FYI, the situation is a hierarchy of complex mathematical
operations, each one in its own class. Most of the classes
can function alone or in concert with others. The sequence
of events is controlled by ctor of the most inherited class.
The data for all classes are mostly the same type, voluminous,
and kept protected in a base class. Virtual inheritance is
used. The most inherited class must recognize itself in the
ctor. There are, of course, other designs, but this provides
simplicity for the unsophisticated user and ease of expansion
as other mathematical procedures and interactions are developed.
Jul 22 '06 #8
Mike - EMAIL IGNORED wrote:
[...]
>>For now, if a virtual class name member function is one solution, as you
have indicated, then a version of the same idea using built-in support
is the typeid operator.


I just tried typeid. It appears that it doesn't know where I
would like to know: in the constructor.
Then you have a bit of a problem, don't you? You can't use virtual
methods in a constructor.

--
Ian Collins.
Jul 22 '06 #9
Mike - EMAIL IGNORED wrote:
[...]
>For now, if a virtual class name member function is one solution, as you
have indicated, then a version of the same idea using built-in support
is the typeid operator.

I just tried typeid. It appears that it doesn't know where I
would like to know: in the constructor.
What did you try?

I tried:

#include <typeinfo>

namespace {

template < typename B >
B unimplemented ( void );

}

template < typename B >
bool has_dynamic_type ( B const & b ) {
return ( typeid( unimplemented<B>() ) == typeid( b ) );
}
#include <iostream>

class Base {
protected:

Base ( void ) {
std::cout << has_dynamic_type< Base >( *this ) << '\n';
}

public:

bool is_base ( void ) const {
return ( has_dynamic_type< Base >( *this ) );
}

virtual
~Base ( void ) {}

static
Base* create ( void ) {
return new Base;
}

};

struct Derived : public Base {
protected:

Derived ( void ) {
std::cout << has_dynamic_type< Base >( *this ) << '\n';
}

public:

static
Derived* create ( void ) {
return new Derived;
}

};
int main ( void ) {
Base* b_ptr = Base::create();
Base* d_ptr = Derived::create();
std::cout << b_ptr->is_base() << '\n';
std::cout << d_ptr->is_base() << '\n';
}
a) I am not sure whether there is UB in the above.

b) A little bit of the code complexity is because I wanted something that
does not require default constructibilty.

c) On my machine, the output is:

1 // construction of *b_ptr
1 // construction of *d_ptr Base() part
0 // construction of *d_ptr Derived() part
1 // b_ptr->is_base();
0 // d_ptr->is_base();

FYI, the situation is a hierarchy of complex mathematical
operations, each one in its own class. Most of the classes
can function alone or in concert with others. The sequence
of events is controlled by ctor of the most inherited class.
The data for all classes are mostly the same type, voluminous,
and kept protected in a base class. Virtual inheritance is
used.
Could you provide a little example code that demonstrates the design?

The most inherited class must recognize itself in the ctor.
Note that when Base() is called, it truly constructs a Base object and
nothing else. The constructor has no way of telling how it is invoked.

Also, the other methods you mentioned in your first post have trouble in
this situation:
1. pass an appropriate bool in the ctor args;
In order to pass that bool, you would need to know already, won't you?
2. use a virtual method that returns, for
example, a siring containing the class name.
Virtual functions are not necessarily resolved the way you like in
constructors. I feel there might be some UB involved.
If you classes are just wrapping operations, then maybe you can move the
logic from the constructors to the destructors?
[snip]
Best

Kai-Uwe Bux
Jul 22 '06 #10

"Mike - EMAIL IGNORED" <m_*************@yahoo.comskrev i meddelandet
news:pa****************************@yahoo.com...
[...]
>For now, if a virtual class name member function is one solution,
as you
have indicated, then a version of the same idea using built-in
support
is the typeid operator.

I just tried typeid. It appears that it doesn't know where I
would like to know: in the constructor.
If you need to know something in a constructor, that something should
generally be a parameter to that constructor. You could add a
protected constructor, that the subclasses can call. Then at least you
know where that call comes from.

But, as Alf said, in a base class you generally don't want to know
your subclasses. They should take care of themselves.
>
FYI, the situation is a hierarchy of complex mathematical
operations, each one in its own class. Most of the classes
can function alone or in concert with others. The sequence
of events is controlled by ctor of the most inherited class.
The data for all classes are mostly the same type, voluminous,
and kept protected in a base class. Virtual inheritance is
used. The most inherited class must recognize itself in the
ctor.
And we all wonder why. :-)
There are, of course, other designs,
Right!
but this provides
simplicity for the unsophisticated user and ease of expansion
as other mathematical procedures and interactions are developed.
To me it looks more like the "ease of expansion" is not at all obvious
here, if the base class needs to know all expansions. But, what do I
know?

Bo Persson
Jul 22 '06 #11

Mike - EMAIL IGNORED wrote:
[...]
For now, if a virtual class name member function is one solution, as you
have indicated, then a version of the same idea using built-in support
is the typeid operator.

I just tried typeid. It appears that it doesn't know where I
would like to know: in the constructor.
Well, duh. In the base class ctor, the derived ctor hasn't yet run, so
it is
not inherited /yet/. typeid knows that, but it isn't psychic. It can't
tell if the
object will ever become derived (an exception may prevent the derived
ctor
from completing) so it can only tell you what's certain.

HTH,
Michiel Salters

Jul 24 '06 #12
On Mon, 24 Jul 2006 08:43:28 -0700, Michiel.Salters wrote:
>
Mike - EMAIL IGNORED wrote:
>[...]
For now, if a virtual class name member function is one solution, as you
have indicated, then a version of the same idea using built-in support
is the typeid operator.

I just tried typeid. It appears that it doesn't know where I
would like to know: in the constructor.

Well, duh. In the base class ctor, the derived ctor hasn't yet run, so
it is
not inherited /yet/. typeid knows that, but it isn't psychic. It can't
tell if the
object will ever become derived (an exception may prevent the derived
ctor
from completing) so it can only tell you what's certain.

HTH,
Michiel Salters
Yes, I found that out. For the same reason, option 2 in
my original post is not possible. From all I have seen
in these responses, it appears that option 1 in my
original post is the way to go, which is what I have
done.

Thanks for your help,
Mike.

Jul 24 '06 #13

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

Similar topics

2
by: Jeff Levinson [mcsd] | last post by:
I guess I would have to know what you mean by "not being able to edit the forms". Does this mean you get an error in the designer when you try to display an inherited form? Does this mean the...
2
by: Terrance | last post by:
Hello all I have a question I'm hoping someone can shed some light on. I have 2 user Inherited Controls on a form. When I click 1 inherited control I would like to change the text of the other...
4
by: Dan | last post by:
I have a need to make a set of classes that all share the same public methods, some implementation and some data. So, I made an abstract base (BaseClass) with an interface (IBaseClass) and a...
13
by: Lorne Smith | last post by:
Hi, First, sorry for the crosspost, but it seemed appropriate... :) I've come accross what I consider to be a bug, but I don't know if it's already known or not. (VS .Net 2003 Pro - VB.Net) ...
8
by: Spam Trap | last post by:
I am getting strange resizing problems when using an inherited form. Controls are moving themselves seemingly randomly, but reproducibly. "frmBase" is my base class (a windows form), and...
3
by: Jeff User | last post by:
Hello I am using C#, .net1.1 Vis Studio 2003 I am using homeBase.aspx.cs page as a base for several other aspx/aspx.cs web pages. The base page handles some operations that are common to all...
4
by: asad.naeem | last post by:
hi to all this is the problem about inheritence. I have designed a form with some essential controls which are required for every form which will inherited from it. for example i have Button1 on...
14
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
3
by: Dan Holmes | last post by:
Say i have: public class A { } public class B : A {
12
by: Janaka Perera | last post by:
Hi All, We have done a object oriented design for a system which will create a class multiply inherited by around 1000 small and medium sized classes. I would be greatful if you can help me...
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...
1
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.