ZFGC

Resources => Coding => Topic started by: Ben on April 15, 2006, 10:55:27 am

Title: [Request / Listing] Making classes as templates
Post by: Ben on April 15, 2006, 10:55:27 am
Hey, I've been using wxWidgets for a while and in wxWidgets your able  to make your own classes based off of other classes, ie:
Code: [Select]
class myDialog : public wxDialog{
...
};

And I was wondering how I would do this myself, as in create the template class, I tried making a normal class and then using it as a template, but I could never get it to compile.

Help please :).
Title: Re: Making classes as templates
Post by: aab on April 15, 2006, 01:20:36 pm
You mean to inherit a template class?
Code: [Select]
template<typename T>
class parent{
T old_t;
};

template<typename T>
class child: parent<T> {
T young_t;
};
Title: Re: Making classes as templates
Post by: Ben on April 15, 2006, 03:06:49 pm
I dont' know, XD.

In wxWidgets you can create a class in the way I said and then rewrite the constructor, so that it calls the basic class constructor (of the parent) and then you can put all your controls in. (Controls are the things like text boxes and stuff).
A regular wxDialog really doesn't do much apart from be a blank window. But somethign built up off of it with controls and whatever becomes your window.

Do you understand?
Title: Re: Making classes as templates
Post by: aab on April 16, 2006, 01:00:37 am

Well, the syntax for this makes little sense, as your calling an implicit constructor for the base with its typename, as appose to an actual identifier of an object......just look at this example:

The parent has values a,b and c.
The child inherits, and adds values d.e and f.

The parent has a constructor for (a,b,c).
The child has a constructor for values (a,b,c,d,e,f).

Code: [Select]
#include<iostream>


class parent{
public:


parent
(
const int ia,
const int ib,
const int ic
):
a(ia),
b(ib),
c(ic)
{

}

int a,b,c;
};



class child: public parent {
public:

child
(
const int ia,
const int ib,
const int ic,
const int id,
const int ie,
const int If
):
parent(ia,ib,ic),
d(id),
e(ie),
f(If)
{
}

void out()const
{
using std::cout;
cout
<<a<<' '
<<b<<' '
<<c<<' '
<<d<<' '
<<e<<' '
<<f<<' ';
}

int d,e,f;
};



int main()
{
child c(0,1,2,3,4,5);  //Ok, so here we want to

c.out();  //this will print out a,b,c,d,e then f's values

std::cout<<std::endl<<"Press return to continue"<<std::endl;
std::cin.get();

return 0;
}
In the childs constructor, we could have went a=ia, b=ib, but your prblem was that you wanted to involve an implicit initialisation so that you could pass arguments to the constructor of the class your inheriting off of (ie wxDialog).
Well, as you can see from the above example, the implicit allocation uses the name of the type your inheriting off of, even though its not a variable...So it doesnt make too much sense, but its usefull. (parent)(*this)(a,b,c) would make more and less sense, less because *this doesnt exist yet.
Title: Re: Making classes as templates
Post by: Ben on April 16, 2006, 02:25:54 pm
Heh, aab there may have been some confusion with the word template in there, i didn't realise it was a C++ keyword, lol.
I'm sorry, I keep wasting your time, but I have got this now :).
Thanks for the help :D.

Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved