473,288 Members | 2,350 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,288 software developers and data experts.

reusing super class constructor code

I have two classes:

aule_gl_window (parent class)

and

aule_button (sub class)

I want to call the super class (parent) constructor code from the sub
class constructor.

I am trying this:

aule_button::aule_button()
{
aule_gl_window::aule_gl_window();
}

and it does not work at all.

Any ideas?

Cheers!
Jul 19 '05 #1
7 13177
On Fri, 27 Jun 2003 20:44:44 -0700, Robin Forster wrote:
I have two classes:

aule_gl_window (parent class)

and

aule_button (sub class)

I want to call the super class (parent) constructor code from the sub
class constructor.

I am trying this:

aule_button::aule_button()
{
aule_gl_window::aule_gl_window();
}

and it does not work at all.

Any ideas?


When you instantiate the sub-class (derived class), the parent class' ctor
is called automatically before the derived class' ctor. You could call it
explicitly by writing it in the member initialization list, like:

aule_button::aule_button(): aule_gl_window() { }

HTH,
-Dhruv.


Jul 19 '05 #2
Robin Forster wrote:
I have two classes:

aule_gl_window (parent class)

and

aule_button (sub class)

I want to call the super class (parent) constructor code from the sub
class constructor.
There is no need to. The default constructors of every base class and
every member variable of class type are called automatically. If you
want a non-default constructor to be called instead, you can specify
that in the initializer list.

aule_button::aule_button()
: aule_gl_window(the, parameters)
{
}
I am trying this:

aule_button::aule_button()
{
aule_gl_window::aule_gl_window();
}

and it does not work at all.


Note that "does not work at all" is not a useful error description.

Jul 19 '05 #3
FYI, I was playing with inheritance yesterday to test how/whether the
child constructor/destructor would call those of the parent. Below is my
test code. Obviously I found out what people have been telling you: That
the child's constructor calls the parent's default constructor
automatically unless otherwise specified in the initializer list.

The attempt to call the parent's default constructor *outside* the
initializer list and actually in the body of the child's constructor bit
me, too (i.e., it did not initialize the child's data members). You said
that "did not work".

Suzanne
-------------------------------------------------------------------------------
// http://www.cs.unc.edu/~vogel/playpen...nheritance.cpp
/************************************************** ****************************
* File: Inheritance.cpp
* Purpose: Test inheritance, with use of parent's constructor and
destructor.
************************************************** ****************************/
#ifndef _STD_USING
#define _STD_USING // Must be #define'd in order to include stddef.h.
#endif // _STD_USING

#include <iostream>

class Parent {
public:
static const int filler = 99;
int* ints;
int n;
int magicNumber;
public:
Parent(const int n=10) {
std::cout << "Parent constructor #1\n";
this->n = n;
this->ints = new int[n];
for (int i=0; i<this->n; i++) {
this->ints[i] = Parent::filler;
}
this->magicNumber = -1;
}
Parent(const int n, const int m) {
std::cout << "Parent constructor #2\n";
this->n = n;
this->ints = new int[n];
for (int i=0; i<this->n; i++) {
this->ints[i] = Parent::filler;
}
this->magicNumber = m;
}
~Parent() {
std::cout << "Parent destructor\n";
delete[] this->ints;
}
};

class Child : public Parent {
public:
Child(const int n=10) {//: Parent(n) { // Parent default
constructor called automatically!
std::cout << "Child constructor #1\n";
}
Child(const int n, const int m) : Parent(n,m) { // Call parent
constructor!!!
std::cout << "Child constructor #2\n";
//Parent::Parent(n,m); // This is *not* valid!
}
~Child() { // Parent destructor is called automatically!
std::cout << "Child destructor\n";
}
};

int main(int argc, char** argv) {

// 1. Test whether parent destructor is called automatically (it is).
Child* c0 = new Child();
int* a = c0->ints;
delete c0;

if (a[0]==Parent::filler) {
std::cout << "ERROR: Child not deleted!\n"; // Not printed out
(Good!)
}
std::cout << a[0] << "\n"; // Senseless number (Good!)

//-------------------------------------------------------------------------
// 2. Test whether parent constructor is called automatically (it is),
// and how. Turns out the *default* parent constructor is called
automatically,
// unless the child calls another constructor *before* the block {}.

std::cout <<
"//--------------------------------------------------------\n";
Child* c1 = new Child(99, 10);
int* b = c1->ints;

std::cout << "magic number: " << c1->magicNumber << "\n";
std::cout << b[0] << "\n";
delete c1;

return 1;
}

Jul 19 '05 #4
"Dhruv" <dh*******@gmx.net> wrote...
On Fri, 27 Jun 2003 20:44:44 -0700, Robin Forster wrote:
I have two classes:

aule_gl_window (parent class)

and

aule_button (sub class)

I want to call the super class (parent) constructor code from the sub
class constructor.

I am trying this:

aule_button::aule_button()
{
aule_gl_window::aule_gl_window();
}

and it does not work at all.

Any ideas?


When you instantiate the sub-class (derived class), the parent class' ctor
is called automatically before the derived class' ctor. You could call it
explicitly by writing it in the member initialization list, like:

aule_button::aule_button(): aule_gl_window() { }


This technique is called "initialisation of base classes", not
"calling constructors explicitly". Please do not confuse others.
Constructors cannot be called.

Victor
Jul 19 '05 #5
Thanks for the input. I am inferring that the following is a
possibility:

sub class:

aule_button::aule_button(int width, int height): aule_gl_window(TYPE,
width, height)
{
etc...
}

where TYPE is some constant that initializes a super class instance
variable.
Jul 19 '05 #6
Suzanne Vogel wrote:
Rolf,

Thanks for your tips. I didn't expect to get tips when I posted my
code.
When I answer postings that contain source code, I always give tips if I
can, even if they weren't asked for. I hope not to look too much like a
smartass by doing so. I just want to help clear things up.
Rolf Magnus wrote:
Suzanne Vogel wrote:
#ifndef _STD_USING
#define _STD_USING // Must be #define'd in order to include stddef.h.
#endif // _STD_USING


Er, what?


On my computer, if I don't write "#define _STD_USING" before trying to
include stddef.h (or some other header files), I get an error that the
"exit()" function is not defined.


Hmm. Sounds strange.
It's not really useful to declare a function or constructor as taking
a const parameter by value, since the function body cannot change the
argument that's passed to it anyway.


Oh.
You could (and should) put initializations in an initializer list.
Also note that this->... isn't needed.


I use "this->" before each data member only as a means of documenting
my code, to denote that I'm using data members and not local function
variables. Some people document data members by naming them with
something like "m_[something]", but I don't to modify the names of the
data members so I simply modify my syntax of how I access them
("this->"). Other suggestions welcomed...


I see. Would be too much typing for my taste :)
int main(int argc, char** argv) {

// 1. Test whether parent destructor is called automatically (it
is). Child* c0 = new Child();
int* a = c0->ints;
delete c0;

if (a[0]==Parent::filler) {

That's not a good idea. If the parent part is deleted (which it is),
you are trying to access memory that doesn't belong to your program
anymore. Depending on your platform and the "situation" or maybe on
the moon phase, a[0] might still be set to Parent::filler, or it
might be overwritten with something else, or your program might get
terminated because you're not allowed to access that memory anymore,
or anything else can happen. DON'T do that!


I did it just as a test to see whether the parent destructor was
properly selected. I *knew* that if the data came back senseless (as
it did), then the parent destructor had been selected. (The opposite
would not necessarily be true, as you pointed out: It is *not*
necessarily true that if the data did not come back senseless, then
the parent destructor had not been selected.)


Ok. I just wanted to note that nobody should expect your test to show
what really happens. It might be the case on your OS/compiler
combination, but this can be different on other systems.

Jul 19 '05 #7
aule_button::aule_button(): aule_gl_window() { }


This technique is called "initialisation of base classes", not
"calling constructors explicitly". Please do not confuse others.
Constructors cannot be called.

Victor


Ok.

-Dhruv.




Jul 19 '05 #8

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

Similar topics

3
by: Phil Powell | last post by:
<?php class SuperClass { var $mySuperClassVar; function SuperClass($myVar) { $this->mySuperClassVar = $myVar; echo "super class var = $myVar<p>"; }
2
by: Michael P. Soulier | last post by:
Ok, this works in Python on Windows, but here on Linux, with Python 2.4.1, I'm getting an error. The docs say: A typical use for calling a cooperative superclass method is: class C(B):...
6
by: cppaddict | last post by:
Hi, I know that C++ does not have an explicit super() constructor for calling a Base class constructor from a Derived class's constructor, but my understanding is that C++ implements this...
4
by: Alex Hunsley | last post by:
I've seen a few discussion about the use of 'super' in Python, including the opinion that 'super' should only be used to solve inheritance diamond problem. (And that a constructor that wants to...
1
by: Martin | last post by:
Hi, I've got a base/super class with just a default constructor. I want another constructor, but I can't modify the base class, so I created a sub class with a constructor like so: public...
0
by: jhhbr549 | last post by:
Need to write a class called Sales that is extends from a super class called Employee. I feel like I am going in the wrong direction. look at this: Here are the specs: static double field...
10
by: Finger.Octopus | last post by:
Hello, I have been trying to call the super constructor from my derived class but its not working as expected. See the code: class HTMLMain: def __init__(self): self.text = "<HTML><BODY>";...
6
by: jmarcrum | last post by:
Hi everyone! I'm using a super class (DVD.java) that handles another class (EnhancedDVD.java). I want to pass the "details" of the DVD into the super class DVD.java. The super class contains the...
25
by: dylan m. austin | last post by:
Hello list. This is my first message and it's nothing needful but rather playful. I'd just like to hear some thoughts on something I consider to be a curiosity of javascript usage. I've seen a lot...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.