php styled vars in c++

Discussion in 'C++' started by Christian Hofmann, Jun 12, 2004.

  1. 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
     
    Christian Hofmann, Jun 12, 2004
    #1
    1. Advertising

  2. Christian Hofmann

    Pete C. Guest

    Christian Hofmann wrote:
    > 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
     
    Pete C., Jun 12, 2004
    #2
    1. Advertising

  3. Christian Hofmann

    JKop Guest

    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
     
    JKop, Jun 12, 2004
    #3
  4. Christian Hofmann

    Siemel Naran Guest

    "Christian Hofmann" <> wrote in message
    news:caf3ct$4lv$04

    > 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);
     
    Siemel Naran, Jun 12, 2004
    #4
  5. "Siemel Naran" <> in
    news:p6Jyc.53193$...

    > 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
     
    Christian Hofmann, Jun 13, 2004
    #5
  6. "Pete C." <> in
    news:vmEyc.11154$...

    > 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
     
    Christian Hofmann, Jun 13, 2004
    #6
  7. "JKop" <> in news:3gGyc.2084$...

    > 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
     
    Christian Hofmann, Jun 13, 2004
    #7
  8. Christian Hofmann

    Pete C. Guest

    Christian Hofmann wrote:
    <snip>
    >
    > 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
     
    Pete C., Jun 13, 2004
    #8
  9. Christian Hofmann

    JKop Guest

    Christian Hofmann posted:

    > "JKop" <> in news:3gGyc.2084$...
    >
    >> 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



    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
     
    JKop, Jun 13, 2004
    #9
  10. On Sat, 12 Jun 2004 16:20:26 +0200, Christian Hofmann
    <> wrote:

    > 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.
     
    Andreas Schmidt, Jun 13, 2004
    #10
  11. Christian Hofmann

    Siemel Naran Guest

    "Christian Hofmann" <> wrote in message
    news:cag31j$s8p$00
    > "Siemel Naran" <> in


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


    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.
     
    Siemel Naran, Jun 14, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon

    app vars and cache vars

    Jon, Dec 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    397
  2. Guest
    Replies:
    1
    Views:
    666
    Steven Cheng[MSFT]
    Aug 16, 2005
  3. Linuxguy123
    Replies:
    7
    Views:
    697
    Paddy O'Loughlin
    Feb 20, 2009
  4. Rajive Narain
    Replies:
    0
    Views:
    1,895
    Rajive Narain
    Sep 18, 2009
  5. caccolangrifata
    Replies:
    18
    Views:
    397
    Chris Torek
    Jul 22, 2011
Loading...

Share This Page