473,395 Members | 1,541 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.

Problem with hierachy and constructor

Hello. I want to do something like this:

class A { // It's virtual
protected:
float* data;
int n;
public:
A(int a);
virtual float* createData();
//...
};

class B : public A {
public:
float* createData();
};

A::A(int a)
{
n=a;
data=createData();
}

float* B::createData()
{ // ... // }

That is, to construct a derived class of A, I only need to define
createData. But I get compiling errors. What's the problem?

Jul 22 '05 #1
6 2013
On Tue, 26 Oct 2004 08:57:06 GMT, "Nafai" <na*******@yahoo.es> wrote:
Hello. I want to do something like this:

class A { // It's virtual
protected:
float* data;
int n;
public:
A(int a);
virtual float* createData();
//...
};
It's not a good idea to have protected data members.
class B : public A {
public:
float* createData();
};

A::A(int a)
{
n=a;
data=createData();
}

float* B::createData()
{ // ... // }

That is, to construct a derived class of A, I only need to define
createData. But I get compiling errors. What's the problem?


You have no default constructor for the base class, so you must
initialize it in B's constructor ... which is also missing.

Also, the base class is created *before* the derived class, so you can
only call the base class implementation of createData() ... which I
don't see anywhere. As a rule, never call any virtual functions in the
constructors of either the base or the derived class, nor in their
destructors.

A should also have a virtual destructor.

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #2
Bob Hairgrove posted:
On Tue, 26 Oct 2004 08:57:06 GMT, "Nafai" <na*******@yahoo.es> wrote:
Hello. I want to do something like this:

class A { // It's virtual protected:
float* data;
int n;
public:
A(int a);
virtual float* createData(); //...
};


It's not a good idea to have protected data members.

I'm sure they'd come in handy somewhere, just like how you can explicitly
call an object's destructor.

class B : public A { public:
float* createData(); };

A::A(int a)
{
n=a;
data=createData(); }

float* B::createData()
{ // ... // }

That is, to construct a derived class of A, I only need to define
createData. But I get compiling errors. What's the problem?


You have no default constructor for the base class, so you must
initialize it in B's constructor ... which is also missing.

Also, the base class is created *before* the derived class, so you can
only call the base class implementation of createData() ... which I
don't see anywhere. As a rule, never call any virtual functions in the
constructors of either the base or the derived class, nor in their
destructors.

A should also have a virtual destructor.

Only if B has defined a destructor, and if you think that it's likely that
an object of B would be destroyed via a pointer to A, as in:

A* blah = new B;

delete blah;
To define virtual destructors just under the premise that "I'm being
inherited from!" is damn right inefficient.

Although I would conceed that if one were writing reusable code, then
informative documentation would be in order:
/* NB: This class's destructor is not virtual */
class Blah
{
...
-JKop
Jul 22 '05 #3

"Nafai" <na*******@yahoo.es> wrote in message
news:Cj***********************@telenews.teleline.e s...
Hello. I want to do something like this:

class A { // It's virtual
protected:
float* data;
int n;
public:
A(int a);
virtual float* createData();
//...
};

class B : public A {
public:
float* createData();
};

A::A(int a)
{
n=a;
data=createData();
}

float* B::createData()
{ // ... // }

That is, to construct a derived class of A, I only need to define
createData. But I get compiling errors. What's the problem?


There are several rules related to constructors.

First part of the problem:
A default constructor is generated by the compiler for any class that has no
constructor declared. If you declare a constructor of the class (see A( int
a)), there will be no default constructor generated anymore. If you need the
default constructor you should declare and implement it explicitly.

The second part of the problem:
In the derived class, if you don't call explicitly the base class'
constructor (like B(): A( 10) {} ) the compiler tries to generate code for
calling the default constructor of the base class. Since A has no default
constructor, the compiler fails and a compiler error is generated.

The third part of the problem:
B has no constructor declared, so a default constructor is generated by the
compiler (see First part of the problem). That generated default constructor
calls the default constructor of the class A (see the second part of the
problem), which doesn't exist (see First part of the problem :), so a
compiler error is generated.

The conclusion:

Define the default constructor for B and call the constructor of A.
Something like this:

B::B(): A( 0) { }

Br/
Catalin
Jul 22 '05 #4
In message <Cj***********************@telenews.teleline.es> , Nafai
<na*******@yahoo.es> writes
Hello. I want to do something like this:

class A { // It's virtual
protected:
float* data;
int n;


That looks like rolling your own std::vector<float> to me.
Why not use a real one?

--
Richard Herring
Jul 22 '05 #5
In message <BC*******************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
Bob Hairgrove posted:
On Tue, 26 Oct 2004 08:57:06 GMT, "Nafai" <na*******@yahoo.es> wrote:
Hello. I want to do something like this:

class A { // It's virtual
protected:
float* data;
int n;
public:
A(int a);
virtual float* createData(); //...
};
class B : public A {
public:
float* createData();
};

[...]
A should also have a virtual destructor.
Only if B has defined a destructor,


It has, whether you can see it or not.
and if you think that it's likely that
an object of B would be destroyed via a pointer to A, as in:

A* blah = new B;

delete blah;
Well, given the presence of a virtual function, that's a pretty good
bet.
To define virtual destructors just under the premise that "I'm being
inherited from!" is damn right
downright?
inefficient.

You do know what is the root of all evil?

--
Richard Herring
Jul 22 '05 #6
It's just an example. The actual class is completely different from a
vector.
"Richard Herring" <ju**@[127.0.0.1]> escribió en el mensaje
news:hp**************@baesystems.com...
In message <Cj***********************@telenews.teleline.es> , Nafai
<na*******@yahoo.es> writes
Hello. I want to do something like this:

class A { // It's virtual
protected:
float* data;
int n;


That looks like rolling your own std::vector<float> to me.
Why not use a real one?

--
Richard Herring

Jul 22 '05 #7

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

Similar topics

17
by: Dave | last post by:
Hi I'm making a 3D Engine which consists of the class C3DEngine. Part of this engine is a file loader, a class called CMeshLoader. I have made an instance of CMeshLoader in C3DEngine, ie...
4
by: Merlin | last post by:
Hi For any of you that own a copy of GOF book, on page 38 there is a class diagram showing the Glyph hierarchy. Two questions Am I right to say that Image(or Graphic) would be another class...
25
by: Nafai | last post by:
Hello. I'll try to explain my problem with an example: I have the following classes: class A { public: string name; .... };
18
by: Greg G | last post by:
http://mysite.verizon.net/gdguarino/indexnew.htm It's not a high priority site, just some of my photos throughout the years. The address above is an experiment for a new home page. My intended...
2
by: A. Elamiri | last post by:
Hello, I have a list of product categories and each category may or may not have children and it goes down in a varying level of depth. Now I have to pull this category information for...
15
by: satankidneypie | last post by:
Hi, I'm going to start this off with some code as it'll make it easier to explain afterwards... using System; namespace ConsoleApplication1 { /// <summary>
3
by: Tom Machado | last post by:
Hello, I am working on a project and have become a little confused on how to call methods from another class. Before I explain, here is an example: class 0 { main() {
2
by: Pascal | last post by:
Hello, I am beginner with XML. I have created a XML file with a DTD and a XSL stylesheet in order to view my data in HTML. It works. Then I have tried to setup a XSD instead of DTD and add...
8
by: Jim | last post by:
Hi, I am hoping that someone here can help me out on this. The following code works just fine and gives me a nice margin between my image to the left and the text that appears adjacent to it...
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: 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: 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
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.