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

Home Posts Topics Members FAQ

subclass initialization

Hello,

I'm guessing I can't do this, but can I do work in a subclass before its
parent class initialization?

I've got 2 classes, class A & subclass B:

class A
{
public:
A(): x(0), y(0) {}
...
}

class B: public A
{
//bunch of definitions
}

So, normally for subclass B I'd initialize as

B::B() : A()
{
do_some_work_here();
}

Is it possible to execute 'do_some_work_here()' BEFORE the parent class
initialization?

Thank you,

Jay


Jul 23 '05 #1
7 2261
Jay Wolfe wrote:
class B: public A
{
//bunch of definitions
}

So, normally for subclass B I'd initialize as

B::B() : A()
{
do_some_work_here();
}

Is it possible to execute 'do_some_work_here()' BEFORE the parent class
initialization?


No. B's constructor doesn't start until the A portion is fully constructed.
Jul 23 '05 #2
Jay Wolfe wrote:
Hello,

I'm guessing I can't do this, but can I do work in a subclass before its
parent class initialization?


You're right. You can't do this.

Jul 23 '05 #3
Jay Wolfe wrote:

B::B() : A()
{
do_some_work_here();
}

Is it possible to execute 'do_some_work_here()' BEFORE the parent class
initialization?


Yes, with a little change. But it's a hack.

class A
{
public:
A(int): x(0), y(0) {}
...
}

class B: public A
{
int do_some_work_here();
}

B::B() : A(do_some_work_here())
{
}

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #4
Jay Wolfe wrote:
Hello,

I'm guessing I can't do this, but can I do work in a subclass before its
parent class initialization?

I've got 2 classes, class A & subclass B:

class A
{
public:
A(): x(0), y(0) {}
...
}

class B: public A
{
//bunch of definitions
}

So, normally for subclass B I'd initialize as

B::B() : A()
{
do_some_work_here();
}

Is it possible to execute 'do_some_work_here()' BEFORE the parent class
initialization?

Thank you,

Jay


This is a bit hackish, but you could make a separate class that did the
work in its constructor, and derive from that as well. You would be
exploiting the fact that base classes are initialized in order of
declaration. For example:

class do_work
{
public:
do_work() { do_some_work_here() ; }
} ;
class B : public do_work, public A
{
....
} ;
The do_work class will be initialized before A. If you want more
information, see the boost utility template here:
http://boost.org/libs/utility/base_from_member.html

-Alan
Jul 23 '05 #5
Thanks to all! A couple of hacks to try is better than nothing!

Jay

Jul 23 '05 #6
Pete Becker wrote:
Jay Wolfe wrote:

B::B() : A()
{
do_some_work_here();
}

Is it possible to execute 'do_some_work_here()' BEFORE the parent class
initialization?

Yes, with a little change. But it's a hack.

class A
{
public:
A(int): x(0), y(0) {}
...
}

class B: public A
{
int do_some_work_here();


do_some_work_here should be static, because the member variables of B are
not yet initialized and thus must not be accessed anyway.
}

B::B() : A(do_some_work_here())
{
}


Jul 23 '05 #7
Rolf Magnus wrote:
class B: public A
{
int do_some_work_here();

do_some_work_here should be static, because the member variables of B are
not yet initialized and thus must not be accessed anyway.


I wouldn't know. I haven't seen its code.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #8

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

Similar topics

1
2404
by: Gerry Sutton | last post by:
Hi All! I have noticed a strange behavior when using a constant identifier to initialize an instance list variable in a base class and then trying to modifying the list in subclasses by using...
2
2029
by: Michael Hopkins | last post by:
Hi all I have a subclass of valarray<T> thus template <typename T> class uo_val : public std::valarray<T> { public: uo_val ( ) : std::valarray<T>() {} uo_val (const int sz ) :...
8
1658
by: Lou Pecora | last post by:
I've been scanning Python in a Nutshell, but this seems to be either undoable or so subtle that I don't know how to do it. I want to subclass a base class that is returned from a Standard Library...
1
4977
by: s.lipnevich | last post by:
Hi All, Is anything wrong with the following code? class Superclass(object): def __new__(cls): # Questioning the statement below return super(Superclass, cls).__new__(Subclass) class...
5
2138
by: Ken Schutte | last post by:
Hi, I'm been trying to create some custom classes derived from some of python's built-in types, like int and list, etc. I've run into some trouble, which I could explain with a couple simple...
31
3515
by: damacy | last post by:
hi, there. i have a problem writing a program which can obtain ip addresses of machines running in the same local network. say, there are 4 machines present in the network; , , and and if i...
3
1550
by: davidfinance | last post by:
Ok, maybe this is a stupid question, but why can't I make a subclass of datetime.date and override the __init__ method? --- from datetime import date class A(date): def __init__(self, a,...
6
4563
by: Me | last post by:
I need to be able to acces non-virtual members of sublcasses via a base class pointer...and without the need for an explicit type cast. I thought a pure virtual getPtr() that acts as a type cast...
4
3465
by: Kurt Smith | last post by:
Hi List: Class inheritance noob here. For context, I have the following base class and subclass: class Base(object): def __init__(self, val): self.val = val
0
7049
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
6912
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
7052
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
7092
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
5348
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,...
1
4790
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...
0
4488
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
1304
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 ...
1
565
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.