php styled vars in c++

C

Christian Hofmann

Hello,

do you know a c++ class that adds php styled variables?

For example:

var1 = "5 dogs"
var2 = 3

var1 + var2 = 8

okay, for some people this looks weird, but is there a class that supports
type-free variables?

Thanks,

Christian
 
P

Pete C.

Christian said:
Hello,

do you know a c++ class that adds php styled variables?

For example:

var1 = "5 dogs"
var2 = 3

var1 + var2 = 8

okay, for some people this looks weird, but is there a class that
supports type-free variables?

Thanks,

Christian

No, but it would not be difficult to make one. You would of course still
have to declare the variables, unlike in PHP.

- Pete
 
J

JKop

Christian Hofmann posted:
Hello,

do you know a c++ class that adds php styled variables?

For example:

var1 = "5 dogs"
var2 = 3

var1 + var2 = 8

okay, for some people this looks weird, but is there a class that
supports type-free variables?

Thanks,

Christian


I'm pretty much certain you'll find some re-usable code somewhere on the net
that'll do that, I'd search for something like "Variant C++", "Variant Class
C++", "Variant Type C++". The resulting actual machine code will be
extremely pessimized.


-JKop
 
S

Siemel Naran

var1 = "5 dogs"
var2 = 3

var1 + var2 = 8

okay, for some people this looks weird, but is there a class that supports
type-free variables?

What should be the result of

var1 = "5 weeks"
var2 = "3 days"

Is is 8 or 5*7+3=38?

For the first way it's easy: just use atof or atoi.

var1 = "5 dogs"
var2 = 3

varplus = atoi(var1) + atoi(var2);
 
C

Christian Hofmann

What should be the result of

var1 = "5 weeks"
var2 = "3 days"
Is is 8 or 5*7+3=38?

var1 = "5 weeks"
var2 = "3 days"

var1 + var2 = 8 yes
var1 . var2 = "5 weeks3 days"

and:

var1 = 21

var2 = var1 + 0.25

so var2 = 21.25

Thats really cool ;-)

adding a garbage-collector (or at least something to avoid memory-leaks")
and the var-class would be complete :)

varplus = atoi(var1) + atoi(var2);

thanks, but that will only work for strings and integer .. I don't want to
use functions like atoi and so on. Just use the vars.

Maybe I will try to write a class on my own.

Chris
 
C

Christian Hofmann

No, but it would not be difficult to make one. You would of course still
have to declare the variables, unlike in PHP.

that would be ok. Better than nothing :)

Christian
 
C

Christian Hofmann

JKop said:
I'm pretty much certain you'll find some re-usable code somewhere on the net
that'll do that, I'd search for something like "Variant C++", "Variant Class
C++", "Variant Type C++". The resulting actual machine code will be
extremely pessimized.

I searched for that phrases, but I had no luck. What is it I should find?
A Codesnip for a template or class or something?

Christian
 
P

Pete C.

Christian Hofmann wrote:
thanks, but that will only work for strings and integer .. I don't
want to use functions like atoi and so on. Just use the vars.

Maybe I will try to write a class on my own.

Chris

This thread got me interested, so I'm writing one. :)
I'll post back here in the next few days, it won't be entirely complete but
should be a great start.

- Pete
 
J

JKop

Christian Hofmann posted:
I searched for that phrases, but I had no luck. What is it I should find?
A Codesnip for a template or class or something?

Christian


A class something like the following:



class Variant
{
enum {
type_string,
type_double,
type_ulong } type;

union {
char data_string[30];
double data_double;
unsignedlong data_ulong; } data;


operator char*(void)
{
//Convert to char and return
}


Variant& operator+(const Variant& in_variant)
{
//Check the types of each Variant
//and work from there
}

};



When I worked with Visual Basic way way back, it had built in things like
this. You could do suff like so:

Dim Numberq as Integer
Dim Stringq as String

Dim SecNumbr as Integer


SecNumbr = Numberq + Stringq



"Dumbed down" is not the term.


-JKop
 
A

Andreas Schmidt

do you know a c++ class that adds php styled variables?

For example:

var1 = "5 dogs"
var2 = 3

var1 + var2 = 8

okay, for some people this looks weird, but is there a class that
supports
type-free variables?

Yes, there is the "any" class in the boost library.

See documentation here:
http://www.boost.org/doc/html/any.html

They also mention boost::lexical_cast there which might even be better for
you, quoting from the docu:

"Converting types that can hold one of a number of possible value types,
e.g. int and string, and freely convert between them, for instance
interpreting 5 as "5" or vice-versa. Such types are common in scripting
and other interpreted languages. boost::lexical_cast supports such
conversion functionality."

A.
 
S

Siemel Naran

Christian Hofmann said:
var1 = "5 weeks"
var2 = "3 days"

var1 + var2 = 8 yes
var1 . var2 = "5 weeks3 days"

and:

var1 = 21

var2 = var1 + 0.25

so var2 = 21.25

Thats really cool ;-)

adding a garbage-collector (or at least something to avoid memory-leaks")
and the var-class would be complete :)



thanks, but that will only work for strings and integer .. I don't want to
use functions like atoi and so on. Just use the vars.

Maybe I will try to write a class on my own.

Sure, write the class, and inside the functions like operator+ use atoi and
atof to convert to int and double. The user won't know. You can always use
atof to be more general.

As for your var1.var2, there's no way because you cannot overload the dot
operator. Maybe some other operator like operator* or an explicit function
name will work.

But why is this class useful at all? Just trying to learn.
 

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,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top