473,395 Members | 1,527 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,395 software developers and data experts.

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 3648
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"<<endl;
}

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

};

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(component); // error: conversion innaccessible

std::vector< Component > vc;
vc.push_back(component); // 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 DerivedComponents
and store these in a container of Components.
DerivedComponents 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_Julian"
<pj@antispam.codigo.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********@munted.org.uk> wrote in message
news:20060310091857.61e02220.al********@munted.org .uk...
| On Fri, 10 Mar 2006 03:08:00 -0500 "Peter_Julian"
| <pj@antispam.codigo.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
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"...
2
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
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,...
10
by: Ioannis Vranos | last post by:
May someone explain why does this compile? class HiddenSealBaseClass { public: HiddenSealBaseClass() { } }; class Sealed: virtual HiddenSealBaseClass
5
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...
45
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...
3
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...
7
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...
2
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.