Jump to content
  • Advertisement
Sign in to follow this  
Kranar

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Correct. Make use of an initializer list here.

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

Share this post


Link to post
Share on other sites
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;
}

// output
A()
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!