Inheritance and Constructors

Started by
5 comments, last by Zahlman 18 years, 10 months ago
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.
Advertisement
PublicChannel(UTString n, UTString w) : Channel(n,w);


...off the top of my head. I am going to go test it now...
Correct. Make use of an initializer list here.

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

Do you mean just with Constructors/Destructors or with all fumction protoptypes?
Gary.Goodbye, and thanks for all the fish.
Quote:Original post by garyfletcher
Do 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.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

Quote:Original post by garyfletcher
Do 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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
Quote:Original post by garyfletcher
Do 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.

This topic is closed to new replies.

Advertisement