php styled vars in c++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christian Hofmann

    php styled vars in c++

    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


  • Pete C.

    #2
    Re: php styled vars in c++

    Christian Hofmann wrote:[color=blue]
    > 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[/color]

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

    - Pete


    Comment

    • JKop

      #3
      Re: php styled vars in c++

      Christian Hofmann posted:
      [color=blue]
      > 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[/color]


      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

      Comment

      • Siemel Naran

        #4
        Re: php styled vars in c++

        "Christian Hofmann" <christian.hofm ann@gmx.de> wrote in message
        news:caf3ct$4lv $04
        [color=blue]
        > 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?[/color]

        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);


        Comment

        • Christian Hofmann

          #5
          Re: php styled vars in c++

          "Pete C." <x@x.x> in
          news:vmEyc.1115 4$uX2.360@newsr ead2.news.pas.e arthlink.net...
          [color=blue]
          > No, but it would not be difficult to make one. You would of course still
          > have to declare the variables, unlike in PHP.[/color]

          that would be ok. Better than nothing :-)

          Christian


          Comment

          • Christian Hofmann

            #6
            Re: php styled vars in c++

            "Siemel Naran" <SiemelNaran@RE MOVE.att.net> in
            news:p6Jyc.5319 3$Gx4.21771@bgt nsc04-news.ops.worldn et.att.net...
            [color=blue]
            > What should be the result of
            >
            > var1 = "5 weeks"
            > var2 = "3 days"[/color]
            [color=blue]
            > Is is 8 or 5*7+3=38?[/color]

            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 :-)

            [color=blue]
            > varplus = atoi(var1) + atoi(var2);[/color]

            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


            Comment

            • Christian Hofmann

              #7
              Re: php styled vars in c++

              "JKop" <NULL@NULL.NULL > in news:3gGyc.2084 $Z14.2122@news. indigo.ie...
              [color=blue]
              > I'm pretty much certain you'll find some re-usable code somewhere on the[/color]
              net[color=blue]
              > that'll do that, I'd search for something like "Variant C++", "Variant[/color]
              Class[color=blue]
              > C++", "Variant Type C++". The resulting actual machine code will be
              > extremely pessimized.[/color]

              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


              Comment

              • Pete C.

                #8
                Re: php styled vars in c++

                Christian Hofmann wrote:
                <snip>[color=blue]
                >
                > 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[/color]

                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


                Comment

                • JKop

                  #9
                  Re: php styled vars in c++

                  Christian Hofmann posted:
                  [color=blue]
                  > "JKop" <NULL@NULL.NULL > in news:3gGyc.2084 $Z14.2122@news. indigo.ie...
                  >[color=green]
                  >> 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.[/color]
                  >
                  > 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[/color]


                  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

                  Comment

                  • Andreas Schmidt

                    #10
                    Re: php styled vars in c++

                    On Sat, 12 Jun 2004 16:20:26 +0200, Christian Hofmann
                    <christian.hofm ann@gmx.de> wrote:
                    [color=blue]
                    > 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?[/color]

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

                    See documentation here:


                    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.

                    Comment

                    • Siemel Naran

                      #11
                      Re: php styled vars in c++

                      "Christian Hofmann" <christian.hofm ann@gmx.de> wrote in message
                      news:cag31j$s8p $00[color=blue]
                      > "Siemel Naran" <SiemelNaran@RE MOVE.att.net> in[/color]
                      [color=blue]
                      > 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 :-)
                      >
                      >[color=green]
                      > > varplus = atoi(var1) + atoi(var2);[/color]
                      >
                      > 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.[/color]

                      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.


                      Comment

                      Working...