## Recommended Posts

##### Share on other sites
no I want to make a $_SESSION() macro which will behave like PHP's$_SESSION and abstract other Complex CGI Librery method Calls

##### Share on other sites
In which language?

##### Share on other sites
Man why you are not understanding

#define $_SESSION(k) session::get(k) #define$_SESSIOn(k, v) session::add(k, v)

but compiler warns that multiple defenition of $_SESSION macro C++ doesn't forbids macros starting with$

##### Share on other sites
Quote:
 Original post by nlbsMan why you are not understanding
Well, it's been five messages already in this thread, and even though I mentioned in the first message that you didn't specify the language you were enquiring about, you've waited until now to mention C++. Why have you not mentioned that earlier, thereby saving me the wasted time of proposing a PHP solution?

Seriously, you're just wasting everyone's time. The reason why you don't get the correct answer is because you suck at asking questions.

Quote:
 but compiler warns that multiple defenition of $_SESSION macro Macros cannot be overloaded. Use a different solution, for instance create a class with an overloaded operator[] returning an object with an impicit conversion (that calls get) and an assignment operator (that calls set). Quote:  C++ doesn't forbids macros starting with$

##### Share on other sites
Quote:
 Man why you are not understanding

Don't be rude to people trying to help you. You're original questions are far too vague; we don't read minds here.

Quote:
 but compiler warns that multiple defenition of $_SESSION macro Because you cannot overload macros. They are part of the preprocessor, and exist for simple, naive textual substitution. Quote:  ok so if it cant be overloaded how can i make a macro that can accept one argument as well as two argument too. You can't, the macros have to have different names. You can also use a function (an actual, honest C++ function) which can be overloaded. Quote:  C++ doesn't forbids macros starting with$...I am using g++ and I've 4-5 macro's already running well who's name starts with $sign. Yes it does, '$' is not in the basic source character set. Visual C++ and some other compilers (including GCC) will let you use the character in an identifier as a language extension. If you disable extensions then it should provide you with an error (if it doesn't, the compiler is non-compliant with the C++ standard on that point).

##### Share on other sites
ok so if it cant be overloaded how can i make a macro that can accept one argument as well as two arguments too. with teh same name e.g. one optional argument

##### Share on other sites
You can't. We've already said this a number of times. A macro isn't going to cut it here, you need to use something else, like a function.

The closest you might be able to get with a macro is the nonstandard, compiler-specific support for varadic macros, but this generally only gives you something like _VA_ARGS_ that is expanded; to get this to work, you'd need a function anyway.

##### Share on other sites
Using macros here is wrong (no ifs or buts).

A PHP equivalent for $SEESION in C++ is std::map<std::string, std::string>. <?phpsession_start();$_SESSION['views'] = 1; // store session dataecho "Pageviews = ". $_SESSION['views']; //retrieve data?> C++ equivalent: typedef std::map<std::string, std::string> Session;Session session;session["Views"] = "1"; // C++ doesn't support dynamic typesstd::cout << "Pageviews = " << session["Views"]; You can however get fancy, and try (not sure if it works), the boost or cdiggins 'any' type. In which case: typedef std::map<std::string, boost::any> Session;Session session;session["Views"] = 1; // Can now store int, string or any other typestd::cout << "Pageviews = " << session["Views"]; But this is untested, I don't know if implicit conversion work in combination with map. #### Share this post ##### Link to post ##### Share on other sites (I might be mistaking what the OP is trying to do, I'm not familiar with$_SESSION or any of PHP)

Quote:
 Original post by nlbsok so if it cant be overloaded how can i make a macro that can accept one argument as well as two arguments too. with teh same name e.g. one optional argument
You could fake it, like this:

int myMacroFunction(int oneParam, int twoParam){   return whatever;}int myMacroFunction(int oneParam){   return thatOtherThing;}#define MY_OVERLOADED_MACRO myMacroFunction...int myFirstParam, mySecondParam;MY_OVERLOADED_MACRO(myFirstParam);MY_OVERLOADED_MACRO(myFirstParam, mySecondParam);

What do you gain from doing it like that? Nothing, really. It complicates your code, and just by reading the name of the macro, '$_SESSION' or 'SESSION', it does not explain what the function does, which is the primary concern of naming any function or variable. I've offered that solution, because you've presented a problem, and I (think) I had an answer. I'm warning you, however, that there are some ways of writing code that improves the readability of your programs, and some ways of writing code that obscures what the program is doing. The method I show above, obscures your code. It'd be far better to just use myGetFunction(), and mySetFunction(), but it's your code, do what you like. [smile] $_SESSION(userID); //Uh, what's this doing?GetSessionData(userID); //Oh, this function is easier to understand. I don't know PHP, but I can see that this is retrieving data of some kind.$_SESSION(userID, data); //Huh? Seriously, if I saw this, I wouldn't know what to think.SetSessionData(userID, data); //While I still don't know what 'session' is, the name of this function clearly tells me it's storing data. Naming functions, constants/enums, and variables are an incredibly important part of programming. Good names makes a program easier to add to later, easier to see mistakes, and easier to work cooperatively with someone else. Also, GameDev.net has people working with C, C++, C#, Python, Java, Perl, and many programming laungages I've never even heard of. Alot of the people here even know ASM. [smile] (I personally know only C++) #### Share this post ##### Link to post ##### Share on other sites Quote:  Original post by Servant of the Lord(I might be mistaking what the OP is trying to do, I'm not familiar with$_SESSION or any of PHP)

From manual:
Quote:
 When you want to store user data in a session use the \$_SESSION associative array.

It is std::map equivalent.

It really is std::map<std::string, any>, where any is one of 'any' arbitrary type containers.

None of the void * or BaseObject * or similar.

If you use the above approach, you get functional and *syntactic* equivalent.

And it even works if one wants to use static (sigh) session object. Just use: static/extern Session session; which matches the OP's apparently static implementation.

##### Share on other sites
@Servant of the Lord
Thats what I was asking for.

actually I am making a CGI application so I thought to make a common Shared librery before doing that so it will need less memory.

and Session functionality is already done I just need an user friendly macro that function method is of work.

I can use Session::add() Session::get() etc.....

I am mainly a Web developer
(I personaly know PHP and C++)

I thought its only C++ Coders forum LOL

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628375
• Total Posts
2982319

• 10
• 9
• 14
• 24
• 11