473,547 Members | 2,553 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inheritance and base constructor

I have a class that may or may not be inherited.
Its constructor calls a function that should be
called only if the class is not inherited. Is
there a way to tell, other than simply passing
a bool argument?
Thanks for your suggestions.
Mike.
Mar 10 '06 #1
8 3660
i think u only need add this func in ur base class .
as this simple
class A
{
public:
A()
{
//you only need add the function here.when constructor
a derived class ,it will //transfer here
cout<<"class A constructor"<<e ndl;
}

};
class B:public A
{
public:
B()
{
cout<<"class B constructor"<<e ndl;
}

};

int main()
{
B temp;

}

Mar 10 '06 #2
* Mike - EMAIL IGNORED:
I have a class that may or may not be inherited.
Its constructor calls a function that should be
called only if the class is not inherited. Is
there a way to tell, other than simply passing
a bool argument?


You could pass the function as argument, but there's no built-in way in
a class to tell whether it's being inherited. You can limit inheritance
to a set of designated classes (which can be empty), and I think that's
in the FAQ, but that's all. Anyway, this looks like a design problem,
that there's something wrong with the design; details would be welcome.

--
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?
Mar 10 '06 #3
In article <pa************ *************** *@yahoo.com>,
Mike - EMAIL IGNORED <m_************ *@yahoo.com> wrote:
I have a class that may or may not be inherited.
Its constructor calls a function that should be
called only if the class is not inherited. Is
there a way to tell, other than simply passing
a bool argument?


Fix your class so it doesn't need to know if a derived class is calling
its constructor. Otherwise, you could do something like this:

class Base
{
public:
Base();
protected:
void call_in_c_tor() ;
};

class Derived: public Base
{
public:
Derived(): Base() { call_in_c_tor() ; }
};

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Mar 10 '06 #4

"Mike - EMAIL IGNORED" <m_************ *@yahoo.com> wrote in message
news:pa******** *************** *****@yahoo.com ...
| I have a class that may or may not be inherited.
| Its constructor calls a function that should be
| called only if the class is not inherited. Is
| there a way to tell, other than simply passing
| a bool argument?
| Thanks for your suggestions.
| Mike.

You'll hit a wall since copy ctors will probably cause an issue.

You are describing a relationship that on the one hand depicts a natural
inheritence tree and on the other hand a component tree.

Say you have a class Base and plan to derive from it with a class Derived.
You could consider starting another private inheritence tree and create a
class Component and therefore isolate the 2 trees, after all:

Component is_not_a Derived.

Your code will benefit from such a distinction.
Its also preferable to place that function in the appropriate tree.

class Component is in_terms_of Base, which is in reality a form of
composition...

#include <iostream>
#include <ostream>
#include <vector> // or some other STL container

class Base
{
protected:
Base() { std::cout << "Base()\n"; }
virtual ~Base() = 0 { std::cout << "virtual ~Base()\n"; }
};

class Derived : public Base
{
public:
Derived() : Base() { std::cout << "Derived()\ n"; }
~Derived() { std::cout << "virtual ~Derived()\n"; }
};

class Component : private Base /* in_terms_of */
{
void foo() { std::cout << "foo()\n"; }
public:
Component() : Base()
{
std::cout << "Component()\n" ;
foo();
}
~Component() { std::cout << "virtual ~Component()\n" ; }
};

int main()
{
{
std::cout << "--- Derived ---\n";
Derived d;
}
std::cout << "--- Component ---\n";
Component component;

// std::vector< Base > vb;
// vb.push_back(co mponent); // error: conversion innaccessible

std::vector< Component > vc;
vc.push_back(co mponent); // ok, but beware, its a copy !!

return 0;
}

/*
--- Derived ---
Base()
Derived()
virtual ~Derived()
virtual ~Base()
--- Component ---
Base()
Component()
foo()
virtual ~Component()
virtual ~Base()
<------------------- copy c~tor does *not* call foo(),
at least no yet it doesn't
virtual ~Component()
virtual ~Base()
*/

Note that you are free to derive from Component but you can't store
Component objects in a container of Base objects. Which makes sense.
Components are components.

You could derive to expand the Component hierarchy with DerivedComponen ts
and store these in a container of Components.
DerivedComponen ts calls foo() as expected except when copy construction is
involved. This may or may not be appropriate but easily corrected.
You'll define Component's copy ctor anyways in the case you plan to store
these.

Component : private Base
{
public:
...
Component(const Component& copy)
{
...
foo(); // maybe, maybe not
}
};

Can you now see why placing foo() in Base is potentially a bad idea?
What you do in Base's copy ctor affects both trees.

And the same arguement applies to the assignment operator.
Mar 10 '06 #5
On Fri, 10 Mar 2006 03:08:00 -0500 "Peter_Juli an"
<pj@antispam.co digo.ca> waved a wand and this message magically
appeared:
virtual ~Base() = 0 { std::cout << "virtual ~Base()\n"; }


That is incorrect! Remove the '= 0', and all will be well.

--
http://www.munted.org.uk

"Honestly, what can I possibly say to get you into my bed?" - Anon.
Mar 10 '06 #6
On Fri, 10 Mar 2006 03:47:25 +0100, Alf P. Steinbach wrote:
* Mike - EMAIL IGNORED:
I have a class that may or may not be inherited.
Its constructor calls a function that should be
called only if the class is not inherited. Is
there a way to tell, other than simply passing
a bool argument?
You could pass the function as argument, but there's no built-in way in
a class to tell whether it's being inherited. You can limit inheritance
to a set of designated classes (which can be empty), and I think that's
in the FAQ, but that's all. Anyway, this looks like a design problem,
that there's something wrong with the design; details would be welcome.


I have changed the design for now. The problem is that in many,
if not all classes that create posix threads, it is necessary to
call pthread_create( ) after all objects in the inheritance
sequence have been fully created. This would be in the constructor
of the most derived class. If a class may or may not be
inherited, It needs to determine whether pthread_create( ) should be
called at that level. A less attractive alternative is to require
an initialize() after construction. I would like a way to avoid
this.

Mike
__
A: Then why does most software encourage top-posting? 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?

Mar 11 '06 #7
* Mike - EMAIL IGNORED:
On Fri, 10 Mar 2006 03:47:25 +0100, Alf P. Steinbach wrote:
* Mike - EMAIL IGNORED:
I have a class that may or may not be inherited.
Its constructor calls a function that should be
called only if the class is not inherited. Is
there a way to tell, other than simply passing
a bool argument?

You could pass the function as argument, but there's no built-in way in
a class to tell whether it's being inherited. You can limit inheritance
to a set of designated classes (which can be empty), and I think that's
in the FAQ, but that's all. Anyway, this looks like a design problem,
that there's something wrong with the design; details would be welcome.


I have changed the design for now. The problem is that in many,
if not all classes that create posix threads, it is necessary to
call pthread_create( ) after all objects in the inheritance
sequence have been fully created. This would be in the constructor
of the most derived class. If a class may or may not be
inherited, It needs to determine whether pthread_create( ) should be
called at that level. A less attractive alternative is to require
an initialize() after construction. I would like a way to avoid
this.


There are several design choices, then. Off the cuff:

* If possible, let all classes inherit down a chain from some Runnable
class, which can only be used by passing it to the run() function of
a Thread object. Or similar design directly in Thread.

* Full separation of interfaces and concrete classes, where concrete
classes can't be derived from (I don't like this, but).

* Use virtual inheritance to impose a final CRTP derivation to obtain
a concrete class, which you ensure can't be derived from.

You're not the first to have this problem, and last I discussed this
with an acknowledged C++ expert, one who I regard as real expert (in
spite of our disagreement about nearly everything!), he rejected all
three possibilities above as not-quite. However, he was then arguing
for introducing a kind of post-construction-pre-destruction facility to
the language, and I think his arguments were designed to argue the case
for that facility, not to find just a practical design space solution.
Hopefully one of the possibilities above, or some other, will help you.
PS: Please don't quote signatures. A usenet posting signature is
delimited from the preceding text by a line consisting of two hyphens
and a space. Your news client should remove it automatically.
Mar 11 '06 #8

"Alex Buell" <al********@mun ted.org.uk> wrote in message
news:2006031009 1857.61e02220.a l********@munte d.org.uk...
| On Fri, 10 Mar 2006 03:08:00 -0500 "Peter_Juli an"
| <pj@antispam.co digo.ca> waved a wand and this message magically
| appeared:
|
| > virtual ~Base() = 0 { std::cout << "virtual ~Base()\n"; }
|
| That is incorrect! Remove the '= 0', and all will be well.
|

Technically, the standard requires the pure-virtual Base class to look like
so...
___
class Base
{
public:
Base() { }
virtual ~Base() = 0;
};

Base::~Base() { } // seperate, empty implementation
___
The std::cout output was only for illustrative purposes.

I'll agree that protected access + pure-virtual d~tor is overkill but there
is no doubt that a pure virtual Base class is a requirement here until
proven otherwise. And since its illegal to provide a pure-virtual d~tor
without an implementation. ..

Surely, a Triangle, Circle or Rectangle are potential 2D object-types but a
Shape is not.

Mar 11 '06 #9

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

Similar topics

10
7215
by: jeffc | last post by:
When compiling the following program, I get an error on the line specified: The base class "B" cannot be initialized because it does not have a default constructor. If I remove the "virtual" inheritance of B (so that it uses plain inheritance), it compiles fine. It also compiles if I invoke the constructor for B explicitly, as shown in the...
2
2823
by: lok | last post by:
consider follow simple code: class Base { protected: int m_nAddr; public: Base(int addr_): m_nAddr(addr_) {} virtual ~Base() {} virtual int GetData() const = 0; };
3
1712
by: Tom | last post by:
Hello, I've searched groups and the std unsuccessfully for an explanation of the following - I'd appreciate any comments you may have. Given the following diamond multiple inheritance pattern, with the base class having 2 constructors, only the default constructor is called. Is there a way to require the other constructor to be used? ...
10
2468
by: Ioannis Vranos | last post by:
May someone explain why does this compile? class HiddenSealBaseClass { public: HiddenSealBaseClass() { } }; class Sealed: virtual HiddenSealBaseClass
5
1678
by: Tony Johansson | last post by:
Hello! If you have the following inheritance A is the base class for the derived class B and B is the base class for the derived class C. In this case will the constructor of class C be executed first then the constructor of class B and finally the constructor of class A. Now to my question. When you have inheritance do you use the...
45
6313
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { //...
3
2347
by: Klaas Vantournhout | last post by:
Hi, Recently I obtained a problem with virtual inheritance when implementing it in multiple files. To present the problem I have included at the bottom of this post the code of the 4 files. I show results with the gnu compiler but i have the same results with the intel compiler. file1.cpp compiles without a problem, the problem occurs...
7
3722
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance goes like this: Shape | +-- Ellipse | +-- Circle
2
2576
by: Mahesh | last post by:
Hi, I encounted some problems using GDB with classes that use virtual inheritance. To illustrate this issue, I have created a simple test case. Here it goes. #include <iostream>
3
2540
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is used, then "the responsibility for initializing a virtual base is borne by the most derived class in the hierarchy". What does it mean? Initializing...
0
7510
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7703
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7463
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5081
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3493
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3473
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1923
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.