How can i do this?

I want to make multi typedef app.
Such as this:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <typeinfo>
using namespace std;
int main(){
typedef char,int,string,float,double var;
var a=1;
var b=2;
var c=a+b;
cout<<c<<endl;
var word="Hello";
cout<<word;
return 0;}
Last edited on
C++ is not PHP. Keep dreaming.
Hmm.Why?
Not only is there no real way to do this in the current version of C++, but even when C++0x comes around C++ will remain a strongly-typed language. The addition of auto won't change that. Sorry. :)

-Albatross
Last edited on
How is the compiler to know if a should be treated as a character or an integer or a float or a double? I don't know what to treat it as and I've got the benefit of context.
Last edited on
You can't do this in C++, though you can write an interpreter for another language that allows this in C++.
How can make Pascal,Delphi or Perl this?int,char,double variables can be put computer in nonvoilatile location.Doesn't it?Or has it another method?
@Moschops:
Well... take a look at the constants. That's how the compiler would figure it out.

@Helegurbann:
?

-Albatross
No idea about Perl, but I am fairly certain you can't do that in Pascal/Delphi either.
Yes.I wrong remember:) var vairable has been in Pascal:)Ok.Thanks.
Pascal:
1
2
3
4
5
var a:integer=1;
var b:integer=2;
var c:integer;
c=a+b;
write('result=',c);
Last edited on
Huh? That doesn't work in the pascal I know. As I recall, Pascal had a seperate section for declaring variables.
@albatross

They look like valid char values as well as ints. I could interpret

var a=1

as var is an integer of value 1, var is the character '1', or var is the character that is stored in memory as the number 1. If I take them as chars, I could then take the + as concatenation, except that I know people sometimes add a fixed integer value to a character to change is from upper/lower case to lower/upper case, so I'm really just guessing at that point, and we're one step away from the clusterf that is javascript and the fiasco where '5' + 3 gives '53' but '5' - 3 gives 2.
Last edited on
On a second note, you can actually do something like that with tagged unions... just that of course you would have to implement code that checks which var type we have there, and picks the corresponding operation.

@Moschops: I wasn't aware we had any char or string type that implements the + operator.
@Moschops: the types of those constants are quite clearly defined. It's just that the compiler makes some implicit conversions in some cases. :)

How do you think auto will be working?

-Albatross
Last edited on
var a=1 doesn't work.var a:integer=1 work
I changed in up code block.
@Hanst: std::string uses the '+' operator for concatenation. Additionally, I could overload it at any time and add a shedload of my own such operations.
Topic archived. No new replies allowed.