Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: [Request / Listing] Making classes as templates  (Read 2984 times)

0 Members and 1 Guest are viewing this topic.

Ben

[Request / Listing] Making classes as templates
« on: April 15, 2006, 10:55:27 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 437
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 :).
« Last Edit: February 24, 2012, 10:12:49 am by Niek »
Logged
Want a place to upload your sprites and games for FREE? Look no further than GameDevotion

aab

^ Evolved from a Hobbit
Re: Making classes as templates
« Reply #1 on: April 15, 2006, 01:20:36 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 992
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;
};
Logged




I ♥ Sol
.... I ♥ Sol ? wtf how long has that been there? >_> *rrrrrrrrar*
  • MySpace

Ben

Re: Making classes as templates
« Reply #2 on: April 15, 2006, 03:06:49 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 437
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?
Logged
Want a place to upload your sprites and games for FREE? Look no further than GameDevotion

aab

^ Evolved from a Hobbit
Re: Making classes as templates
« Reply #3 on: April 16, 2006, 01:00:37 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 992

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.
Logged




I ♥ Sol
.... I ♥ Sol ? wtf how long has that been there? >_> *rrrrrrrrar*
  • MySpace

Ben

Re: Making classes as templates
« Reply #4 on: April 16, 2006, 02:25:54 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 437
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.
Logged
Want a place to upload your sprites and games for FREE? Look no further than GameDevotion
Pages: [1]   Go Up

 


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



Page created in 0.095 seconds with 47 queries.

anything