|
ahmed zahmed wrote: Read my post again.
I did, but I see no misunderstanding on my part. I may not have been clear enough though. All of the things I said above were accurate and I actually checked Visual Studio to make sure they were.
Also anything I 'interpreted' into your posting was accurate - I did not state anything that you didn't say, but you might not have realized the literal meaning of what you were saying. I did not in fact 'interpret' your posting inaccurately, I merely took it literally. I am aware that you did not mean to make (some of) the statements that I read into your posting, therefore I pointed that out to you.
Here's what you said:
ahmed zahmed wrote: It depends on the definition of CString. If it has a operator+(const char*, const char*)...
First: it does not depend on CString, but more on that later.
Second, when you say that a class X 'has a operator Y(...)', then this might mean that either the operator is defined as a member function, or the operator is defined outside the class. Both are possible, and in fact, the class CString does define all overloads of it's operator+() outside the class, not as member functions. You can indeed find them in the class definition, but only as friend declarations. All 7 overloads are declared friend.
So, when I considered the two possibilities of either a member function operator, or a non-member function, I did not misunderstand your posting, I merely considered two possible meanings.
Third, ...
ahmed zahmed wrote: Where did I say anything about a tertiary operator +?
... you might not have meant to, but you implied the possibility of an operator
CString& CString::operator+(const char*, const char*)
Now think about that. When you define a member function with two parameters, then in truth, this function has three parameters: the two arguments you specified, plus the instance of the class itself. This instance will be passed to that member function as a pointer, using the name this . Therefore, when you define an operator as a member function and pass it two parameters, the operator will in fact have 3 parameters. An operator with 3 parameters is a ternary operator.
ahmed zahmed wrote: And CString is NOT a built-in type.
Now you are mistaking me, I did not say that. What I did say was:
Stefan_Lang wrote: you cannot overload operators working on built-in types
I did not explicitely or implicitely relate to CString here. I did implicitely relate to the (imaginary) simple function const char* operator+(const char*, const char*) , not a member function of CString. The arguments that this operator is working on are of type const char* , and that is a built-in type.
Now back to my first point above:
Stefan_Lang wrote: it does not depend on CString
When you have an expression like this
CString s = "foo" + "bar";
then the compiler evaluates (or at least tries to evaluate) it to
CString::operator=(operator+("foo", "bar"));
Now the compiler has to match all overloads of operator+(,) against the argument types. The argument types in this case are (const char*, const char*) . Since neither of the argument types is CString, the compiler can not hope to find one in the list of CString operators! Therefore the implementation of CString is irrelevant in this case!
I hope now my meaning gets clearer.
|
|
|
|
|
Sadly az is a person who thinks he knows a lot more than he actually does. There is no arguing with him. It's a waste of time and effort.
|
|
|
|
|
There is nothing in the posted expression "foo"+"bar" that is or implies "CString", they are two standard language char * expressions. And, as Stefan says, you cannot "add" them.
This is why I wrap them with explicit CString constructors. CString("foo") + CString("bar") when I need to do something like that.
|
|
|
|
|
You are correct, you can't "add" the const char expressions.
However, the OP *did* say the variable was a CString. Given that, and the fact that I said *if* CString had an operator+(const char*, const char*) *then* the code *would* be valid C++.
If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun
|
|
|
|
|
True, that's what he said *but* the statement that he wrote, with the + for addition of the strings, in now way invokes any CString operators on the right side of the assignment (=) so even if CString would have such an operator, it would not be applied since no items on the right side of the expression are of type CString to start with. A CString object would have to be on the right side for its theoretical + operator to apply.
|
|
|
|
|
That was the original error I got, I have alot to learn about c++, but so far so good. Wish I could run my program b someone to make sure I got it right.
|
|
|
|
|
I ended up doing this, but after reading the conversations, I will use your method.
Hmm, so simple, I overlooked it. So I'm removing the below code.
Thank you for the help, the conversations were an interesting insight into speed and optimization. Thisis my first C++ app.
lp_Parameters = "/iu ";
lp_Parameters += "IIS-WebServerRole;";
lp_Parameters += "IIS-WebServer;";
lp_Parameters += "IIS-CommonHttpFeatures;";
lp_Parameters += "IIS-StaticContent;";
lp_Parameters += "IIS-DefaultDocument;";
|
|
|
|
|
Glad i could help. Welcome to the C++ world.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> If it doesn't matter, it's antimatter.<
|
|
|
|
|
Nothing wrong with that, it keeps the code readable. There are lots of reasons for doing something like this, I do it all the time to keep the lines at a reasonable lenght or to see the code in approximately the same layout as the user will see, for example.
ts.Format("%s #%d", KnownExternalSizers[ndx].TargetShortName, theServ->CalcIDNumber);
ts = CString("This page anchors the \"") + ts + CString("\".\r\n\r\n") +
CString("Please use that tool to enter data and make changes to your requirements.\r\n\r\n") +
CString("Do Not Close / Exit that application, doing so will lose any data entered and will not produce results.\r\n\r\n") +
CString("When you use the \"Solve / Submit\" button above, the results of that tool will be combined with the other results of the ") +
master_UserPreferences->GetSizerLongName();
m_messagebox.SetWindowText(ts);
|
|
|
|
|
OK, now why was this downvoted? It reinforces the original question with an example that even experienced programmers (me) use such a construct and it's perfectly reasonable. I'm trying to understand the reasons for the anonymous downvoters choice.
|
|
|
|
|
Correction, it does work, just a package option error in my list.
hmm, the string builds fine, but when ran through the ShellExecuteEx, the lpParameters bombs. with a -214 type of error. Not looking for the answer, just an FYI if someone runs across this thread in the future.
This worked as a small test.
lp_Parameters = "/iu "IIS-WebServerRole;IIS-WebServer;";
modified 27-Sep-11 18:56pm.
|
|
|
|
|
|
On a sidenote, this shouldn't even compile as there is no operator+(const char*, const char*) ! At best you could write:
lp_parameters = CString("/iu;") +
CString("IIS-WebServerRole;") +
But, as I've seen, you already found the better solution.
|
|
|
|
|
Hi,
The problem is:
I have a Cstring Object and inside then there are a function name that is wrote in my aplication code.
How can i start that function using the content of CString ?
It seems to me that i need to seek the memory function address and use then as a pointer...
It it possible ?
Samuel
|
|
|
|
|
Please share any relevant code with us, seeing the code might us help understand the problem you are facing.
You can modify your question to add the code.
0100000101101110011001000111001011101001
|
|
|
|
|
Yep... I did not understand that question at all...
|
|
|
|
|
Sorry for my poor explanation
please, Look at answer sent to Maximilien
Samuel
|
|
|
|
|
Sorry for my poor explanation
please, Look at answer sent to Maximilien
Samuel
|
|
|
|
|
nope, you cannot directly call a function from a string.
If I understand correctly, you have something like this and want to call myFunction .
CString s("myFunction");
What you will need is a factory:
void myFunction(){};
CString s("myFunction");
Factory(s);
void Factory( CString& string);
{
if ( string.Compare("myFunction") == 0 )
{
myFactory();
}
}
Watched code never compiles.
|
|
|
|
|
Thanks for you attention Maximilien.
Realy, today i do this ... but a have so many functions to call and the struct of IF is going to be bigger i was planned.
I want to create a text file and put inside then a struct of tasks as shown above:
1 - Text description of task 1
1.1 - Text description of task 2 (Below to task 1)
1.1.1 - Text description of task 3
2 - Text description of task 4
2.1 - Text description of task 5
2.1.2 - 1.1 (reused it and your childs, if there are)
It seems complex, but it isn't for my aplication and the objective is wish.
As you can see, with this method i can create complex tasks just manipulating the file. obviosly i pass and return data and parameters between the functions using files seems like .ini
the ideia It will be like this:
.h
void GPC_1_2{};
.cpp
--> I create a deque or table and put inside then all functions names found inside text file
--> I receive a command from entrace process to execute task "GPC_1_2"
--> now a want to get the memory address for "GPC_1_2"
--> Using the memory address, i want to start GPC_1_2 function
Ps: Sometime ago i saw, but not save, a code to find the memory address function. it was using just the name of function inside a string object.
Sorry for the poor english, it is not my native language. I'm Brazilian
|
|
|
|
|
You might be able to use LoadLibrary() and GetProcAddress() to accomplish this. See here[^] and here[^].
Chris Meech
I am Canadian. [heard in a local bar]
In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]
|
|
|
|
|
Excellent suggestion. Dynamically lookup names of functions found in a DLL entry vector. That'll do it.
|
|
|
|
|
Chris,
I beliave it works, but i will need many of DLLs and a process to manage then. The struct i'm using will must change a lot to work this way
Samuel
|
|
|
|
|
Samuel Nunes de Arruda wrote: i will need many of DLLs and a process to manage then
This is not true. You can put all your functions into a single DLL and load it once using LoadLibrary() [^]. You can then find any of the exported functions using GetProcAddress() [^] with the name that you read from your text file. You can do this with minimal changes to your existing code.
|
|
|
|
|
You can use an map of function pointers to accomplish this.
This is assuming all the functions have the same signature, that is same return type and same numbers and types of parameters.
typedef void (*VoidFuncType)();
map<string, VoidFuncType> functions;
functions["myFunction1"] = myFunction1;
functions["myFunction2"] = myFunction2;
...
string func = "myFunction1";
...
functions[func]();
Note that myFunction1 and myFunction2 are functions or methods in your code.
If they are member functions, then the typedef will need to be different, and so will the subsequent usage.
Be sure to
#include <map>
If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun
modified 28-Sep-11 17:11pm.
|
|
|
|
|