derived class constructors

B

BlimeyOReilly

Hi,
I have a problem, any help would be much appreciated.

To simplify it for you I have tried to isolate some code. There is a
base class (class_a) and a derived class (class_b). Not very
intelligent names I know, but bear with me.. My problem is that I would
like to write a constructor for class_b based on an instance of
class_a. I have added the declaration for it, but how do I define
class_b(class_a&) ?????

Cheers
BOR



#define CLASS_A_N 8

class class_a
{
private:
int a;
int* g;
public:
class_a(void): a=0, g(NULL) {}
virtual ~class_a(void)
{
if ( g != NULL )
delete[] g;
}
class_a(class_a& rhs): g(NULL)
{
a = rhs.a;
g = new int[CLASS_A_N];
for ( int i = 0; i < CLASS_A_N; i++)
g = rhs.g;
}
}

class class_b : public class_a
{
private:
int b, c;
public:
class_b(int local_b=0, int local_c=0): b(local_b), c(local_c) {}
~class_b(){};
class_b(class_a&);
}
 
R

Rolf Magnus

BlimeyOReilly said:
Hi,
I have a problem, any help would be much appreciated.

To simplify it for you I have tried to isolate some code. There is a
base class (class_a) and a derived class (class_b). Not very
intelligent names I know, but bear with me.. My problem is that I would
like to write a constructor for class_b based on an instance of
class_a. I have added the declaration for it, but how do I define
class_b(class_a&) ?????

Just like any other constructor. Which problem did you get?
#define CLASS_A_N 8

I'd make that a static const member variable of class_a instead of a macro.
class class_a
{
private:
int a;
int* g;
public:
class_a(void): a=0, g(NULL) {}
virtual ~class_a(void)
{
if ( g != NULL )

That check isn't necessary. delete[] on a null pointer is a no-op.
delete[] g;
}
class_a(class_a& rhs): g(NULL)

That constructor doesn't modify rhs, so it should take it as a const
reference.
{
a = rhs.a;
g = new int[CLASS_A_N];
for ( int i = 0; i < CLASS_A_N; i++)
g = rhs.g;
}
}
;

class class_b : public class_a
{
private:
int b, c;
public:
class_b(int local_b=0, int local_c=0): b(local_b), c(local_c) {}
~class_b(){};
class_b(class_a&);
}


;

So what's the problem now? You have implemented other constructors. This on
is no different in that respect.
 
B

BlimeyOReilly

So what's the problem now? You have implemented other constructors. This on
is no different in that respect.

Well I can write the outside ok. I can initialise the derived class
private members, but I can't seem to find the way to copy the base
class members across from &rhs (which yes should be const too).

BOR
 
R

Rolf Magnus

BlimeyOReilly said:
is no different in that respect.

Well I can write the outside ok. I can initialise the derived class
private members, but I can't seem to find the way to copy the base
class members across from &rhs (which yes should be const too).

Ah, I think now I see the problem. Your class_a has a copy constructor, so
you can just use that. Try something like:

class_b::class_b(const class_a& rhs)
: class_a(rhs),
... initialization of class_b members
{
}

You cannot directly access rhs's members from class_b's constructor, because
they are private.
 
B

BlimeyOReilly

Perfect!!
I hadn't realised you were allowed to specify base class parts by name
in the initialisation list. I knew there had to be a simple solution
to the problem - thank you so much!
BOR

(go RedHotPawn.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,298
Messages
2,571,542
Members
48,283
Latest member
RitaVui655

Latest Threads

Top