overloading macro

Started by
13 comments, last by nlbs 15 years, 9 months ago
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.
Advertisement
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.
(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 nlbs
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
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++)
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.
@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

This topic is closed to new replies.

Advertisement