By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,910 Members | 1,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,910 IT Pros & Developers. It's quick & easy.

Can I declare my Class at several blocks.

P: n/a
Hi,

I am a C++ SE. I was workingon a project, instantly I thought of trying
to declare a class at several blocks,

is it possible,

like

Class X
{ int i;
};

{ ....}

class X
{
float f;
void f(double d){}
};

is it possible,
is there some way I can achieve this,
I am thinking of building a user defined data type at run time, by
asking infromation from the user.

Help
sandX

Jul 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
> I am a C++ SE. I was workingon a project, instantly I thought of trying
to declare a class at several blocks,
is it possible,


No. Namespaces work that way, but not classes. Classes are compile-time
creatures.
Jonathan

Jul 23 '05 #2

P: n/a
:Jonathan

Well, okey.
So it means I cannot generate data types at compile time.
Is there any language which supports this.

sandX

Jul 23 '05 #3

P: n/a
> Well, okey.
So it means I cannot generate data types at compile time.
Is there any language which supports this.


Not that I know of. Java has an impressive support for reflection
(knowing at run-time informations about a type), but cannot modify
anything at run-time. In fact, compiled languages are, to the best of
my knowledge, incapable of modifying types.

You may look at scripting languages such as PHP, but even then I think
it will be quite difficult.

Perhaps if you enlighten us about your design, we may be of more help.
Jonathan

Jul 23 '05 #4

P: n/a
sandSpiderX wrote:
Hi,

I am a C++ SE. I was workingon a project, instantly I thought of trying
to declare a class at several blocks,

is it possible,

like

Class X
{ int i;
};

{ ....}

class X
{
float f;
void f(double d){}
};

is it possible,
is there some way I can achieve this,
I am thinking of building a user defined data type at run time, by
asking infromation from the user.

Help
sandX


I was wondering if you could use templates to somehow solve the
problem.

template <typename T>
class X
{
T a;
}

So, here T could be int/float/double/etc.

Jul 23 '05 #5

P: n/a


sandSpiderX schreef:
:Jonathan

Well, okey.
So it means I cannot generate data types at compile time.
Is there any language which supports this.


It is possible. Read "Modern C++ Design", or see the Boost MPL
libraries. At run-time things are slightly more difficult, but
a std::map<std::string, boost::any> is often a 95% solution.

Regards,
Michiel Salters

Jul 23 '05 #6

P: n/a

"sandSpiderX" <m7*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,

I am a C++ SE. I was workingon a project, instantly I thought of trying
to declare a class at several blocks,

is it possible,

like

Class X
{ int i;
};

{ ....}

class X
{
float f;
void f(double d){}
};

is it possible,
is there some way I can achieve this,
I am thinking of building a user defined data type at run time, by
asking infromation from the user.

Help
sandX


If you only want to pull a complete class definition apart in several
blocks, I guess you can do it by multiple inheritance:

class X;

class X_block1
{
friend class X;
//...
};

class X_block2
{
friend class X;
// ...
};

//...

class X: private X_block1, private X_block2 ...
{
// constructor, destructor, etc...
};

But if you want to change type definition at runtime, there's NO WAY you can
do it. In fact, type definition is a concept only exists at compile time.

ben
Jul 23 '05 #7

P: n/a
I am inteding to build a game ....
In this game, its an RPG, all humans are ientified by their true
traits...

So , I am building datatypes at runtime...
Else, generalising , human traits is quiet huge and ineffecient..

Its like, the player logs in, enters his traits, and start playing,
I create a data type based on his info at run time and tell game's AI
that hey here is another player, add and modify.

This game idea , I find is too real.
Help me with this

sandX

Jul 23 '05 #8

P: n/a
> I am inteding to build a game ....
In this game, its an RPG, all humans are ientified by their true
traits...
Ok.
So , I am building datatypes at runtime...
Else, generalising , human traits is quiet huge and ineffecient..
Humans may be caracterised by generic traits. How generic they are
depends on the design. For example, you may have an 'intelligence'
value, from 1 to 20, or different values for 'memory', 'imagination'
and 'reason'. Or something like that.
Its like, the player logs in, enters his traits, and start playing,
I create a data type based on his info at run time and tell game's AI
that hey here is another player, add and modify.


You don't need runtime type modifications for that, iiuc. What are the
traits? D&D like, such as strength, intelligence and so on? In that
case, put some ints in a class and here you are.

You usually want to change the value, not the type of an object.
Please, explain to us exactly, with a complete example, what you mean
with "creating new data type based on his info".
Jonathan

Jul 23 '05 #9

P: n/a
> I am inteding to build a game ....
In this game, its an RPG, all humans are ientified by their true
traits...

So , I am building datatypes at runtime...
Else, generalising , human traits is quiet huge and ineffecient..

Its like, the player logs in, enters his traits, and start playing,
I create a data type based on his info at run time and tell game's AI
that hey here is another player, add and modify.

This game idea , I find is too real.
Help me with this

sandX


Sounds like you need some database support. Consider std::map<std::string,
std::string> as a growable "generic" data type (in which you have to store
everything in strings).

ben
Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.