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

Derived and base class question

Hi,

I have 2 questions that I could not find an answer for yet. Suppose I
have the following code (only illustrative...):

class A {
A(int parm = 0);
~A();
};

class B : public A {
B();
~B();
};

Question1: If I create an object of type B, then the ctor is
automatically called for A without passing arguments. How (where) do I
change the code such that I can give a parameter to the ctor of A when
creating an object of type B?

Question 2: If I have a vector:

std::vector<A*A_pointers;

then am I allowed to store pointers to B in it also?

Thanks,

Jeroen
May 23 '07 #1
5 1202
On 5/23/2007 11:44 AM, Jeroen wrote:
Hi,

I have 2 questions that I could not find an answer for yet. Suppose I
have the following code (only illustrative...):

class A {
A(int parm = 0);
~A();
virtual ~A();
};

class B : public A {
B();
B(int parm) : A(parm) { }
~B();
};

Question1: If I create an object of type B, then the ctor is
automatically called for A without passing arguments. How (where) do I
change the code such that I can give a parameter to the ctor of A when
creating an object of type B?

Question 2: If I have a vector:

std::vector<A*A_pointers;

then am I allowed to store pointers to B in it also?
Yes. But make the d'tor of A virtual (s. above).
S.
--
Stefan Naewe stefan dot naewe at atlas-elektronik dot com
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
May 23 '07 #2
Stefan Naewe schreef:
On 5/23/2007 11:44 AM, Jeroen wrote:
>Hi,

I have 2 questions that I could not find an answer for yet. Suppose I
have the following code (only illustrative...):

class A {
A(int parm = 0);
~A();
virtual ~A();
>};

class B : public A {
B();
B(int parm) : A(parm) { }
> ~B();
};

Question1: If I create an object of type B, then the ctor is
automatically called for A without passing arguments. How (where) do I
change the code such that I can give a parameter to the ctor of A when
creating an object of type B?

Question 2: If I have a vector:

std::vector<A*A_pointers;

then am I allowed to store pointers to B in it also?

Yes. But make the d'tor of A virtual (s. above).
S.
OK, thanks. For question 1 I meant that when constructing an object B,
the ctor of A always should be called with a certain parameter. I quess
I have to achieve that with:

class B : public A {
B() : A(31515) { } // or whatever parameters I want here...
~B();
};

Can you give me a clue why the dtor of A must be virual? Is that only
'better coding practice' or mandatory by C++?

Jeroen
May 23 '07 #3
On 5/23/2007 12:20 PM, Jeroen wrote:
Stefan Naewe schreef:
>On 5/23/2007 11:44 AM, Jeroen wrote:
>>Hi,

I have 2 questions that I could not find an answer for yet. Suppose I
have the following code (only illustrative...):

class A {
A(int parm = 0);
~A();
virtual ~A();
>>};

class B : public A {
B();
B(int parm) : A(parm) { }
>> ~B();
};

Question1: If I create an object of type B, then the ctor is
automatically called for A without passing arguments. How (where) do I
change the code such that I can give a parameter to the ctor of A when
creating an object of type B?

Question 2: If I have a vector:

std::vector<A*A_pointers;

then am I allowed to store pointers to B in it also?

Yes. But make the d'tor of A virtual (s. above).
S.

OK, thanks. For question 1 I meant that when constructing an object B,
the ctor of A always should be called with a certain parameter. I quess
I have to achieve that with:

class B : public A {
B() : A(31515) { } // or whatever parameters I want here...
~B();
};
Yes, that's the way to go (if 31515 is the desired value...)
Can you give me a clue why the dtor of A must be virual? Is that only
'better coding practice' or mandatory by C++?
http://www.parashift.com/c++-faq-lit....html#faq-20.7
S.
--
Stefan Naewe stefan dot naewe at atlas-elektronik dot com
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
May 23 '07 #4
Stefan Naewe schreef:
On 5/23/2007 12:20 PM, Jeroen wrote:
>Stefan Naewe schreef:
>>On 5/23/2007 11:44 AM, Jeroen wrote:
Hi,

I have 2 questions that I could not find an answer for yet. Suppose I
have the following code (only illustrative...):

class A {
A(int parm = 0);
~A();
virtual ~A();
};

class B : public A {
B();
B(int parm) : A(parm) { }
~B();
};

Question1: If I create an object of type B, then the ctor is
automatically called for A without passing arguments. How (where) do I
change the code such that I can give a parameter to the ctor of A when
creating an object of type B?

Question 2: If I have a vector:

std::vector<A*A_pointers;

then am I allowed to store pointers to B in it also?
Yes. But make the d'tor of A virtual (s. above).
S.
OK, thanks. For question 1 I meant that when constructing an object B,
the ctor of A always should be called with a certain parameter. I quess
I have to achieve that with:

class B : public A {
B() : A(31515) { } // or whatever parameters I want here...
~B();
};

Yes, that's the way to go (if 31515 is the desired value...)
>Can you give me a clue why the dtor of A must be virual? Is that only
'better coding practice' or mandatory by C++?

http://www.parashift.com/c++-faq-lit....html#faq-20.7
S.
OK, thanks guys!
May 23 '07 #5
On May 23, 12:34 pm, Keith Halligan <keith.halli...@gmail.comwrote:
On May 23, 11:20 am, Jeroen <no_m...@thanx.comwrote:
Can you give me a clue why the dtor of A must be virual? Is that only
'better coding practice' or mandatory by C++?
The reason that the destructor must be virtual for A is that if you
have code like A* p =new B; then we delete p, then the destructor for
B should be called to correctly. If you do not have the destructor in
A virtual this cannot occur and A's destructor will only called and
not the objects destructor that p points to.
Maybe. If you do "delete p", and the static type of *p is not
the same as the dynamic type of *p, it is undefined behavior.
For very simple cases, like the above, you will generally end up
just not calling the destructor of the derived class, but this
is not guaranteed, and in more complicated cases, stranger
things (e.g. core dumps) really do happen.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
May 24 '07 #6

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

Similar topics

7
by: Christian Engström | last post by:
When i compile the program listed below with gcc version 3.3.1 (MinGW on Windows XP) I get the following result: Calling 'func(d)': 'base' copy constructor Calling 'func(*d_handle)': 'base'...
24
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
3
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ......
1
by: Mark McDonald | last post by:
This question kind of follows on from Mike Spass’ posting 10/11/2004; I don’t understand why you can’t declare an implicit operator to convert a base class to a derived class. The text...
6
by: John Glover | last post by:
I'm having a very strange problem with XML serialization. I'm writing web services which pass instances of various classes back and forth as parameters and return values of web methods. The...
24
by: AtariPete | last post by:
Hey All, I have a C# question for you regarding up casting (base to derived). I was wondering about the most elegant way (readable, less code) to cast from a base type to its derived type....
5
by: Bushido Hacks | last post by:
I think this message got lost the first time I sent it, so I'm reposting it. I need a little help with Destructors. Suppose I created a multidimmensional array in a Base class that was also...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
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...
0
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...

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.