473,327 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

how to construct a class with member type unknown till runtime?

Hi,

I am now writing a class using template. I run into a situation
that, one of my member type is not unknown until run-time (but i dont'
want to specify a template entry for it). How should I handle this?
template <class T>
class myfirst
{
public:

private:
Array<T> myarray;
void * runtime_change_member;
};
Thanks,

Carson

Sep 5 '05 #1
4 1431
ck*******@gmail.com wrote:
Hi,

I am now writing a class using template. I run into a situation
that, one of my member type is not unknown until run-time (but i dont'
want to specify a template entry for it). How should I handle this?


Derive them all from a polymorphic base class. Then use a pointer to that
base class in your class template.

Sep 5 '05 #2
but if I use pointer to refer, how can I know which type I should cast
to? (cos' i don't know the base class type, as it is polymorphic?)

Carson

Sep 5 '05 #3
ck*******@gmail.com wrote:
but if I use pointer to refer, how can I know which type I should cast
to? (cos' i don't know the base class type, as it is polymorphic?)

Carson


That's what virtual functions are for.

class Shape {
public:
virtual void Draw() = 0;
};

class Circle : public Shape {
public:
void Draw() {
// draw a circle;
}
};

class Rectangle : public Shape {
public:
void Draw() {
// draw a rectangle
}
};

class Polygon : public Shape {
public:
void Draw() {
// guess what this is supposed to do
}
};

template <class T>
class myfirst
{
public:

private:
Array<T> myarray;
void * runtime_change_member;
};

myfirst<Shape*> struct;
Sep 5 '05 #4
On Mon, 05 Sep 2005 04:01:10 +0400, <ck*******@gmail.com> wrote:
template <class T>
class myfirst
{
public:

private:
Array<T> myarray;
void * runtime_change_member;
};


You may use something like Boost.Any, Boost.Variant.
Sep 5 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sebastian Faust | last post by:
Hi, I have 4 questions related to templates. I wanna do something like the following: template<typename T> class Template { public: Template_Test<T>()
2
by: Dave | last post by:
Hello all, I am creating a linked list implementation which will be used in a number of contexts. As a result, I am defining its value node as type (void *). I hope to pass something in to its...
10
by: emma middlebrook | last post by:
Hi I discovered that if you declare a structure (and not 'new()' it) you can then separately initialize its members and the compiler counts those separate statements as a full initialization....
2
by: Larry | last post by:
I'm having trouble finding the right method to determine at runtime the data type of the fields in a class. here's what I've got so far: Public Class ImgSubmissionRecord Public fileName As...
2
by: Crirus | last post by:
I have some question related to the other post about iteration on a class members. I had found a way to figure out when a member of my class is array so I can pull out it's elements to iterate...
8
by: ypjofficial | last post by:
Hi all, In what way does the enumerated data type contibute to the size of a class if its part of that class? eg. #include <iostream.h> class one { public:
12
by: Mark Fink | last post by:
I wrote a Jython class that inherits from a Java class and (thats the plan) overrides one method. Everything should stay the same. If I run this nothing happens whereas if I run the Java class it...
5
by: SunnyDrake | last post by:
HI! I wrting some program part of it is XML config parser which contains some commands(for flexibility of engenie). how do i more simple(if it possible not via System.Reflection or...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.