Can I declare my Class at several blocks.

S

sandSpiderX

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
 
J

Jonathan Mcdougall

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
 
S

sandSpiderX

:Jonathan

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

sandX
 
J

Jonathan Mcdougall

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
 
J

Jaspreet

sandSpiderX said:
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.
 
M

msalters

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
 
B

benben

sandSpiderX said:
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
 
S

sandSpiderX

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
 
J

Jonathan Mcdougall

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
 
B

benben

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top