Public Group

# Inheritance and Constructors

This topic is 4833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Say I have these two classes:
class Channel {
public:
Channel(UTString n, UTString w);
virtual ~Channel(void);
};


And this one
#include "Channel.h"

class PublicChannel : public Channel {
public:
PublicChannel(UTString n, UTString w);
~PublicChannel(void);
};


My problem is that when I compile PublicChannel, I get the error saying that Channel doesn't have a default constructor. My question is how do I evoke Channel's constructor (the one that takes in 2 parameters) from within PublicChannel? I really don't want to have to make a default constructor for Channel just to get around this problem. Thanks for the help.

##### Share on other sites
PublicChannel(UTString n, UTString w) : Channel(n,w);

...off the top of my head. I am going to go test it now...

##### Share on other sites
Correct. Make use of an initializer list here.

Also, as a style note, please use plain "()" prototypes in C++ rather than "(void)".

##### Share on other sites
Zahlman.

Do you mean just with Constructors/Destructors or with all fumction protoptypes?

##### Share on other sites
Quote:
 Original post by garyfletcherDo you mean just with Constructors/Destructors or with all fumction protoptypes?

Just with the constructors. Understand what is going on here. When you use the initializer list, that data is used to initialize data before the constructor is called.

class Point {    int x;    int y;    int z;    Point() : x(0), y(0), z(0) {        // x, y and z are 0 before this code gets called.    }};

Now why is it important. One reasons it that initializing data this way can speed things up. This is not always the case, but its good to remember. The other reason is to pass things to the base class constructor. When you create an object, notice the order in which they are created and destroyed.

#include <iostream>class A {public:    int num;    A( int n ) : num(n) { std::cout << "A()" << std::endl; }    ~A(){ std::cout << "~A()" << std::endl; }};class B : public A{public:    B() : A(23) { std::cout << "B()" << std::endl; }    ~B(){ std::cout << "~B()" << std::endl; }};int main(){    B b;    std::cout << b.num << std::endl;    return 0;}// outputA()B()23~B()~A()

Objects are created in one order, and deleted backwards. Before the constructor for B is call, A must be created. But A needs an int? So use the initializer list the pass the number before the B constructor code is called.

Hope I helped, not made it worse.

##### Share on other sites
Quote:
 Original post by garyfletcherDo you mean just with Constructors/Destructors or with all fumction protoptypes?

To clarify - initializer lists are a constructor-only feature, but the fact that f() is equivalent (and preferred to) f(void) is true for all functions.

##### Share on other sites
Quote:
Original post by Fruny
Quote:
 Original post by garyfletcherDo you mean just with Constructors/Destructors or with all fumction protoptypes?

To clarify - initializer lists are a constructor-only feature, but the fact that f() is equivalent (and preferred to) f(void) is true for all functions.

Yes. Thank you.

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5
JoeJ
10

• 20
• 13
• 14
• 76
• 22
• ### Forum Statistics

• Total Topics
632139
• Total Posts
3004369

×