By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,086 Members | 1,875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,086 IT Pros & Developers. It's quick & easy.

Problem with hierachy and constructor

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.